Compare commits

...

5 Commits

Author SHA1 Message Date
robin 4d9a749b03 First debug free working version 2022-05-15 10:33:40 +02:00
robin 3f7283ef86 add some comments 2022-05-15 10:28:27 +02:00
robin 2e5f2777b3 rough readme 2022-05-15 10:27:39 +02:00
robin 8f1e33d111 add secret store 2022-05-15 10:19:26 +02:00
robin 305f6b3543 add log directory 2022-05-15 10:06:33 +02:00
5 changed files with 106 additions and 2 deletions

View File

@ -0,0 +1,5 @@
apt install python3-mastodon
apt install python3-feedparser
cp settings.py.sample settings.py
Edit settings.py for your setup
python3 main.py

0
data/.ignore Normal file
View File

0
log/.ignore Normal file
View File

87
main.py
View File

@ -1,5 +1,88 @@
def main():
print("hi")
import feedparser
from settings import *
from mastodon import Mastodon
from os.path import exists
from datetime import timedelta, datetime
def main():
# print("Start ...")
if not exists(app_path):
Mastodon.create_app(
bot_name,
api_base_url = api_url,
to_file = app_path
)
if not exists(user_path):
mastodon = Mastodon(
client_id = app_path,
api_base_url = api_url
)
mastodon.log_in(
account_name,
account_password,
to_file = user_path
)
api = Mastodon(
access_token = user_path,
api_base_url = api_url
)
for j, url in enumerate(feeds):
save = False
feed = feedparser.parse(url)
# print(feed['feed']['title'])
datafile = f"{log_path}sent_{j}.data"
if not exists(datafile):
data = open(datafile, 'w')
data.close()
send_data = []
with open(datafile,'r') as f:
for line in f:
send_data.append(line.strip())
f.close()
reply_id = None
visibility = 'public'
for item in feed['items']:
title = item['title'].encode('latin-1', 'ignore').decode('latin-1')
line = f"{title}{item['link']}"
try:
pubdate = datetime.strptime(item['published'],'%a, %d %b %Y %H:%M:%S %z')
except ValueError:
try:
pubdate = datetime.fromisoformat(item['published'])
except ValueError:
pubdate = datetime.now(pubdate.tzinfo) # Fix later pubdate is not defined
if pubdate > datetime.now(pubdate.tzinfo) - timedelta(days=5):
if not line in send_data:
result = api.status_post(
(title + "\n\n" + item['link']),
reply_id, #in_reply_to_id
None, #media_ids
False, #sensitive
visibility, #visibility ('direct', 'private', 'unlisted', 'public')
None, #spoiler_text
None, #language
line, #idempotency_key
None, #content_type
None, #scheduled_at
None, #poll
None, #quote_id
)
if result.id:
if not reply_id:
reply_id = result.id
visibility = 'unlisted'
send_data.append(line)
save = True
if save:
save = False
while len(send_data) > 1000:
send_data.pop(0)
with open(datafile,'w+') as f:
for lines in send_data:
f.write('%s\n' %lines)
f.close()
if __name__ == "__main__":
main()

16
settings.py.sample Normal file
View File

@ -0,0 +1,16 @@
# Change required
api_url = 'https://example.social' # Mastodon Server URL
bot_name = 'localfeedbot' # Botname
account_name = 'feedbot' # Mastodon-Account name
account_password = 'supersecret' # Mastodon-Account password
feeds = [
'https://rss.golem.de/rss.php?feed=RSS2.0',
'https://www.derstandard.at/rss/web',
'https://www.heise.de/rss/heise.rdf',
'https://www.zdf.de/rss/zdf/nachrichten'
]
# Make sure to change the setup when you edit the following settings
log_path = "./log/" # Stores feed info
data_path = "./data/" # Make sure this directory is not accessible for other users or per web
app_path = f"{data_path}.htapp.data" # Store app secret
user_path = f"{data_path}/.htuser.data" # Store user secret