-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarta.star
85 lines (73 loc) · 2.47 KB
/
marta.star
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
load("render.star", "render")
load("http.star", "http")
load("math.star", "math")
load("cache.star", "cache")
load("encoding/json.star", "json")
MARTA_API = "http://developer.itsmarta.com/RealtimeTrain/RestServiceNextTrain/GetRealtimeArrivals"
STATION = "MIDTOWN STATION"
def main(config):
predictions_cached = cache.get("marta_predictions")
if predictions_cached != None:
predictions = json.decode(predictions_cached)
else:
response = http.get(MARTA_API)
if response.status_code != 200:
fail("MARTA Train API request failed with status %d", response.status_code)
predictions = response.json()
cache.set("marta_predictions", json.encode(predictions), ttl_seconds=10)
count = 0
station_predictions = []
rows = []
for prediction in predictions:
if prediction["STATION"] == STATION:
station_predictions.append(prediction)
count = count + 1
sorted_predictions = sorted(station_predictions, key=sortFunc)
for prediction in sorted_predictions:
dest = getDestination(prediction["DESTINATION"])
time = math.floor(int(prediction["WAITING_SECONDS"]) / 60)
if time == 0: time = "Arr"
else: time = str(time)
rows.append(render.Stack(
children=[
render.Row(
children = [
render.Box(width = 1, height = 1, color = "#000"),
render.Column(
children = [
render.Box(width = 1, height = 1, color = "#000"),
render.Box(width = 3, height = 6, color = getLineColor(prediction["LINE"]))
]
),
render.Box(width = 1, height = 1, color = "#000"),
render.Text(dest, font = "tb-8"),
]
),
render.Row(
expanded=True,
main_align="end",
children = [
render.Text(time, font = "tb-8", color = "#CCF")
]
)
]
))
return render.Root(
render.Column(
children = rows
)
)
def getLineColor(line):
if line == "RED": return "#F00"
if line == "GOLD": return "#FF0"
if line == "BLUE": return "#00F"
if line == "GREEN": return "#080"
return "#000"
def getDestination(dest):
if dest == "North Springs": return "N Springs"
if dest == "Hamilton E Holmes": return "HE Holmes"
if dest == "Indian Creek": return "Indian Cr"
if dest == "Edgewood Candler Park": return "E'wood/CP"
return dest
def sortFunc(prediction):
return int(prediction["WAITING_SECONDS"])