From af5a373bcd4502709656e3f0a9daabd7bb17270d Mon Sep 17 00:00:00 2001 From: spla Date: Fri, 15 Oct 2021 14:02:11 +0200 Subject: [PATCH] Optimized code (-22 lines) and removed unneeded table feeds --- db-setup.py | 3 --- mastotuit.py | 60 +++++++++++++++------------------------------------- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/db-setup.py b/db-setup.py index 79d133a..ff03421 100644 --- a/db-setup.py +++ b/db-setup.py @@ -135,9 +135,6 @@ print("Creating table...") db = feeds_db db_user = feeds_db_user -table = "feeds" -sql = "create table "+table+" (link varchar(300) PRIMARY KEY)" -create_table(db, db_user, table, sql) table = "id" sql = "create table "+table+" (toot_id bigint PRIMARY KEY, tweet_id bigint)" diff --git a/mastotuit.py b/mastotuit.py index 73f0da9..98e8cce 100644 --- a/mastotuit.py +++ b/mastotuit.py @@ -311,54 +311,32 @@ if __name__ == '__main__': link = entry['link'] toot_id = link.rsplit('/')[4] - reply_id = mastodon.status(toot_id).in_reply_to_id - if reply_id != None: - is_reply = True - tweet_id = get_tweet_id(reply_id) + tweet_id = get_tweet_id(toot_id) - if len(entry.links) >= 2: + if tweet_id == 0: - with_images = True - images_list = [] - images = len(entry.links) - 1 + publish = True - i = 0 - while i < images: + reply_id = mastodon.status(toot_id).in_reply_to_id + if reply_id != None: - image_url = entry.links[i+1].href - image_filename = write_image(image_url) - images_list.append(image_filename) - i += 1 + is_reply = True + tweet_id = get_tweet_id(reply_id) - ################################################################### - # check database if feed is already published + if len(entry.links) >= 2: - try: + with_images = True + images_list = [] + images = len(entry.links) - 1 - conn = None + i = 0 + while i < images: - conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") - - cur = conn.cursor() - - cur.execute('select link from feeds where link=(%s)', (link,)) - - row = cur.fetchone() - if row == None: - publish = True - - cur.close() - - except (Exception, psycopg2.DatabaseError) as error: - - print(error) - - finally: - - if conn is not None: - - conn.close() + image_url = entry.links[i+1].href + image_filename = write_image(image_url) + images_list.append(image_filename) + i += 1 ########################################################### @@ -377,8 +355,6 @@ if __name__ == '__main__': ######################################################### - sql_insert_link = 'INSERT INTO feeds(link) VALUES (%s)' - sql_insert_ids = 'INSERT INTO id(toot_id, tweet_id) VALUES (%s,%s)' conn = None @@ -389,8 +365,6 @@ if __name__ == '__main__': cur = conn.cursor() - cur.execute(sql_insert_link, (link,)) - cur.execute(sql_insert_ids, (toot_id, tweet.id)) conn.commit()