From 0ee71ee2ccc41a874d6a82c9aea6b96e0a94fa95 Mon Sep 17 00:00:00 2001 From: kk6fut Date: Wed, 14 Dec 2022 08:46:03 -0800 Subject: [PATCH] Initial config and mastodon connectors --- configs/mainconf.ini.example | 9 +++++++- defMastodon.py | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 defMastodon.py diff --git a/configs/mainconf.ini.example b/configs/mainconf.ini.example index 114b869..bebdbc7 100644 --- a/configs/mainconf.ini.example +++ b/configs/mainconf.ini.example @@ -61,4 +61,11 @@ CONSUMER_SECRET = cs [MAP] #Map to create from Google Static Maps or screenshot global tar1090 from globe.adsbexchange.com #Enter GOOGLESTATICMAP or ADSBX -OPTION = ADSBX \ No newline at end of file +OPTION = ADSBX + +[MASTODON] +ENABLE = TRUE +ACCESS_TOKEN = mastodonaccesstoken +APP_URL = mastodonappurl +MAX_IMAGE_SIZE = maximagesizeformastodonserver + diff --git a/defMastodon.py b/defMastodon.py new file mode 100644 index 0000000..de6343d --- /dev/null +++ b/defMastodon.py @@ -0,0 +1,40 @@ +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'] + ) + #todo: add photo/image processing here, Mastodon has strict image sizing requirements + mediaid = mastodonBot.media_post(photo, mime_type="image/jpeg") + sent = mastodonBot.status_post(message,None,mediaid,False, feedvisibility) + 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('Telegram 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