From c65a4c1fc8913ed7846ef4038a76ec202b6c88b3 Mon Sep 17 00:00:00 2001 From: spla Date: Tue, 20 Oct 2020 12:54:34 +0200 Subject: [PATCH] Fixed indents --- db-setup.py | 109 ++++++++++++++++++++++----------------------- mastofeeds.py | 119 ++++++++++++++++++++++++++------------------------ 2 files changed, 116 insertions(+), 112 deletions(-) diff --git a/db-setup.py b/db-setup.py index d0e7cb1..cf0e542 100644 --- a/db-setup.py +++ b/db-setup.py @@ -66,84 +66,85 @@ def create_table(db, db_user, table, sql): conn.close() -############################################################################################# +############################################################################### +# main -# Load configuration from config file -config_filepath = "db_config.txt" -feeds_db = get_parameter("feeds_db", config_filepath) -feeds_db_user = get_parameter("feeds_db_user", config_filepath) -feeds_url = get_parameter("feeds_url", config_filepath) +if __name__ == '__main__': -############################################################ -# create database -############################################################ + # Load configuration from config file + config_filepath = "db_config.txt" + feeds_db = get_parameter("feeds_db", config_filepath) + feeds_db_user = get_parameter("feeds_db_user", config_filepath) + feeds_url = get_parameter("feeds_url", config_filepath) -conn = None + ############################################################ + # create database + ############################################################ -try: + conn = None - conn = psycopg2.connect(dbname='postgres', - user=feeds_db_user, host='', - password='') + try: - conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) + conn = psycopg2.connect(dbname='postgres', user=feeds_db_user, host='', password='') - cur = conn.cursor() + conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) - print("Creating database " + feeds_db + ". Please wait...") + cur = conn.cursor() - cur.execute(sql.SQL("CREATE DATABASE {}").format( - sql.Identifier(feeds_db)) - ) - print("Database " + feeds_db + " created!") + print("Creating database " + feeds_db + ". Please wait...") -except (Exception, psycopg2.DatabaseError) as error: + cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(feeds_db))) - print(error) + print("Database " + feeds_db + " created!") -finally: + except (Exception, psycopg2.DatabaseError) as error: - if conn is not None: + print(error) - conn.close() + finally: -############################################################################################# + if conn is not None: -try: + conn.close() - conn = None - conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") + ############################################################################################# -except (Exception, psycopg2.DatabaseError) as error: + try: - print(error) - # Load configuration from config file - os.remove("db_config.txt") - print("Exiting. Run setup again with right parameters") - sys.exit(0) + conn = None + conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") -if conn is not None: + except (Exception, psycopg2.DatabaseError) as error: - print("\n") - print("Host parameters saved to config.txt!") - print("\n") + print(error) -############################################################ -# Create needed tables -############################################################ + # Load configuration from config file + os.remove("db_config.txt") + print("Exiting. Run setup again with right parameters") + sys.exit(0) -print("Creating table...") + if conn is not None: -######################################## + print("\n") + print("Host parameters saved to config.txt!") + print("\n") -db = feeds_db -db_user = feeds_db_user -table = "feeds" -sql = "create table "+table+" (id varchar(200) PRIMARY KEY, link varchar(250))" -create_table(db, db_user, table, sql) + ############################################################ + # Create needed tables + ############################################################ -##################################### + print("Creating table...") -print("Done!") -print("Now you can run setup.py!") -print("\n") + ######################################## + + db = feeds_db + db_user = feeds_db_user + table = "feeds" + sql = "create table "+table+" (id varchar(200) PRIMARY KEY, link varchar(250))" + create_table(db, db_user, table, sql) + + ##################################### + + print("Done!") + print("Now you can run setup.py!") + print("\n") diff --git a/mastofeeds.py b/mastofeeds.py index 15fc0ee..8ef907b 100644 --- a/mastofeeds.py +++ b/mastofeeds.py @@ -52,76 +52,39 @@ mastodon = Mastodon( # Initialise access headers headers={ 'Authorization': 'Bearer %s'%uc_access_token } -######################################################## +############################################################################### +# main -publish = 0 +if __name__ == '__main__': -newsfeeds = feedparser.parse(feeds_url) + publish = 0 -for entry in newsfeeds.entries: + newsfeeds = feedparser.parse(feeds_url) - title = entry['title'] - id = entry['id'] - link = entry['link'] + for entry in newsfeeds.entries: - ################################################################### - # check database if feed is already published - ################################################################### + title = entry['title'] + id = entry['id'] + link = entry['link'] - try: - - conn = None - conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") - - cur = conn.cursor() - - cur.execute('select id from feeds where id=(%s)', (id,)) - - row = cur.fetchone() - if row == None: - publish = 1 - else: - publish = 0 - - cur.close() - - except (Exception, psycopg2.DatabaseError) as error: - - print(error) - - finally: - - if conn is not None: - - conn.close() - - ########################################################### - - if publish == 1: - - toot_text = str(title)+'\n' - toot_text += str(link) - - print("Tooting...") - print(toot_text) - - mastodon.status_post(toot_text, in_reply_to_id=None,) - - ######################################################### - - insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)' - - conn = None + ################################################################### + # check database if feed is already published + ################################################################### try: + conn = None conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") cur = conn.cursor() - cur.execute(insert_line, (id, link,)) + cur.execute('select id from feeds where id=(%s)', (id,)) - conn.commit() + row = cur.fetchone() + if row == None: + publish = 1 + else: + publish = 0 cur.close() @@ -134,6 +97,46 @@ for entry in newsfeeds.entries: if conn is not None: conn.close() - else: - print("Any new feeds") + ########################################################### + + if publish == 1: + + toot_text = str(title)+'\n' + toot_text += str(link) + + print("Tooting...") + print(toot_text) + + mastodon.status_post(toot_text, in_reply_to_id=None,) + + ######################################################### + + insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)' + + conn = None + + try: + + conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + cur.execute(insert_line, (id, link,)) + + conn.commit() + + cur.close() + + except (Exception, psycopg2.DatabaseError) as error: + + print(error) + + finally: + + if conn is not None: + + conn.close() + else: + + print("Any new feeds")