From 17b88d752f79b2f044b5ebb7ca470a204e943640 Mon Sep 17 00:00:00 2001 From: Hillebrand van de Groep Date: Sun, 18 Dec 2022 17:39:22 +0100 Subject: [PATCH] initial commit --- .gitignore | 2 ++ LICENSE | 7 +++++++ config.py.dist | 14 ++++++++++++++ mastodon-spacestate.py | 34 ++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 5 files changed, 59 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 config.py.dist create mode 100644 mastodon-spacestate.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..853989b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.py +__pycache__/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..72fbf67 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2022 Stichting Bitlair & contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/config.py.dist b/config.py.dist new file mode 100644 index 0000000..105fb36 --- /dev/null +++ b/config.py.dist @@ -0,0 +1,14 @@ +mqtt_server = 'mqtt.bitlair.nl' +mqtt_port = 1883 + +spacestate_topic = 'bitlair/state/bitlair' +open_string = 'open' +closed_string = 'closed' + +spacestate_profile_key = 'Spacestate' +open_profile_field = 'Open! @ {}' +closed_profile_field = 'Closed @ {}' + +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 new file mode 100644 index 0000000..9ea88b9 --- /dev/null +++ b/mastodon-spacestate.py @@ -0,0 +1,34 @@ +import paho.mqtt.client as mqtt +import config +import datetime +from mastodon import Mastodon + +def set_profile_fields(profile_open_text_value): + profile_fields = config.profile_fields + profile_fields.append((config.spacestate_profile_key, profile_open_text_value)) + mastodon.account_update_credentials(fields=profile_fields) + # don't be alarmed by the method name, this is just for updating profile metadata + print(profile_fields) + +def on_connect(client, userdata, flags, rc): + if rc != 0: + print(f"rc != 0??? rc={rc}") + exit(1) + client.subscribe(config.spacestate_topic) + +def on_message(client, userdata, msg): + message = msg.payload.decode("UTF-8") + cur_dt_string = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + if message == config.open_string: + set_profile_fields(config.open_profile_field.format(cur_dt_string)) + elif message == config.closed_string: + set_profile_fields(config.closed_profile_field.format(cur_dt_string)) + +mastodon = Mastodon(access_token=config.access_token, api_base_url=config.homeserver) + +client = mqtt.Client() +client.on_connect = on_connect +client.on_message = on_message + +client.connect(config.mqtt_server, config.mqtt_port) +client.loop_forever() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9f12059 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +mastodon.py +paho-mqtt \ No newline at end of file