Compare commits

...

4 Commits

Author SHA1 Message Date
octo@swiss d90ae0da00 aaaa I keep forgetting :(( I'm so R today 2022-11-07 19:25:20 +01:00
octo@swiss 6df053305c Forgot the HTTP error patch 2022-11-07 19:19:44 +01:00
octo@swiss d620cdc4f2 Changeable mail encryption, fix recursion limit 2022-11-07 17:33:38 +01:00
octo@swiss 191d49845f Fix example config 2022-11-07 15:51:13 +01:00
2 changed files with 38 additions and 12 deletions

View File

@ -11,10 +11,11 @@ Feeds = [
] ]
# SMTP configuration (only required for sending mail). # SMTP configuration (only required for sending mail).
MailUsername = "example@example.com" MailUsername = "example@example.com" # Your full email address.
MailPassword = "Example" MailPassword = "Example"
MailServer = "smtp.example.com" MailServer = "smtp.example.com"
MailPort = 465 MailPort = 465 # (Usually) 465 for SSL, 587 for TLS, 25 for No encryption.
MailEncryption = "SSL" # SSL, TLS, or None.
# How often to refresh the feeds (in seconds). Set to 0 for a single run, instead of having the program sleep. # How often to refresh the feeds (in seconds). Set to 0 for a single run, instead of having the program sleep.
LoopTime = 300 LoopTime = 300
@ -41,4 +42,4 @@ MaxTryCount = 5
# Other advanced settings. # Other advanced settings.
AppName = "bottocto-MastodonFeedHTML" AppName = "bottocto-MastodonFeedHTML"
DbFile = "Database.txt" DbFile = "Database.txt"
UserAgent = AppName UserAgent = AppName.replace('-','; ')

View File

@ -9,6 +9,7 @@ from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from urllib.request import urlopen, Request from urllib.request import urlopen, Request
from urllib.error import HTTPError
from Config import * from Config import *
MediaDescsBlock = '<br><details><summary>Media descriptions</summary><ul>\n{Content}</ul></details>' MediaDescsBlock = '<br><details><summary>Media descriptions</summary><ul>\n{Content}</ul></details>'
@ -52,18 +53,21 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo
URL = URL.removesuffix('/').removesuffix('/with_replies') + '/with_replies' URL = URL.removesuffix('/').removesuffix('/with_replies') + '/with_replies'
Usertag = f"{URL.split('/')[-2]}@{URL.split('/')[-3]}" Usertag = f"{URL.split('/')[-2]}@{URL.split('/')[-3]}"
Pages = [] Pages = []
LastEntryIsNew, PageOlder = HandleURL(True, URL, Usertag, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo) LastEntryIsNew, PageOlder = HandleURL(True, URL, Usertag, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo, 1)
if LastEntryIsNew and PageOlder: if LastEntryIsNew and PageOlder:
Pages += [PageOlder] Pages += [PageOlder]
while LastEntryIsNew and PageOlder and len(Pages) < MaxPagesRecursion: while LastEntryIsNew and PageOlder and (MaxPagesRecursion <= 0 or len(Pages) < MaxPagesRecursion):
LastEntryIsNew, PageOlder = HandleURL(True, PageOlder, Usertag, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo) LastEntryIsNew, PageOlder = HandleURL(True, PageOlder, Usertag, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo, 1)
if LastEntryIsNew and PageOlder and MaxPagesRecursion: if LastEntryIsNew and PageOlder:
Pages += [PageOlder] Pages += [PageOlder]
Pages.reverse() Pages.reverse()
for Page in Pages: for Page in Pages:
HandleURL(False, Page, Usertag, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo) TryCount, Try0, Try1 = 0, False, False
while not Try0 and not Try1: # Handle retries
TryCount += 1
Try0, Try1 = HandleURL(False, Page, Usertag, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo, TryCount)
def HandleURL(IsFirstRun, URL, Usertag, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo): def HandleURL(IsFirstRun, URL, Usertag, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo, TryCount):
LastEntryIsNew = False LastEntryIsNew = False
PageOlder = '' PageOlder = ''
try: try:
@ -171,9 +175,20 @@ def HandleURL(IsFirstRun, URL, Usertag, IncludeRetoots, IncludeReplies, LocalSav
.replace('{ Replace:MastodonFeedHTML:MediaDescs }', MakeMediaDescsBlock(MediaDescs)), 'html')) .replace('{ Replace:MastodonFeedHTML:MediaDescs }', MakeMediaDescsBlock(MediaDescs)), 'html'))
for File in MailAttach: for File in MailAttach:
Message.attach(File) Message.attach(File)
with smtplib.SMTP_SSL(MailServer, MailPort, context=ssl.create_default_context()) as Client:
Client.login(MailUsername, MailPassword) if MailEncryption.lower() == 'ssl':
Client.sendmail(MailUsername, MailTo, Message.as_string()) Mailer = smtplib.SMTP_SSL(MailServer, MailPort, context=ssl.create_default_context())
elif MailEncryption.lower() in ('tls', 'none'):
Mailer = smtplib.SMTP(MailServer, MailPort)
if MailEncryption.lower() == 'tls':
Mailer.starttls(context=ssl.create_default_context())
else:
print("[E] MailEncryption variable is set incorrectly. Cannot continue. Please check your config.")
exit(1)
Mailer.login(MailUsername, MailPassword)
Mailer.sendmail(MailUsername, MailTo, Message.as_string())
Mailer.quit()
SleepPrint(MailSleep) SleepPrint(MailSleep)
if LocalSave: if LocalSave:
@ -194,6 +209,16 @@ def HandleURL(IsFirstRun, URL, Usertag, IncludeRetoots, IncludeReplies, LocalSav
return LastEntryIsNew, PageOlder return LastEntryIsNew, PageOlder
except HTTPError as e:
if e.code == 404:
print(e) # TODO: Should get the cached images from the local instance
return True, True
else:
if TryCount == MaxTryCount:
return True, True
print(e)
SleepPrint(PageSleep * (1.5**TryCount))
return False, False
except Exception: except Exception:
raise raise