mastofeeds/mastofeeds.py

68 líneas
1,1 KiB
Python
Original Vista normal Històric

2020-03-21 20:13:19 +01:00
import os
2023-05-06 15:05:41 +02:00
import sys
import time
from app.libraries.database import Database
from app.libraries.setup import Setup
2020-03-21 20:13:19 +01:00
import feedparser
from mastodon import Mastodon
import psycopg2
2020-10-20 12:54:34 +02:00
# main
2020-03-21 20:13:19 +01:00
2020-10-20 12:54:34 +02:00
if __name__ == '__main__':
2020-03-21 20:13:19 +01:00
2023-05-06 15:05:41 +02:00
db = Database()
2023-05-06 15:05:41 +02:00
setup = Setup()
mastodon = Mastodon(
2023-05-06 15:05:41 +02:00
access_token = setup.mastodon_app_token,
api_base_url= setup.mastodon_hostname
)
2023-05-06 15:05:41 +02:00
#publish = 0
2020-03-21 20:13:19 +01:00
2021-05-15 11:19:08 +02:00
try:
2023-05-06 15:05:41 +02:00
newsfeeds = feedparser.parse(db.feeds_url)
2021-05-15 11:19:08 +02:00
print(newsfeeds.status)
except:
print(newsfeeds.status)
sys.exit(0)
2020-03-21 20:13:19 +01:00
2020-10-20 12:54:34 +02:00
for entry in newsfeeds.entries:
2020-03-21 20:13:19 +01:00
2020-10-20 12:54:34 +02:00
title = entry['title']
id = entry['id']
link = entry['link']
2020-03-21 20:13:19 +01:00
2020-10-20 12:54:34 +02:00
# check database if feed is already published
2020-03-21 20:13:19 +01:00
2023-05-06 15:05:41 +02:00
publish = db.published(link)
2020-03-21 20:13:19 +01:00
2023-05-06 15:05:41 +02:00
if publish:
2020-03-21 20:13:19 +01:00
2020-10-20 12:54:34 +02:00
toot_text = str(title)+'\n'
2023-05-06 15:05:41 +02:00
2020-10-20 12:54:34 +02:00
toot_text += str(link)
2020-03-21 20:13:19 +01:00
2020-10-20 12:54:34 +02:00
print("Tooting...")
2023-05-06 15:05:41 +02:00
2020-10-20 12:54:34 +02:00
print(toot_text)
2020-03-21 20:13:19 +01:00
2020-10-20 12:54:34 +02:00
mastodon.status_post(toot_text, in_reply_to_id=None,)
2020-03-21 20:13:19 +01:00
2021-05-15 11:19:08 +02:00
time.sleep(2)
2023-05-06 15:05:41 +02:00
# write feed
2020-03-21 20:13:19 +01:00
2023-05-06 15:05:41 +02:00
db.write_feed(link)
2020-03-21 20:13:19 +01:00
2020-10-20 12:54:34 +02:00
else:
2020-03-21 20:13:19 +01:00
2020-10-20 12:54:34 +02:00
print("Any new feeds")
sys.exit(0)