-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice
executable file
·62 lines (53 loc) · 1.51 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
#!/usr/bin/env python
import os
import urllib
import urllib2
import traceback
import time
import datetime
import pytz
from hosted import CONFIG, NODE
CONFIG.restart_on_update()
def download(key, what, where):
try:
url = "http://api.wunderground.com/api/%s/%s/q/%s.json" % (
key, what, urllib.quote(where)
)
print url
response = urllib2.urlopen(url)
with file("%s.json.tmp" % what, "wb") as f:
f.write(response.read())
os.rename("%s.json.tmp" % what, "%s.json" % what)
except:
traceback.print_exc()
time.sleep(10)
def current_time():
timezone = pytz.timezone(CONFIG['timezone'])
now = datetime.datetime.utcnow()
now = now.replace(tzinfo=pytz.utc)
now = now.astimezone(timezone)
now = now.replace(tzinfo=None)
return now
def send_clock():
now = current_time()
since_midnight = (
now -
now.replace(hour=0, minute=0, second=0, microsecond=0)
)
seconds_since_midnight = since_midnight.seconds + since_midnight.microseconds / 1000000.
NODE['/clock/set'](seconds_since_midnight)
def idle(seconds):
end = time.time() + seconds
while time.time() < end:
send_clock()
time.sleep(5)
def main():
while 1:
if not CONFIG['key']:
time.sleep(5)
continue
download(CONFIG['key'], 'conditions', CONFIG['location'])
download(CONFIG['key'], 'forecast', CONFIG['location'])
idle(900)
if __name__ == "__main__":
main()