From d021b20d813ab387dcbb9f2d06a976b04b3a9afa Mon Sep 17 00:00:00 2001 From: jeancf Date: Wed, 14 Jun 2023 16:22:28 +0200 Subject: [PATCH] profile_has_changed() done --- twoot.py | 52 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/twoot.py b/twoot.py index 4991e28..771ed25 100755 --- a/twoot.py +++ b/twoot.py @@ -169,16 +169,43 @@ def build_config(args): print('CRITICAL: Missing Mastodon user') terminate(-1) -def update_profile(TOML, soup, db, mast_password): +def profile_has_changed(soup, db): + """ + Check if the profile has changed since the last time we checked + :param soup: BeautifulSoup object containing the page + :param db: database object + :return: None if no change, tuple of new avatar and banner URLs if changed + """ # Extract avatar picture address - avatar_url = 'https://' + unquote(soup.find('div', class_='profile-card-info').findChild('a').findChild('img').get('src').removeprefix('/pic/')) + new_avatar_url = 'https://' + unquote(soup.find('div', class_='profile-card-info').findChild('a').findChild('img').get('src').removeprefix('/pic/')) # Extract banner picture address - banner_url = unquote(soup.find('div', class_='profile-banner').findChild('a').findChild('img').get('src').removeprefix('/pic/')) + new_banner_url = unquote(soup.find('div', class_='profile-banner').findChild('a').findChild('img').get('src').removeprefix('/pic/')) - mastodon_account = TOML['config']['mastodon_user'] + db.execute("SELECT avatar_url, banner_url FROM profiles WHERE mastodon_account=?", (TOML['config']['mastodon_user'],)) + profile_in_db = db.fetchone() + if profile_in_db is not None: + cur_avatar_url = profile_in_db[0] + cur_banner_url = profile_in_db[1] + # Check if urls have changed + if new_avatar_url!= cur_avatar_url or new_banner_url!= cur_banner_url: + return new_avatar_url, new_banner_url + else: + return None - exit() + # If profile was not found in db, return urls + return new_avatar_url, new_banner_url + +def update_profile(mastodon, avatar_url, banner_url, db): + """ + Update the profile of the Mastodon account + :param mastodon: Mastodon object + :param avatar_url: URL of the new avatar picture + :param banner_url: URL of the new banner picture + :param db: database object + :return: + """ + pass def deredir_url(url): """ @@ -767,10 +794,6 @@ def main(argv): # Make soup soup = BeautifulSoup(twit_account_page.text, 'html.parser') - # Update profile if it has changed - # TODO Make conditional to TOML option - update_profile(TOML, soup, db, mast_password) - # Extract twitter timeline timeline = soup.find_all('div', class_='timeline-item') @@ -939,9 +962,16 @@ def main(argv): logging.info(str(out_date_cnt) + ' tweets outside of valid time range') logging.info(str(in_db_cnt) + ' tweets already in database') - # Login to account on maston instance + # Initialise Mastodon object mastodon = None - if len(tweets) != 0: + + # Update profile if it has changed + if profile_has_changed(soup, db) is not None: + mastodon = login(mast_password) + update_profile() + + # Login to account on maston instance + if len(tweets) != 0 and mastodon is None: mastodon = login(mast_password) # **********************************************************