From 83487044066fe4cfe5c5152ec4e91e189e53f7fd Mon Sep 17 00:00:00 2001 From: octospacc Date: Tue, 16 Aug 2022 13:23:56 +0200 Subject: [PATCH] Put MastodonFeedHTML in own dir, add Config file --- .gitignore | 2 + .../MastodonFeedHTML.py | 63 ++++++------------- 2 files changed, 22 insertions(+), 43 deletions(-) create mode 100644 .gitignore rename MastodonFeedHTML.py => MastodonFeedHTML/MastodonFeedHTML.py (80%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a7e9c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +Config.py diff --git a/MastodonFeedHTML.py b/MastodonFeedHTML/MastodonFeedHTML.py similarity index 80% rename from MastodonFeedHTML.py rename to MastodonFeedHTML/MastodonFeedHTML.py index 937633f..9bde210 100644 --- a/MastodonFeedHTML.py +++ b/MastodonFeedHTML/MastodonFeedHTML.py @@ -3,38 +3,14 @@ import base64 import feedparser import os import time -import urllib.request import email, smtplib, ssl from bs4 import BeautifulSoup from email import encoders from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText -# from Config import * - -Feeds = [ - { - "URLs": ["https://botsin.space/@sitoctt"], - "IncludeRetoots": True, - "IncludeReplies": True, - "LocalSave": True, - "SendMail": True, - "MailTo": ["example@example.com"] - } -] - -MailUsername = "example@example.com" -MailPassword = "Example" -MailServer = "smtp.example.com" -MailPort = 465 - -NoSpacesFile = False - -LoopTime = 0 -MailSleep = 10 - -AppName = "MastodonFeedHTML" - +from urllib.request import urlopen, Request +from Config import * StripWS = '\t\r\n' @@ -53,7 +29,7 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo URL = URL.rstrip('/').rstrip('/with_replies') + '/with_replies' Usertag = f"{URL.split('/')[-2]}@{URL.split('/')[-3]}" try: - Response = urllib.request.urlopen(URL) + Response = urlopen(Request(URL, headers={'User-Agent':UserAgent})) Data = Response.read() Soup = BeautifulSoup(Data, 'html.parser') Feed = Soup.find_all('div', class_='entry') @@ -61,12 +37,17 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo for Entry in Feed: Attached = '' - print(Entry) GlobalId = Entry.find('a', class_='u-url') if GlobalId: GlobalId = GlobalId['href'].lstrip('https://').lstrip('http://') else: continue + + if os.path.isfile(f'{AppName}.db'): + with open(f'{AppName}.db', 'r') as Db: + if GlobalId in Db.read().splitlines(): + continue + LocalId = GlobalId.split('/')[-1] Username = Entry.find('a', class_='status__display-name').get_text().strip(StripWS) Content = Entry.find('div', class_='e-content') @@ -79,16 +60,16 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo StatusPrepend = ' replied' Title = Content.get_text().strip(StripWS) Title = f"{Usertag}{StatusPrepend}: {Title[:32]}..." - for Emoji in Entry.find_all('img', class_='custom-emoji'): + for Emoji in Entry.find_all('img', class_='custom-emoji'): # Custom emojis in text Emoji['style'] = 'max-height:1em;' - Entry.find('img', class_='u-photo account__avatar')['style'] = 'max-height:2em;'#'display:none; visibility:hidden;' + Entry.find('img', class_='u-photo account__avatar')['style'] = 'max-height:4em;' # Profile pics Entry.find('div', class_='status__action-bar').replace_with('') print(f"-> {LocalId} - {Title}") HTML = f"""\

{Title}

-
+
{Entry} {{ Replace:Attached }} @@ -116,13 +97,14 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo elif e.endswith('title='): Alt = "'".join(Attachment[i+1:-1]) if Href: - Response = urllib.request.urlopen(Href) + Response = urlopen(Request(Href, headers={'User-Agent':UserAgent})) Data = Response.read() Mime = Response.info().get_content_type() - Tag = 'img' if Mime.split('/')[0] == 'image' else Mime.split('/')[0] - Opening = f'<{Tag} alt="{Alt}" title="{Alt}"' if Tag == 'img' else f'<{Tag} controls' - Closing = '>' if Tag == 'img' else f">" - Attached += f"""{Opening} style="max-width:100%;max-height:100vh;" src="data:{Mime};base64,{base64.b64encode(Data).decode()}"{Closing}\n""" + if LocalSave: + Tag = 'img' if Mime.split('/')[0] == 'image' else Mime.split('/')[0] + Opening = f'<{Tag} alt="{Alt}" title="{Alt}"' if Tag == 'img' else f'<{Tag} controls' + Closing = '>' if Tag == 'img' else f">" + Attached += f"""{Opening} style="max-width:100%; max-height:100vh;" src="data:{Mime};base64,{base64.b64encode(Data).decode()}"{Closing}\n""" if SendMail: File = MIMEBase(Mime.split('/')[0], Mime.split('/')[1]) File.set_payload(Data) @@ -132,11 +114,6 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo f"attachment; filename={Href.split('/')[-1]}") Message.attach(File) - if os.path.isfile(f'{AppName}.db'): - with open(f'{AppName}.db', 'r') as Db: - if GlobalId in Db.read().splitlines(): - pass #continue - if SendMail: with smtplib.SMTP_SSL(MailServer, MailPort, context=ssl.create_default_context()) as Client: Client.login(MailUsername, MailPassword) @@ -152,14 +129,14 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo File.write(HTML.replace('{ Replace:Attached }', Attached)) with open(f'{AppName}.db', 'a') as Db: - pass #Db.write(GlobalId + '\n') + Db.write(GlobalId + '\n') except Exception: raise def HandleFeedList(List): for Feed in List: - print(f"[I] Handling item:\n{Feed}") + print(f"[I] Handling item ->\n: {Feed}") HandleFeed( URLs=SureList(Feed['URLs']), IncludeRetoots=Feed['IncludeRetoots'] if 'IncludeRetoots' in Feed else True,