mastofeeds/mastofeeds.py

140 líneas
3,6 KiB
Python
Original Vista normal Històric

2020-03-21 20:13:19 +01:00
import os
import feedparser
from mastodon import Mastodon
import psycopg2
import sys
###############################################################################
# INITIALISATION
###############################################################################
# Returns the parameter from the specified file
def get_parameter( parameter, file_path ):
# Check if secrets file exists
if not os.path.isfile(file_path):
print("File %s not found, exiting."%file_path)
sys.exit(0)
# Find parameter in file
with open( file_path ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
print(file_path + " Missing parameter %s "%parameter)
sys.exit(0)
# Load secrets from secrets file
secrets_filepath = "secrets/secrets.txt"
uc_client_id = get_parameter("uc_client_id", secrets_filepath)
uc_client_secret = get_parameter("uc_client_secret", secrets_filepath)
uc_access_token = get_parameter("uc_access_token", secrets_filepath)
# Load configuration from config file
db_config_filepath = "db_config.txt"
feeds_db = get_parameter("feeds_db", db_config_filepath)
feeds_db_user = get_parameter("feeds_db_user", db_config_filepath)
feeds_url = get_parameter("feeds_url", db_config_filepath)
# Load configuration from config file
config_filepath = "config.txt"
mastodon_hostname = get_parameter("mastodon_hostname", config_filepath) # E.g., mastodon.social
# Initialise Mastodon API
mastodon = Mastodon(
client_id = uc_client_id,
client_secret = uc_client_secret,
access_token = uc_access_token,
api_base_url = 'https://' + mastodon_hostname,
)
# Initialise access headers
headers={ 'Authorization': 'Bearer %s'%uc_access_token }
########################################################
publish = 0
newsfeeds = feedparser.parse(feeds_url)
for entry in newsfeeds.entries:
title = entry['title']
id = entry['id']
link = entry['link']
###################################################################
2020-03-21 20:27:33 +01:00
# check database if feed is already published
2020-03-21 20:13:19 +01:00
###################################################################
try:
2020-03-21 20:26:16 +01:00
conn = None
conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432")
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
cur = conn.cursor()
2020-03-21 20:13:19 +01:00
2020-10-20 12:41:24 +02:00
cur.execute('select id from feeds where id=(%s)', (id,))
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
row = cur.fetchone()
if row == None:
publish = 1
else:
publish = 0
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
cur.close()
2020-03-21 20:13:19 +01:00
except (Exception, psycopg2.DatabaseError) as error:
2020-03-21 20:26:16 +01:00
print(error)
2020-03-21 20:13:19 +01:00
finally:
2020-03-21 20:26:16 +01:00
if conn is not None:
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
conn.close()
2020-03-21 20:13:19 +01:00
###########################################################
if publish == 1:
2020-03-21 20:26:16 +01:00
toot_text = str(title)+'\n'
toot_text += str(link)
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
print("Tooting...")
print(toot_text)
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
mastodon.status_post(toot_text, in_reply_to_id=None,)
2020-03-21 20:13:19 +01:00
#########################################################
2020-10-20 12:41:24 +02:00
insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)'
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
conn = None
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
try:
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432")
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
cur = conn.cursor()
2020-03-21 20:13:19 +01:00
2020-10-20 12:41:24 +02:00
cur.execute(insert_line, (id, link,))
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
conn.commit()
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
cur.close()
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
except (Exception, psycopg2.DatabaseError) as error:
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
print(error)
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
finally:
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
if conn is not None:
2020-03-21 20:13:19 +01:00
2020-03-21 20:26:16 +01:00
conn.close()
2020-03-21 20:13:19 +01:00
else:
2020-03-21 20:26:16 +01:00
print("Any new feeds")