-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastodon-spacestate.py
executable file
·41 lines (34 loc) · 1.44 KB
/
mastodon-spacestate.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
40
41
#!/usr/bin/env python3
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,
(config.spacestate_profile_key, profile_open_text_value),
# Fill empty field slots with empty strings. Without these, Mastodon seems to interpret the
# field update as an append operation.
*([('', '')] * (3-len(config.profile_fields))),
]
print(profile_fields)
# don't be alarmed by the method name, this is just for updating profile metadata
mastodon.account_update_credentials(fields=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()