This repository was archived by the owner on Feb 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (62 loc) · 2.02 KB
/
main.py
File metadata and controls
79 lines (62 loc) · 2.02 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
from mattermostdriver import Driver
import requests
import time
import os
import yaml
def main():
print("read config")
config = ""
with open("/config/config.yaml", "r") as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
if (config == ""):
print("config is empty")
return
print("start login")
driver = Driver({'url': config['url'], 'login_id': config['user'], 'token': config['token'], 'scheme': 'https', 'port': 443})
driver.login()
print("login successful")
#hardcode timezone because docker container may not know the correct timezone
os.environ['TZ'] = 'Europe/Berlin'
time.tzset()
return driver
def refresh(driver, lastStatus):
team = driver.teams.get_team_by_name('section77')
channel = driver.channels.get_channel_by_name(team['id'], 'clubstatus')
#get current time
timestamp = time.strftime('%H:%M')
#get spaceapi status
response = requests.get('https://api.section77.de')
status = response.json()['state']['open']
#if state has changed
if (lastStatus != status):
#create message
message = ""
name = ""
if (status):
message = f'{timestamp} Die Section77 ist offen'
name = "Status: Offen"
else:
message = f'{timestamp} Die Section77 ist geschlossen'
name = "Status: Geschlossen"
#send message
post = driver.posts.create_post({
'channel_id': channel['id'],
'message': message
})
#update room name
driver.channels.patch_channel(channel['id'], {'id': channel['id'], 'display_name': name})
#log message
print(f"Nachricht \"{message}\" gesendet!")
return status
if __name__ == '__main__':
#execute once
driver = main()
lastStatus = True
#execute every minute
while True:
lastStatus = refresh(driver, lastStatus)
time.sleep(60)