-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
executable file
·73 lines (53 loc) · 1.74 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
import itertools
import times
import config
from board import DepartureBoard
departures = DepartureBoard()
def fetch_data():
return times.NRETimes(config.STATION, config.DIRECTIONS)
trains = fetch_data()
def next_two_to_waterloo():
next_two = itertools.islice(trains.going_to("London"), 0, 2, 1)
if trains.error:
departures.set_error()
else:
departures.set_trains(next_two)
def next_train_in_each_direction():
next_each = [
next(trains.going_to(direction))
for i, direction in enumerate(config.DIRECTIONS)]
departures.set_trains(next_each)
def all_trains():
full_list = []
for i, direction in enumerate(config.DIRECTIONS):
# Print out all trains in each direction
#print(direction.upper())
#print('-' * len(direction))
for train in trains.going_to(direction):
full_list.append(train)
departures.set_trains(full_list)
from datetime import datetime
import time
# wait two seconds for arduino to reset
print("starting up...", end=" ")
time.sleep(2)
print("OK")
while True:
# update data every 5 minutes
minute = datetime.now().minute
if minute % 5 == 0:
trains = fetch_data()
next_two_to_waterloo()
#next_train_in_each_direction()
# Don't bother fetching trains between 1am and 5am
dt = datetime.now()
if dt.hour in config.QUIET_HOURS:
hour = config.QUIET_HOURS[-1] + 1
switch_on = dt.replace(hour=hour, minute=0, second=0)
sleepytimes = (switch_on - dt).seconds
print('sleeping until {} ({} seconds)'.format(switch_on, sleepytimes))
else:
# Sleep until the next minute boundary
sleepytimes = (60 - dt.second)
time.sleep(sleepytimes)