Put MastodonFeedHTML in own dir, add Config file

This commit is contained in:
octospacc 2022-08-16 13:23:56 +02:00
parent cdbc07120a
commit 8348704406
2 changed files with 22 additions and 43 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.pyc
Config.py

View File

@ -3,38 +3,14 @@ import base64
import feedparser import feedparser
import os import os
import time import time
import urllib.request
import email, smtplib, ssl import email, smtplib, ssl
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from email import encoders from email import encoders
from email.mime.base import MIMEBase 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 Config import * from urllib.request import urlopen, Request
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"
StripWS = '\t\r\n' StripWS = '\t\r\n'
@ -53,7 +29,7 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo
URL = URL.rstrip('/').rstrip('/with_replies') + '/with_replies' URL = URL.rstrip('/').rstrip('/with_replies') + '/with_replies'
Usertag = f"{URL.split('/')[-2]}@{URL.split('/')[-3]}" Usertag = f"{URL.split('/')[-2]}@{URL.split('/')[-3]}"
try: try:
Response = urllib.request.urlopen(URL) Response = urlopen(Request(URL, headers={'User-Agent':UserAgent}))
Data = Response.read() Data = Response.read()
Soup = BeautifulSoup(Data, 'html.parser') Soup = BeautifulSoup(Data, 'html.parser')
Feed = Soup.find_all('div', class_='entry') Feed = Soup.find_all('div', class_='entry')
@ -61,12 +37,17 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo
for Entry in Feed: for Entry in Feed:
Attached = '' Attached = ''
print(Entry)
GlobalId = Entry.find('a', class_='u-url') GlobalId = Entry.find('a', class_='u-url')
if GlobalId: if GlobalId:
GlobalId = GlobalId['href'].lstrip('https://').lstrip('http://') GlobalId = GlobalId['href'].lstrip('https://').lstrip('http://')
else: else:
continue 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] 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(StripWS)
Content = Entry.find('div', class_='e-content') Content = Entry.find('div', class_='e-content')
@ -79,16 +60,16 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo
StatusPrepend = ' replied' StatusPrepend = ' replied'
Title = Content.get_text().strip(StripWS) Title = Content.get_text().strip(StripWS)
Title = f"{Usertag}{StatusPrepend}: {Title[:32]}..." 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;' 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('') Entry.find('div', class_='status__action-bar').replace_with('')
print(f"-> {LocalId} - {Title}") print(f"-> {LocalId} - {Title}")
HTML = f"""\ HTML = f"""\
<h1>{Title}</h1> <h1>{Title}</h1>
<div> <div class="AppName-content" style="word-wrap:break-word;">
{Entry} {Entry}
{{ Replace:Attached }} {{ Replace:Attached }}
@ -116,13 +97,14 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo
elif e.endswith('title='): elif e.endswith('title='):
Alt = "'".join(Attachment[i+1:-1]) Alt = "'".join(Attachment[i+1:-1])
if Href: if Href:
Response = urllib.request.urlopen(Href) Response = urlopen(Request(Href, headers={'User-Agent':UserAgent}))
Data = Response.read() Data = Response.read()
Mime = Response.info().get_content_type() Mime = Response.info().get_content_type()
if LocalSave:
Tag = 'img' if Mime.split('/')[0] == 'image' else Mime.split('/')[0] 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' Opening = f'<{Tag} alt="{Alt}" title="{Alt}"' if Tag == 'img' else f'<{Tag} controls'
Closing = '>' if Tag == 'img' else f"></{Tag}>" Closing = '>' if Tag == 'img' else f"></{Tag}>"
Attached += f"""{Opening} style="max-width:100%;max-height:100vh;" src="data:{Mime};base64,{base64.b64encode(Data).decode()}"{Closing}\n""" Attached += f"""{Opening} style="max-width:100%; max-height:100vh;" src="data:{Mime};base64,{base64.b64encode(Data).decode()}"{Closing}\n"""
if SendMail: if SendMail:
File = MIMEBase(Mime.split('/')[0], Mime.split('/')[1]) File = MIMEBase(Mime.split('/')[0], Mime.split('/')[1])
File.set_payload(Data) File.set_payload(Data)
@ -132,11 +114,6 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo
f"attachment; filename={Href.split('/')[-1]}") f"attachment; filename={Href.split('/')[-1]}")
Message.attach(File) 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: if SendMail:
with smtplib.SMTP_SSL(MailServer, MailPort, context=ssl.create_default_context()) as Client: with smtplib.SMTP_SSL(MailServer, MailPort, context=ssl.create_default_context()) as Client:
Client.login(MailUsername, MailPassword) Client.login(MailUsername, MailPassword)
@ -152,14 +129,14 @@ def HandleFeed(URLs, IncludeRetoots, IncludeReplies, LocalSave, SendMail, MailTo
File.write(HTML.replace('{ Replace:Attached }', Attached)) File.write(HTML.replace('{ Replace:Attached }', Attached))
with open(f'{AppName}.db', 'a') as Db: with open(f'{AppName}.db', 'a') as Db:
pass #Db.write(GlobalId + '\n') Db.write(GlobalId + '\n')
except Exception: except Exception:
raise raise
def HandleFeedList(List): def HandleFeedList(List):
for Feed in List: for Feed in List:
print(f"[I] Handling item:\n{Feed}") print(f"[I] Handling item ->\n: {Feed}")
HandleFeed( HandleFeed(
URLs=SureList(Feed['URLs']), URLs=SureList(Feed['URLs']),
IncludeRetoots=Feed['IncludeRetoots'] if 'IncludeRetoots' in Feed else True, IncludeRetoots=Feed['IncludeRetoots'] if 'IncludeRetoots' in Feed else True,