Handle db per-user; Fix domain name issues

This commit is contained in:
octospacc 2022-08-22 18:35:37 +02:00
parent adb0909297
commit ad45d852ee
1 changed files with 9 additions and 11 deletions

20
MastodonFeedHTML/MastodonFeedHTML.py Normal file → Executable file
View File

@ -12,8 +12,6 @@ from email.mime.text import MIMEText
from urllib.request import urlopen, Request
from Config import *
StripWS = '\t\r\n'
def SureList(Item):
return Item if type(Item) == list else [Item]
@ -26,7 +24,7 @@ def MakePathStr(Str):
def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo):
for URL in URLs:
URL = URL.rstrip('/').rstrip('/with_replies') + '/with_replies'
URL = URL.removesuffix('/').removesuffix('/with_replies') + '/with_replies'
Usertag = f"{URL.split('/')[-2]}@{URL.split('/')[-3]}"
try:
Response = urlopen(Request(URL, headers={'User-Agent':UserAgent}))
@ -39,26 +37,26 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo
Attached = ''
GlobalId = Entry.find('a', class_='u-url')
if GlobalId:
GlobalId = GlobalId['href'].lstrip('https://').lstrip('http://')
GlobalId = GlobalId['href'].removeprefix('https://').removeprefix('http://')
else:
continue
if os.path.isfile(f'{AppName}.db'):
with open(f'{AppName}.db', 'r') as Db:
if GlobalId in Db.read().splitlines():
if f'{Usertag} {GlobalId}' in Db.read().splitlines():
continue
LocalId = GlobalId.split('/')[-1]
Username = Entry.find('a', class_='status__display-name').get_text().strip(StripWS)
Username = Entry.find('a', class_='status__display-name').get_text().strip()
Content = Entry.find('div', class_='e-content')
StatusPrepend = Entry.find('div', class_='status__prepend')
StatusPrepend = StatusPrepend.get_text().strip(StripWS)[len(Username):] if StatusPrepend else ''
StatusPrepend = ' ' + StatusPrepend.strip(StripWS) if StatusPrepend else ''
StatusPrepend = StatusPrepend.get_text().strip()[len(Username):] if StatusPrepend else ''
StatusPrepend = ' ' + StatusPrepend.strip() if StatusPrepend else ''
if not IncludeRetoots and StatusPrepend:
continue
if not StatusPrepend and IncludeReplies and Entry.find('i', class_='fa-reply-all'):
StatusPrepend = ' replied'
Title = Content.get_text().strip(StripWS)
Title = Content.get_text().strip()
Title = f"{Usertag}{StatusPrepend}: {Title[:32]}..."
for Emoji in Entry.find_all('img', class_='custom-emoji'): # Custom emojis in text
Emoji['style'] = 'max-height:1em;'
@ -90,7 +88,7 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo
if Attachments:
for Attachment in Attachments:
Href, Alt = '', ''
Attachment = str(Attachment).strip(StripWS).replace("'",'"').split('"')
Attachment = str(Attachment).strip().replace("'",'"').split('"')
for i,e in enumerate(Attachment):
if e.endswith('<a href='):
Href = Attachment[i+1]
@ -129,7 +127,7 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo
File.write(HTML.replace('{ Replace:Attached }', Attached))
with open(f'{AppName}.db', 'a') as Db:
Db.write(GlobalId + '\n')
Db.write(f'{Usertag} {GlobalId}' + '\n')
except Exception:
raise