Implemented new login()

This commit is contained in:
jeancf 2022-11-30 12:34:55 +01:00
parent f64645cf69
commit 8ca17bef58

View File

@ -100,7 +100,7 @@ def build_config(args):
# Load config file if it was provided # Load config file if it was provided
toml_file = args['f'] toml_file = args['f']
if toml_file is not None: if toml_file is not None:
try: # Included starting with python 3.11 try: # Included in python from version 3.11
import tomllib import tomllib
except ModuleNotFoundError: except ModuleNotFoundError:
# for python < 3.11, tomli module must be installed # for python < 3.11, tomli module must be installed
@ -469,46 +469,66 @@ def is_time_valid(timestamp):
def login(password): def login(password):
"""
instance = TOML['config']['mastodon_instance'] Login to Mastodon account and return mastodon object used to post content
logging.info('Logging in to ' + instance + ' as ' + TOML['config']['twitter_account']) :param password: Password associated to account. None if not provided
# Create Mastodon application if it does not exist yet :return: mastodon object
if not os.path.isfile(instance + '.secret'): """
# Create Mastodon application if it does not exist yet
if not os.path.isfile(TOML['config']['mastodon_instance'] + '.secret'):
try: try:
Mastodon.create_app( Mastodon.create_app(
'twoot', 'feedtoot',
api_base_url='https://' + instance, api_base_url='https://' + TOML['config']['mastodon_instance'],
to_file=instance + '.secret' to_file=TOML['config']['mastodon_instance'] + '.secret'
) )
except MastodonError as me: 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) logging.fatal(me)
sys.exit(-1) exit(-1)
# Log in to Mastodon instance mastodon = None
try:
mastodon = Mastodon(
client_id=instance + '.secret',
api_base_url='https://' + instance
)
mastodon.log_in( # Log in to Mastodon instance with password
username=TOML['config']['mastodon_user'], if password is not None:
password=password, try:
to_file=TOML['config']['mastodon_user'] + ".secret" mastodon = Mastodon(
) client_id=TOML['config']['mastodon_instance'] + '.secret',
api_base_url='https://' + TOML['config']['mastodon_instance']
)
except MastodonError as me: mastodon.log_in(
logging.fatal('ERROR: Login to ' + instance + ' Failed') username=TOML['config']['mastodon_user'],
logging.fatal(me) password=password,
sys.exit(-1) to_file=TOML['config']['mastodon_user'] + ".secret"
)
logging.info('Logging in to ' + TOML['config']['mastodon_instance'])
# Check ratelimit status except MastodonError as me:
logging.debug('Ratelimit allowed requests: ' + str(mastodon.ratelimit_limit)) logging.fatal('Login to ' + TOML['config']['mastodon_instance'] + ' Failed\n')
logging.debug('Ratelimit remaining requests: ' + str(mastodon.ratelimit_remaining)) logging.fatal(me)
logging.debug('Ratelimit reset time: ' + time.asctime(time.localtime(mastodon.ratelimit_reset))) exit(-1)
logging.debug('Ratelimit last call: ' + time.asctime(time.localtime(mastodon.ratelimit_lastcall)))
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 return mastodon