From a352f3e442d23a982252daf2e2b9bc16c073e8d7 Mon Sep 17 00:00:00 2001 From: Hillebrand van de Groep Date: Wed, 21 Dec 2022 20:29:35 +0100 Subject: [PATCH] will now update profile pic based upon spacestate if configured --- .gitignore | 4 +++- config.py.dist | 4 ++++ mastodon-spacestate.py | 24 ++++++++++++++++++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 853989b..bf5c876 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ config.py -__pycache__/ \ No newline at end of file +__pycache__/ +./open.png +./closed.png \ No newline at end of file diff --git a/config.py.dist b/config.py.dist index 105fb36..54a7d20 100644 --- a/config.py.dist +++ b/config.py.dist @@ -9,6 +9,10 @@ spacestate_profile_key = 'Spacestate' open_profile_field = 'Open! @ {}' closed_profile_field = 'Closed @ {}' +update_profilepic_on_spacestate_change = True +spacestate_open_profilepic = './open.png' +spacestate_closed_profilepic = './closed.png' + profile_fields = [('Website', 'https://bitlair.nl/')] access_token = 'aaaaAAAAaaaa1234' homeserver = 'https://hsnl.social' \ No newline at end of file diff --git a/mastodon-spacestate.py b/mastodon-spacestate.py index 9ea88b9..92085f4 100644 --- a/mastodon-spacestate.py +++ b/mastodon-spacestate.py @@ -3,10 +3,16 @@ import config import datetime from mastodon import Mastodon -def set_profile_fields(profile_open_text_value): +if hasattr(config, 'update_profilepic_on_spacestate_change'): + update_profilepic_on_spacestate_change = config.update_profilepic_on_spacestate_change +else: + # backcompat, standaard false + update_profilepic_on_spacestate_change = False + +def set_profile_fields(profile_open_text_value, picture = None): profile_fields = config.profile_fields profile_fields.append((config.spacestate_profile_key, profile_open_text_value)) - mastodon.account_update_credentials(fields=profile_fields) + mastodon.account_update_credentials(fields=profile_fields, avatar=picture) # don't be alarmed by the method name, this is just for updating profile metadata print(profile_fields) @@ -18,11 +24,21 @@ def on_connect(client, userdata, flags, rc): def on_message(client, userdata, msg): message = msg.payload.decode("UTF-8") + print(message) cur_dt_string = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + new_avatar = None + new_profile_value = None if message == config.open_string: - set_profile_fields(config.open_profile_field.format(cur_dt_string)) + if update_profilepic_on_spacestate_change: + new_avatar = config.spacestate_open_profilepic + new_profile_value = config.open_profile_field.format(cur_dt_string) elif message == config.closed_string: - set_profile_fields(config.closed_profile_field.format(cur_dt_string)) + if update_profilepic_on_spacestate_change: + new_avatar = config.spacestate_open_profilepic + new_profile_value = config.closed_profile_field.format(cur_dt_string) + + if new_profile_value is not None: + set_profile_fields(new_profile_value, picture=new_avatar) mastodon = Mastodon(access_token=config.access_token, api_base_url=config.homeserver)