From acb3f81d31208197b200215364fbc77943de534e Mon Sep 17 00:00:00 2001 From: spla Date: Sat, 9 Oct 2021 13:30:18 +0200 Subject: [PATCH] Code optimization --- mastotuit.py | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/mastotuit.py b/mastotuit.py index 2a5e41a..5160843 100644 --- a/mastotuit.py +++ b/mastotuit.py @@ -231,7 +231,7 @@ if __name__ == '__main__': try: conn = None - + conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") cur = conn.cursor() @@ -359,7 +359,9 @@ if __name__ == '__main__': ######################################################### - insert_line = 'INSERT INTO feeds(link) VALUES (%s)' + sql_insert_link = 'INSERT INTO feeds(link) VALUES (%s)' + + sql_insert_ids = 'INSERT INTO id(toot_id, tweet_id) VALUES (%s,%s)' conn = None @@ -369,33 +371,9 @@ if __name__ == '__main__': cur = conn.cursor() - cur.execute(insert_line, (link,)) + cur.execute(sql_insert_link, (link,)) - conn.commit() - - cur.close() - - except (Exception, psycopg2.DatabaseError) as error: - - print(error) - - finally: - - if conn is not None: - - conn.close() - - insert_line = 'INSERT INTO id(toot_id, tweet_id) 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, (toot_id, tweet.id)) + cur.execute(sql_insert_ids, (toot_id, tweet.id)) conn.commit()