First debug free working version

This commit is contained in:
robin 2022-05-15 10:33:40 +02:00
parent 3f7283ef86
commit 4d9a749b03
1 changed files with 85 additions and 2 deletions

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()