-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·53 lines (38 loc) · 1.22 KB
/
run.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
from datetime import datetime
import json
import subprocess
import time
FMT = '%Y-%m-%d %I:%M %p'
SEC = 5 * 60 # Every 5 mins
def get_time():
output = subprocess.check_output(["casperjs", "scrape.js"])
lines = output.strip().split('\n')
response = zip(lines[::2], lines[1::2])
home, work = '94105', '94025'
timestamp = datetime.now().strftime(FMT)
ret = dict(time=timestamp)
print timestamp
for (header, info) in response:
label, time = None, None
try:
label, _ = header.split(' - ')
except ValueError, e:
print "\theader:%s\n\t\t%s\n\t\t%s" % (e, header, info)
try:
_, time = info.split(': ')
except ValueError, e:
print "\tinfo:%s\n\t\t%s\n\t\t%s" % (e, header, info)
if label and time:
time = time.strip()
slug = 'home' if label.endswith(home) else 'work'
ret[slug] = time
print '\t%s: %s' % (slug, time)
return ret
def main():
while True:
d = get_time()
with open('data.txt', 'a') as f:
f.write('%s\n' % json.dumps(d, sort_keys=True))
time.sleep(SEC)
if __name__ == '__main__':
main()