diff --git a/twoot.py b/twoot.py index 6762665..48c9403 100755 --- a/twoot.py +++ b/twoot.py @@ -100,7 +100,7 @@ def build_config(args): # Load config file if it was provided toml_file = args['f'] if toml_file is not None: - try: # Included starting with python 3.11 + try: # Included in python from version 3.11 import tomllib except ModuleNotFoundError: # for python < 3.11, tomli module must be installed @@ -469,46 +469,66 @@ def is_time_valid(timestamp): def login(password): - - instance = TOML['config']['mastodon_instance'] - logging.info('Logging in to ' + instance + ' as ' + TOML['config']['twitter_account']) - # Create Mastodon application if it does not exist yet - if not os.path.isfile(instance + '.secret'): + """ + Login to Mastodon account and return mastodon object used to post content + :param password: Password associated to account. None if not provided + :return: mastodon object + """ +# Create Mastodon application if it does not exist yet + if not os.path.isfile(TOML['config']['mastodon_instance'] + '.secret'): try: Mastodon.create_app( - 'twoot', - api_base_url='https://' + instance, - to_file=instance + '.secret' + 'feedtoot', + api_base_url='https://' + TOML['config']['mastodon_instance'], + to_file=TOML['config']['mastodon_instance'] + '.secret' ) except MastodonError as me: - logging.fatal('failed to create app on ' + instance) + logging.fatal('failed to create app on ' + TOML['config']['mastodon_instance']) logging.fatal(me) - sys.exit(-1) + exit(-1) - # Log in to Mastodon instance - try: - mastodon = Mastodon( - client_id=instance + '.secret', - api_base_url='https://' + instance - ) + mastodon = None - mastodon.log_in( - username=TOML['config']['mastodon_user'], - password=password, - to_file=TOML['config']['mastodon_user'] + ".secret" - ) + # Log in to Mastodon instance with password + if password is not None: + try: + mastodon = Mastodon( + client_id=TOML['config']['mastodon_instance'] + '.secret', + api_base_url='https://' + TOML['config']['mastodon_instance'] + ) - except MastodonError as me: - logging.fatal('ERROR: Login to ' + instance + ' Failed') - logging.fatal(me) - sys.exit(-1) + mastodon.log_in( + username=TOML['config']['mastodon_user'], + password=password, + to_file=TOML['config']['mastodon_user'] + ".secret" + ) + logging.info('Logging in to ' + TOML['config']['mastodon_instance']) - # Check ratelimit status - logging.debug('Ratelimit allowed requests: ' + str(mastodon.ratelimit_limit)) - logging.debug('Ratelimit remaining requests: ' + str(mastodon.ratelimit_remaining)) - logging.debug('Ratelimit reset time: ' + time.asctime(time.localtime(mastodon.ratelimit_reset))) - logging.debug('Ratelimit last call: ' + time.asctime(time.localtime(mastodon.ratelimit_lastcall))) + except MastodonError as me: + logging.fatal('Login to ' + TOML['config']['mastodon_instance'] + ' Failed\n') + logging.fatal(me) + exit(-1) + + if os.path.isfile(TOML['config']['mastodon_user'] + '.secret'): + logging.warning("""You successfully logged in using a password. An access token + has been saved therefore the password can 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'): + try: + mastodon = Mastodon( + access_token=TOML['config']['mastodon_user'] + '.secret', + api_base_url='https://' + TOML['config']['mastodon_instance'] + ) + except MastodonError as me: + logging.fatal('Login to ' + TOML['config']['mastodon_instance'] + ' Failed\n') + logging.fatal(me) + exit(-1) + else: + logging.fatal('No secret file found. Password required to log in') + exit(-1) return mastodon