mirror of
https://gitlab.com/jeancf/twoot.git
synced 2025-04-16 09:37:38 +00:00
Implemented new login()
This commit is contained in:
parent
f64645cf69
commit
8ca17bef58
62
twoot.py
62
twoot.py
@ -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,28 +469,33 @@ 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
|
||||||
|
|
||||||
|
# Log in to Mastodon instance with password
|
||||||
|
if password is not None:
|
||||||
try:
|
try:
|
||||||
mastodon = Mastodon(
|
mastodon = Mastodon(
|
||||||
client_id=instance + '.secret',
|
client_id=TOML['config']['mastodon_instance'] + '.secret',
|
||||||
api_base_url='https://' + instance
|
api_base_url='https://' + TOML['config']['mastodon_instance']
|
||||||
)
|
)
|
||||||
|
|
||||||
mastodon.log_in(
|
mastodon.log_in(
|
||||||
@ -498,17 +503,32 @@ def login(password):
|
|||||||
password=password,
|
password=password,
|
||||||
to_file=TOML['config']['mastodon_user'] + ".secret"
|
to_file=TOML['config']['mastodon_user'] + ".secret"
|
||||||
)
|
)
|
||||||
|
logging.info('Logging in to ' + TOML['config']['mastodon_instance'])
|
||||||
|
|
||||||
except MastodonError as me:
|
except MastodonError as me:
|
||||||
logging.fatal('ERROR: Login to ' + instance + ' Failed')
|
logging.fatal('Login to ' + TOML['config']['mastodon_instance'] + ' Failed\n')
|
||||||
logging.fatal(me)
|
logging.fatal(me)
|
||||||
sys.exit(-1)
|
exit(-1)
|
||||||
|
|
||||||
# Check ratelimit status
|
if os.path.isfile(TOML['config']['mastodon_user'] + '.secret'):
|
||||||
logging.debug('Ratelimit allowed requests: ' + str(mastodon.ratelimit_limit))
|
logging.warning("""You successfully logged in using a password. An access token
|
||||||
logging.debug('Ratelimit remaining requests: ' + str(mastodon.ratelimit_remaining))
|
has been saved therefore the password can be omitted from the
|
||||||
logging.debug('Ratelimit reset time: ' + time.asctime(time.localtime(mastodon.ratelimit_reset)))
|
command-line in future invocations""")
|
||||||
logging.debug('Ratelimit last call: ' + time.asctime(time.localtime(mastodon.ratelimit_lastcall)))
|
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
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user