-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice
executable file
·281 lines (240 loc) · 10.5 KB
/
service
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/python
import os
import sys
import time
import traceback
from calendar import timegm
from datetime import datetime, timedelta
import pytz
from requests import get
from json import JSONDecoder
from collections import OrderedDict
from hosted import config, node
config.restart_on_update()
SEND_PREFIX = os.environ["NODE"].replace("root/", "root/plugin/")
class TalkSorter(object):
def __init__(self, obj):
self.obj = obj
def __lt__(self, other):
if not isinstance(other, TalkSorter) or "start_ts" not in other.obj:
raise ValueError("TalkSorter can only compare talk dicts")
if (
"room_idx" in self.obj
and "room_idx" in other.obj
and self.obj["start_ts"] == other.obj["start_ts"]
):
return self.obj["room_idx"] < other.obj["room_idx"]
return self.obj["start_ts"] < other.obj["start_ts"]
def log(msg):
sys.stderr.write("[pretalx] {}\n".format(msg))
def idle(seconds, event_start, event_end, event_tz, event_days):
end = time.time() + seconds
log("sleeping for {} seconds".format(seconds))
while time.time() < end:
send_data = {
"clock": "??",
"day": "??",
"time": int(time.time()),
}
if event_start is not None:
event_now = datetime.now(event_tz)
event_day = None
log("event_now = {}".format(event_now.isoformat()))
send_data["clock"] = event_now.strftime(config["clock_format"])
for day in event_days:
if day["starts"] <= event_now <= day["ends"]:
log("Today is day {} according to schedule".format(day["index"]))
event_day = day["index"]
if event_day is None:
log(
"Schedule does not contain information for this day, calculating using event start time"
)
utc_now = datetime.now(pytz.utc)
day_zero = event_tz.localize(
event_start.replace(hour=0, minute=0, second=0)
) - timedelta(days=1)
day_info = event_now - day_zero
log(
"Today is day {}, based on day zero at {}".format(
day_info.days, day_zero.isoformat()
)
)
event_day = day_info.days
send_data["day"] = event_day
# "single day" is only used in standalone mode to hide the
# the day information
if event_end is not None:
if event_start == event_end:
send_data["single_day"] = 1
else:
send_data["single_day"] = 0
for k, v in send_data.items():
node.send_raw("{}/{}:{}".format(SEND_PREFIX, k, v))
time.sleep(1)
def main():
event_info = None
event_start = None
event_end = None
schedule = None
event_tz = pytz.utc
while True:
schedule_url = config["schedule_url"]
room_uuid_mapping = {}
event_days = []
if "example.com" in schedule_url:
log("default schedule url, waiting for config update")
# sleep forever, service will get restarted if the config
# changes.
time.sleep(99999999)
log("event url: {}".format(schedule_url))
log("using json flavour: {}".format(config["json_flavour"]))
if config["json_flavour"] == "pretalx-broadcast-tools":
if not schedule_url.endswith("/"):
schedule_url = schedule_url + "/"
try:
r = get(
schedule_url + "p/broadcast-tools/event.json",
)
r.raise_for_status()
except Exception as e:
log("updating event info failed: {}".format(repr(e)))
# Only print the error message. If we have fetched the event
# info json blob atleast once, we have all the information
# we need.
else:
event_info = r.json()
if event_info is not None:
event_start = datetime.strptime(event_info["start"], "%Y-%m-%d")
event_end = datetime.strptime(event_info["end"], "%Y-%m-%d")
event_tz = pytz.timezone(event_info["timezone"])
for uuid, room_name in event_info.get("rooms", {}).items():
room_uuid_mapping[room_name] = uuid
try:
r = get(
schedule_url + "p/broadcast-tools/schedule.json",
)
r.raise_for_status()
except Exception as e:
log("updating schedule failed: {}".format(repr(e)))
# Only print the error message. If we have fetched the schedule
# info json blob atleast once, we have all the information
# we need.
else:
schedule = JSONDecoder(object_pairs_hook=OrderedDict).decode(r.text)
tracks = {}
for talk in schedule["talks"]:
talk["start_str"] = (
datetime.fromtimestamp(talk["start_ts"])
.replace(tzinfo=pytz.utc)
.astimezone(event_tz)
.strftime("%H:%M")
)
talk["room_uuid"] = room_uuid_mapping.get(talk["room"])
if talk["track"]:
if talk["track"]["name"] not in tracks:
tracks[talk["track"]["name"]] = talk["track"]["color"]
schedule["tracks"] = []
for name, color in sorted(tracks.items()):
schedule["tracks"].append(
{
"name": name,
"color": color,
}
)
schedule["talks"] = sorted(schedule["talks"], key=TalkSorter)
node.write_json("schedule.json", schedule)
log("updated schedule json")
elif config["json_flavour"] == "voc-schema":
try:
r = get(schedule_url)
r.raise_for_status()
except Exception as e:
log("getting schedule.json failed: {}".format(repr(e)))
else:
raw_schedule = JSONDecoder(object_pairs_hook=OrderedDict).decode(r.text)["schedule"]
schedule = {
"tracks": raw_schedule["conference"]["tracks"],
"talks": [],
}
for room in raw_schedule["conference"]["rooms"]:
room_uuid_mapping[room["name"]] = room["guid"]
event_start = datetime.strptime(
raw_schedule["conference"]["start"][:10], "%Y-%m-%d"
)
event_end = datetime.strptime(
raw_schedule["conference"]["end"][:10], "%Y-%m-%d"
)
event_tz = pytz.timezone(raw_schedule["conference"]["time_zone_name"])
for day in raw_schedule["conference"]["days"]:
event_days.append(
{
"index": day["index"],
"starts": event_tz.localize(
datetime.strptime(
day["day_start"][:19], "%Y-%m-%dT%H:%M:%S"
)
),
"ends": event_tz.localize(
datetime.strptime(
day["day_end"][:19], "%Y-%m-%dT%H:%M:%S"
)
),
}
)
for room_idx, (room_name, room) in enumerate(day["rooms"].items()):
for talk in room:
start = event_tz.localize(
datetime.strptime(
talk["date"][:19], "%Y-%m-%dT%H:%M:%S"
)
)
d_h, d_m = talk["duration"].split(":")
end = start + timedelta(hours=int(d_h), minutes=int(d_m))
talk["start_ts"] = int(
timegm(start.timetuple())
- start.utcoffset().total_seconds()
)
talk["start_str"] = talk["start"]
talk["end_ts"] = int(
timegm(end.timetuple())
- end.utcoffset().total_seconds()
)
talk["locale"] = talk["language"]
track = None
if talk["track"]:
for t in raw_schedule["conference"]["tracks"]:
if t["name"] == talk["track"]:
track = {
"color": t["color"],
"name": t["name"],
}
break
talk["track"] = track
talk["room_uuid"] = room_uuid_mapping.get(talk["room"])
talk["room_idx"] = room_idx
persons = []
for p in talk["persons"]:
name = p.get("public_name", p.get("name"))
if name:
persons.append(name)
talk["persons"] = persons
schedule["talks"].append(talk)
schedule["talks"] = sorted(schedule["talks"], key=TalkSorter)
node.write_json("schedule.json", schedule)
log("updated schedule json")
else:
log("unknown json flavour, something is very wrong")
uuid_json = {}
for name, uuid in room_uuid_mapping.items():
uuid_json[uuid] = name
node.write_json("uuid.json", uuid_json)
idle(30, event_start, event_end, event_tz, event_days)
if __name__ == "__main__":
try:
main()
except Exception:
traceback.print_exc()
try:
idle(30, None, None, None)
except Exception:
time.sleep(30)