-
Notifications
You must be signed in to change notification settings - Fork 0
/
service
88 lines (73 loc) · 2.61 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
#!/usr/bin/python
import sys
sys.path.insert(0, 'dependencies.zip')
import traceback
import time
import pytz
from calendar import timegm
from datetime import datetime, timedelta
from operator import itemgetter
from hosted import config, node
import importer
config.restart_on_update()
WEEKDAYS = {'de_DE': ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag']}
weekdays_local = WEEKDAYS[config.locale or 'de_DE']
tz = pytz.timezone(config.timezone)
try:
sim_date, sim_time = config.time.strip().split()
d, m, y = sim_date.split('.')
ho, mi = sim_time.split(':')
diff = datetime(int(y), int(m), int(d), int(ho), int(mi), 00) - datetime.utcnow()
except:
diff = timedelta()
print >>sys.stderr, "timedelta for simulation: %r" % (diff,)
def current_time():
utc_now = datetime.now(pytz.utc) + diff
timestamp = timegm(utc_now.timetuple()) + utc_now.microsecond / 1000000.
return utc_now, timestamp
def main():
while 1:
now, ts = current_time()
if now.year < 2000:
print >>sys.stderr, "too soon"
time.sleep(1)
continue
events = []
for schedule in config.schedule_urls:
print >>sys.stderr, "fetching %s" % schedule['url']
events.extend(importer.get_schedule(schedule['url']))
print >>sys.stderr, "done. now ", len(events)
if not events:
print >>sys.stderr, "no events"
time.sleep(60)
continue
events.sort(key=itemgetter('start_unix'))
node.write_json("schedule.json", [
dict(
duration = event['duration'],
lang = event['lang'],
place = event['place'],
abstract = event['abstract'],
speakers = [s for s in event['speakers'] if s],
start_str = event['start_str'],
start_date = event['start_date'],
start_weekday = weekdays_local[event['start_weekday']],
title = event['title'],
subtitle = event['subtitle'],
track = event['track'],
start_unix = event['start_unix'],
end_unix = event['end_unix'],
end_str = event['end_str'],
id = event['id'],
) for event in sorted(events, key=itemgetter('start_unix'))
])
print >>sys.stderr, "updated schedule"
deadline = time.time() + 60*5
while time.time() < deadline:
time.sleep(10)
if __name__ == "__main__":
try:
main()
except:
traceback.print_exc()
time.sleep(30)