This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_status.py
78 lines (65 loc) · 2.05 KB
/
generate_status.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import json
from datetime import datetime
import requests
from jinja2 import Template
import config
api_data = config.API_DATA.copy()
for plugin in config.PLUGINS:
try:
plugin_module = getattr(
__import__("plugins.%s" % plugin, plugin).__dict__[plugin], plugin
)
except Exception as e:
print(e)
else:
old_data = api_data
try:
api_data = plugin_module(api_data)
except:
api_data = old_data
# The next part will generate the HTML for the status page
with open(config.SAVE_PATH, 'w') as f:
f.write(json.dumps(api_data))
f.close()
if api_data['state']['open'] is True:
icon = api_data['icon']['open']
else:
icon = api_data['icon']['closed']
if 'names' in api_data['sensors']['people_now_present'][0]:
people = ", ".join(api_data['sensors']['people_now_present'][0]['names'])
else:
people = ""
html_parser_args = {
'status_open': api_data['state']['open'],
'status_lastchange': datetime.fromtimestamp(
int(api_data['state']['lastchange'])).strftime('%H:%M:%S %d.%m.%Y'),
'status_message': api_data['state']['message'],
'status_icon': icon,
'logo': api_data['logo'],
'url': api_data['url'],
'location': api_data['location'],
'people_now_present': api_data['sensors']['people_now_present'][0]['value'],
'names': people,
'temperatures': False,
}
for plugin in config.PLUGINS:
try:
plugin_module = getattr(
__import__("plugins.%s" % plugin, plugin).__dict__[plugin],
plugin + "_html"
)
except Exception as e:
pass
else:
old_args = html_parser_args
try:
html_parser_args = plugin_module(api_data, html_parser_args)
except:
html_parser_args = old_args
html_template = Template(open(config.TEMPLATE_PATH, encoding='utf-8').read())
html = html_template.render(**html_parser_args)
with open(config.HTML_PATH, 'wb+') as html_out:
html_out.write(html.encode('utf-8'))