-
Notifications
You must be signed in to change notification settings - Fork 6
/
service
executable file
·79 lines (67 loc) · 2.26 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
#!/usr/bin/python
import os
import sys
import time
import pytz
import json
from calendar import timegm
from datetime import datetime
from operator import itemgetter
from hosted import CONFIG, NODE
import importer
CONFIG.restart_on_update()
diff = datetime(2014,7,22,8,20,00) - datetime.utcnow()
def current_time():
now = datetime.utcnow() # + diff
timestamp = timegm(now.timetuple()) + now.microsecond / 1000000.
return now, timestamp
def send_clock(now, ts):
now = now.replace(tzinfo=pytz.utc)
now = now.astimezone(pytz.timezone(CONFIG['timezone']))
now = now.replace(tzinfo=None)
since_midnight = (
now -
now.replace(hour=0, minute=0, second=0, microsecond=0)
)
since_midnight = since_midnight.seconds + since_midnight.microseconds / 1000000.
NODE.send('/clock/set:%f' % ts)
NODE.send('/clock/midnight:%f' % since_midnight)
def main():
while 1:
now, ts = current_time()
if now.year < 2000:
print >>sys.stderr, "too soon"
time.sleep(1)
continue
has_events, events = importer.get_schedule(
CONFIG['schedule_url'],
pytz.timezone(CONFIG['timezone']),
)
if not has_events:
print >>sys.stderr, "no events"
time.sleep(60)
continue
with file("schedule.json.new", "wb") as f:
f.write(json.dumps([dict(
duration = event['duration'],
lang = event['lang'],
place = event['place'],
speakers = event['speakers'],
start_str = event['start_str'],
title = event['title'],
type = event['type'],
start_unix = event['start_unix'],
end_unix = event['end_unix'],
) for event in sorted(events, key=itemgetter('start_unix'))],
ensure_ascii=False,
separators=(',',':')
).encode('utf8'))
os.rename("schedule.json.new", "schedule.json")
print >>sys.stderr, "updated schedule"
for i in xrange(60):
now, ts = current_time()
print >>sys.stderr, "time is", now, ts
send_clock(now, ts)
time.sleep(10)
if __name__ == "__main__":
main()