Debugging profile update

This commit is contained in:
jeancf 2023-06-14 22:24:51 +02:00
parent 5be406f765
commit 69161503e0

View File

@ -170,21 +170,23 @@ def build_config(args):
terminate(-1)
def update_profile(soup, db, mast_password):
def update_profile(soup, sql, mast_password):
"""
Update the profile on Mastodon
:param soup: BeautifulSoup object containing the page
:param db: database object
:param sql: database connection
:param mast_password: <PASSWORD>
:return: mastodon object we had to login to update, None otherwise
"""
# TODO Check if TOML option to update profile is set
db = sql.cursor()
# Extract avatar picture address
new_avatar_url = 'https://' + unquote(soup.find('div', class_='profile-card-info').findChild('a').findChild('img').get('src').removeprefix('/pic/'))
# Extract banner picture address
new_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/')) + ".jpg"
# 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'],))
@ -203,6 +205,10 @@ def update_profile(soup, db, mast_password):
new_banner_url = None
else:
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'], "", ""))
sql.commit()
mastodon = None
if new_avatar_url is not None or new_banner_url is not None:
@ -210,13 +216,16 @@ def update_profile(soup, db, mast_password):
mastodon = login(mast_password)
# Update profile on Mastodon
try:
mastodon.account_update_credentials(avatar=new_avatar_url, header=new_banner_url)
mastodon.account_update_credentials(avatar=new_avatar_url, avatar_mime_type='image/jpg', header=new_banner_url, header_mime_type='image/jpg')
except Exception as e:
logging.error("Could not update profile")
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']))
sql.commit()
# TODO update database
exit(5)
return mastodon
def deredir_url(url):
@ -574,9 +583,9 @@ def login(password):
terminate(-1)
if os.path.isfile(TOML['config']['mastodon_user'] + '.secret'):
logging.warning('You successfully logged in using a password and an access token \
has been saved. The password can therefore be omitted from the \
command-line in future invocations')
logging.warning('''You successfully logged in using a password and an access token
has been saved. The password can therefore be omitted from the
command-line in future invocations''')
else: # No password provided, login with token
# Using token in existing .secret file
if os.path.isfile(TOML['config']['mastodon_user'] + '.secret'):
@ -978,7 +987,7 @@ def main(argv):
mastodon = None
# Update profile if it has changed
mastodon = update_profile(soup, db, mast_password)
mastodon = update_profile(soup, sql, mast_password)
# Login to account on maston instance
if len(tweets) != 0 and mastodon is None: