-
Notifications
You must be signed in to change notification settings - Fork 432
/
defMastodon.py
39 lines (39 loc) · 1.56 KB
/
defMastodon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def sendMastodon(photo, message, config):
from mastodon import Mastodon
sent = False
retry_c = 0
while sent == False:
try:
bot = Mastodon(
access_token=config.get('MASTODON','ACCESS_TOKEN'),
api_base_url=config.get('MASTODON','APP_URL')
)
mediaid = bot.media_post(photo, mime_type="image/jpeg")
sent = bot.status_post(message,None,mediaid,False, "Public")
except Exception as err:
print('err.args:')
print(err.args)
print(f"Unexpected {err=}, {type(err)=}")
print("\nString err:\n"+str(err))
if retry_c > 4:
print('Mastodon attempts exceeded. Message not sent.')
break
elif str(err) == 'Unauthorized':
print('Invalid Mastodon bot token, message not sent.')
break
elif str(err) == 'Timed out':
retry_c += 1
print('Mastodon timeout count: '+str(retry_c))
pass
elif str(err)[:35] == '[Errno 2] No such file or directory':
print('Mastodon module couldn\'t find an image to send.')
break
elif str(err) == 'Media_caption_too_long':
print('Mastodon image caption lenght exceeds 1024 characters. Message not send.')
break
else:
print('[X] Unknown error. Message not sent.')
break
else:
print("Mastodon message successfully sent.")
return sent