From 44c56867ede1ee8f2605e317a273128d563949de Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Tue, 1 Oct 2024 14:20:28 +0100 Subject: [PATCH] Handle Mastodon 2FA (sort of) --- fediverse/fediverse.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/fediverse/fediverse.py b/fediverse/fediverse.py index 7c28f6dc..768e96ea 100644 --- a/fediverse/fediverse.py +++ b/fediverse/fediverse.py @@ -40,9 +40,11 @@ def post_on_mastodon(settings, new_posts): mt_username = settings.get('MASTODON_USERNAME', '') global mt_password mt_password = settings.get('MASTODON_PASSWORD', '') + global mt_code + mt_code = settings.get('MASTODON_CODE', '') # check if config file has been duly filled or print an error message and exit - if mt_base_url == '' or mt_username == '' or mt_password == '': + if mt_base_url == '' or ((mt_username == '' or mt_password == '') and mt_code == ''): logger.warning('Pelican_fediverse: Mastodon access credentials not configured...') sys.exit(9) @@ -78,15 +80,22 @@ def post_updates(generator, writer): # we only write the newly found sites to disk if posting them worked. that way we can retry later if new_posts: if post_on_mastodon(generator.settings, new_posts): - mastodon = Mastodon( - client_id = 'pelicanfediverse_clientcred.secret', - api_base_url = mt_base_url - ) - mastodon.log_in( - mt_username, - mt_password, - to_file = 'pelicanfediverse_usercred.secret' - ) + if not os.path.exists('pelicanfediverse_usercred.secret'): + mastodon = Mastodon( + client_id = 'pelicanfediverse_clientcred.secret', + api_base_url = mt_base_url + ) + if mt_code: + mastodon.log_in( + code=mt_code, + to_file = 'pelicanfediverse_usercred.secret' + ) + else: + mastodon.log_in( + mt_username, + mt_password, + to_file = 'pelicanfediverse_usercred.secret' + ) mastodon = Mastodon( access_token = 'pelicanfediverse_usercred.secret', api_base_url = mt_base_url -- 2.30.2