-
Notifications
You must be signed in to change notification settings - Fork 0
/
buses.py
88 lines (79 loc) · 2.69 KB
/
buses.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from screen import Screen
from loading import LoadingScreen
import requests
import datetime
import time
import threading
import logging
class Buses(Screen):
app_key = "2cb8993ac53cdf432e11556ac1d480eb"
app_id = "7f77bbc8"
stop_66 = "3290YYA03633"
stop_44 = "3290YYA03608"
address = "http://transportapi.com/v3/uk/bus/stop/{0}/live.json?app_id={1}&app_key={2}&group=route&nextbus=yes"
def __init__(self, hat):
self.requests = 0
self.hat = hat
self.ls = LoadingScreen(self.hat)
self.notReady = threading.Event()
self.refetch = True
def left(self):
return "BinaryClock"
def request(self, stop):
addr = self.address.format(stop, self.app_id, self.app_key)
try:
ret = requests.request('GET', addr, timeout=5)
return ret.json()
except requests.exceptions.Timeout as e:
return "Timed out"
return "Request failed"
def timeToNext(self, time):
now = datetime.datetime.now()
time = time.split(":")
hr = int(time[0])
mins = int(time[1])
if now.hour != hr:
mins += 60
nxt = mins - now.minute
if nxt <= 0:
self.refetch = True
if nxt == 1:
return str(nxt)+"min"
else:
return str(nxt)+"mins"
def parse(self, data, bus):
next_bus = data["departures"][bus][0]
now = datetime.datetime.now()
if next_bus.has_key("best_departure_estimate"):
time = next_bus["best_departure_estimate"]
return self.timeToNext(time)
else:
time = next_bus["expected_departure_time"]
if time == "null":
time = next_bus["aimed_departure_time"]
if time == "null":
return "No Data"
self.refetch = True
return self.timeToNext(time)
def loadData(self):
self.notReady.set()
loading = threading.Thread(target=self.ls.run, args=(self.notReady,))
loading.start()
data66 = self.request(self.stop_66)
data44 = self.request(self.stop_44)
self.notReady.clear()
loading.join()
self.refetch = False
return (("66", data66), ("44", data44))
def display(self, time, bus):
self.hat.show_message(bus+":"+time, scroll_speed=0.1, text_colour=[100,0,0])
def run(self, going):
while going.isSet():
if self.refetch:
data = self.loadData()
for b,d in data:
self.display(self.parse(d, b), b)
if not going.isSet():
break
if going.isSet():
self.delay(2, going)