From c30618719676a689c775f1e7375dc2e4fb360568 Mon Sep 17 00:00:00 2001 From: jeancf Date: Thu, 15 Jun 2023 14:49:30 +0200 Subject: [PATCH] Refined db structure --- twoot.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/twoot.py b/twoot.py index e7b2976..79b2de8 100755 --- a/twoot.py +++ b/twoot.py @@ -158,19 +158,19 @@ def build_config(args): TOML['options']['tweet_delay'] = float(args['d']) if args['c'] is not None: TOML['options']['toot_cap'] = int(args['c']) - if args['p'] is True: - TOML['options']['update_profile'] = args['p'] + if args['q'] is True: + TOML['options']['update_profile'] = args['q'] # Verify that we have a minimum config to run if 'twitter_account' not in TOML['config'].keys() or TOML['config']['twitter_account'] == "": print('CRITICAL: Missing Twitter account') - terminate(-1) + exit(-1) if 'mastodon_instance' not in TOML['config'].keys() or TOML['config']['mastodon_instance'] == "": print('CRITICAL: Missing Mastodon instance') - terminate(-1) + exit(-1) if 'mastodon_user' not in TOML['config'].keys() or TOML['config']['mastodon_user'] == "": print('CRITICAL: Missing Mastodon user') - terminate(-1) + exit(-1) def update_profile(nitter_url, soup, sql, mast_password): @@ -196,7 +196,7 @@ def update_profile(nitter_url, soup, sql, mast_password): new_banner_url = soup.find('div', class_='profile-banner').findChild('a').findChild('img').get('src') # Get the original urls of the avatar and banner pictures on the account profile - db.execute("SELECT avatar_url, banner_url FROM profiles WHERE mastodon_account=?", (TOML['config']['mastodon_user'],)) + db.execute("SELECT avatar_url, banner_url FROM profiles WHERE mastodon_instance=? AND mastodon_account=?", (TOML['config']['mastodon_instance'], TOML['config']['mastodon_user'],)) profile_in_db = db.fetchone() changed = False @@ -215,7 +215,7 @@ def update_profile(nitter_url, soup, sql, mast_password): logging.info('banner image changed on twitter profile') else: # Mastodon user not found in database. Add new record - db.execute("INSERT INTO profiles (mastodon_account, avatar_url, banner_url) VALUES (?, ?, ?)", (TOML['config']['mastodon_user'], "", "")) + db.execute("INSERT INTO profiles (mastodon_instance, mastodon_account, avatar_url, banner_url) VALUES (?, ?, ?, ?)", (TOML['config']['mastodon_instance'], TOML['config']['mastodon_user'], "", "")) sql.commit() changed = True logging.debug("added new profile to database") @@ -249,7 +249,7 @@ def update_profile(nitter_url, soup, sql, mast_password): logging.error(e) else: # Add urls to database - db.execute("UPDATE profiles SET avatar_url=?, banner_url=? WHERE mastodon_account=?", (new_avatar_url, new_banner_url, TOML['config']['mastodon_user'])) + db.execute("UPDATE profiles SET avatar_url=?, banner_url=? WHERE mastodon_instance=? AND mastodon_account=?", (new_avatar_url, new_banner_url, TOML['config']['mastodon_instance'], TOML['config']['mastodon_user'])) sql.commit() logging.debug("updated profile on database") @@ -712,7 +712,7 @@ def main(argv): parser.add_argument('-u', action='store_true', help='Remove trackers from URLs') parser.add_argument('-v', action='store_true', help='Ingest twitter videos and upload to Mastodon instance') parser.add_argument('-o', action='store_true', help='Do not add reference to Original tweet') - parser.add_argument('-p', action='store_true', help='update profile if changed') + parser.add_argument('-q', action='store_true', help='update profile if changed') parser.add_argument('-a', metavar='', action='store', type=float) parser.add_argument('-d', metavar='', action='store', type=float) parser.add_argument('-c', metavar='', action='store', type=int) @@ -785,8 +785,8 @@ def main(argv): mastodon_account TEXT, tweet_id TEXT, toot_id TEXT)''') db.execute('''CREATE INDEX IF NOT EXISTS main_index ON toots (twitter_account, mastodon_instance, mastodon_account, tweet_id)''') - db.execute('''CREATE TABLE IF NOT EXISTS profiles (mastodon_account TEXT, avatar_url TEXT, banner_url TEXT)''') - db.execute('''CREATE INDEX IF NOT EXIsTS profile_index ON profiles (mastodon_account)''') + db.execute('''CREATE TABLE IF NOT EXISTS profiles (mastodon_instance TEXT, mastodon_account TEXT, avatar_url TEXT, banner_url TEXT)''') + db.execute('''CREATE INDEX IF NOT EXIsTS profile_index ON profiles (mastodon_instance, mastodon_account)''') # Select random nitter instance to fetch updates from nitter_url = NITTER_URLS[random.randint(0, len(NITTER_URLS) - 1)]