|
| 1 | +load("encoding/json.star", "json") |
| 2 | +load("http.star", "http") |
| 3 | +load("render.star", "render") |
| 4 | + |
| 5 | +# Fetch data from your deployed Lambda API |
| 6 | +def fetch_data(): |
| 7 | + res = http.get("https://up8y1e1xae.execute-api.us-east-1.amazonaws.com/lax-departures") |
| 8 | + if res.status_code != 200: |
| 9 | + return None |
| 10 | + |
| 11 | + data = json.decode(res.body()) |
| 12 | + if type(data.get("departures", None)) != "list": |
| 13 | + return None |
| 14 | + |
| 15 | + return data["departures"][:6] # First 6 flights max |
| 16 | + |
| 17 | +# Build one page of output |
| 18 | +def make_page(flights, page_index, total_pages): |
| 19 | + rows = [] |
| 20 | + |
| 21 | + # Header row with left and right components |
| 22 | + rows.append(render.Row(children = [ |
| 23 | + render.Text("Next From LAX", font = "tb-8", color = "#fcf7c5"), |
| 24 | + render.Text(" {}/{}".format(page_index + 1, total_pages), font = "5x8", color = "#666666"), |
| 25 | + ])) |
| 26 | + |
| 27 | + # Add flight rows |
| 28 | + for flight in flights: |
| 29 | + status = flight.get("status", "").lower() |
| 30 | + |
| 31 | + if "canceled" in status: |
| 32 | + time_color = "#ff5555" # red |
| 33 | + elif flight.get("is_past"): |
| 34 | + time_color = "#888888" # gray |
| 35 | + else: |
| 36 | + time_color = "#a8ffb0" # light green |
| 37 | + |
| 38 | + rows.append( |
| 39 | + render.Row(children = [ |
| 40 | + render.Text(flight["scheduled_time"], font = "5x8", color = time_color), |
| 41 | + render.Text(" {} {}".format(flight["airline"], flight["destination"]), font = "5x8"), |
| 42 | + ]), |
| 43 | + ) |
| 44 | + |
| 45 | + return render.Column(children = rows) |
| 46 | + |
| 47 | +# Main entrypoint |
| 48 | +def main(): |
| 49 | + flights = fetch_data() |
| 50 | + if flights == None: |
| 51 | + return render.Root(child = render.Text("Error", font = "5x8", color = "#ff0000")) |
| 52 | + |
| 53 | + pages = [] |
| 54 | + total_pages = (min(len(flights), 6) + 2) // 3 # ceil(len / 3) |
| 55 | + |
| 56 | + for page_index, i in enumerate(range(0, min(len(flights), 6), 3)): |
| 57 | + page_flights = flights[i:i + 3] |
| 58 | + page = make_page(page_flights, page_index, total_pages) |
| 59 | + |
| 60 | + # Repeat page N times to control timing |
| 61 | + for _ in range(200): |
| 62 | + pages.append(page) |
| 63 | + |
| 64 | + return render.Root( |
| 65 | + child = render.Sequence(children = pages), |
| 66 | + ) |
0 commit comments