feat: raise exceptions from mastodon
This commit is contained in:
parent
87c386a721
commit
4bd80c31a5
1 changed files with 22 additions and 7 deletions
29
mastodon.py
29
mastodon.py
|
@ -1,6 +1,7 @@
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
from . import utils
|
from . import utils
|
||||||
|
|
||||||
|
session_id = None
|
||||||
|
|
||||||
def get_account_tagged_statuses(app, tag):
|
def get_account_tagged_statuses(app, tag):
|
||||||
mastodon = initialize_client(app)
|
mastodon = initialize_client(app)
|
||||||
|
@ -18,6 +19,7 @@ def get_account_tagged_statuses(app, tag):
|
||||||
return list(map(lambda x: utils.clean_status(x), statuses))
|
return list(map(lambda x: utils.clean_status(x), statuses))
|
||||||
|
|
||||||
def initialize_client(app):
|
def initialize_client(app):
|
||||||
|
global session_id
|
||||||
mastodon = None
|
mastodon = None
|
||||||
secret = None
|
secret = None
|
||||||
try:
|
try:
|
||||||
|
@ -34,18 +36,31 @@ def initialize_client(app):
|
||||||
api_base_url = app['server'],
|
api_base_url = app['server'],
|
||||||
to_file = app['secret_file']
|
to_file = app['secret_file']
|
||||||
)
|
)
|
||||||
mastodon = Mastodon(client_id=app['secret_file'])
|
try:
|
||||||
print('>>> Persisted new token!')
|
mastodon = Mastodon(client_id=app['secret_file'])
|
||||||
|
print('>>> Persisted new token!')
|
||||||
|
except:
|
||||||
|
message = '>>> Failed to create masto client token'
|
||||||
|
raise Exception(message)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
#... otherwise, reuse
|
#... otherwise, reuse
|
||||||
mastodon = Mastodon(access_token=app['secret_file'])
|
mastodon = Mastodon(access_token=app['secret_file'])
|
||||||
print('>>> Reused persisted token!')
|
print('>>> Reused persisted token!')
|
||||||
|
|
||||||
mastodon.log_in(
|
if session_id == None:
|
||||||
app['user'],
|
try:
|
||||||
app['password'],
|
session_id = mastodon.log_in(
|
||||||
to_file = app['secret_file']
|
app['user'],
|
||||||
)
|
app['password'],
|
||||||
|
to_file = app['secret_file']
|
||||||
|
)
|
||||||
|
print('>>> Logged in: ', session_id)
|
||||||
|
except:
|
||||||
|
message = '>>> Failed to get mastodon session'
|
||||||
|
raise Exception(message)
|
||||||
|
else:
|
||||||
|
print('>>> Reused session: ', session_id)
|
||||||
|
|
||||||
|
|
||||||
return mastodon
|
return mastodon
|
Loading…
Reference in a new issue