|
| 1 | +# SPDX-FileCopyrightText: 2024 DJDevon3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Coded for Circuit Python 9.x |
| 4 | +"""Queue-Times.com API w/Display Example""" |
| 5 | + |
| 6 | +import os |
| 7 | +import time |
| 8 | + |
| 9 | +import adafruit_connection_manager |
| 10 | +import wifi |
| 11 | + |
| 12 | +import adafruit_requests |
| 13 | + |
| 14 | +# Initalize Wifi, Socket Pool, Request Session |
| 15 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 16 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
| 17 | +requests = adafruit_requests.Session(pool, ssl_context) |
| 18 | + |
| 19 | +# Time between API refreshes |
| 20 | +# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
| 21 | +SLEEP_TIME = 300 |
| 22 | + |
| 23 | +# Get WiFi details, ensure these are setup in settings.toml |
| 24 | +ssid = os.getenv("CIRCUITPY_WIFI_SSID") |
| 25 | +password = os.getenv("CIRCUITPY_WIFI_PASSWORD") |
| 26 | + |
| 27 | +# Publicly Open API (no credentials required) |
| 28 | +QTIMES_SOURCE = "https://queue-times.com/parks/16/queue_times.json" |
| 29 | + |
| 30 | + |
| 31 | +def time_calc(input_time): |
| 32 | + """Converts seconds to minutes/hours/days""" |
| 33 | + if input_time < 60: |
| 34 | + return f"{input_time:.0f} seconds" |
| 35 | + if input_time < 3600: |
| 36 | + return f"{input_time / 60:.0f} minutes" |
| 37 | + if input_time < 86400: |
| 38 | + return f"{input_time / 60 / 60:.0f} hours" |
| 39 | + return f"{input_time / 60 / 60 / 24:.1f} days" |
| 40 | + |
| 41 | + |
| 42 | +qtimes_json = {} |
| 43 | +while True: |
| 44 | + now = time.monotonic() |
| 45 | + # Connect to Wi-Fi |
| 46 | + print("\n===============================") |
| 47 | + print("Connecting to WiFi...") |
| 48 | + while not wifi.radio.ipv4_address: |
| 49 | + try: |
| 50 | + wifi.radio.connect(ssid, password) |
| 51 | + except ConnectionError as e: |
| 52 | + print("❌ Connection Error:", e) |
| 53 | + print("Retrying in 10 seconds") |
| 54 | + print("✅ WiFi!") |
| 55 | + |
| 56 | + try: |
| 57 | + print(" | Attempting to GET Queue-Times JSON!") |
| 58 | + try: |
| 59 | + qtimes_response = requests.get(url=QTIMES_SOURCE) |
| 60 | + qtimes_json = qtimes_response.json() |
| 61 | + except ConnectionError as e: |
| 62 | + print("Connection Error:", e) |
| 63 | + print("Retrying in 10 seconds") |
| 64 | + print(" | ✅ Queue-Times JSON!") |
| 65 | + |
| 66 | + DEBUG_QTIMES = False |
| 67 | + if DEBUG_QTIMES: |
| 68 | + print("Full API GET URL: ", QTIMES_SOURCE) |
| 69 | + print(qtimes_json) |
| 70 | + qtimes_response.close() |
| 71 | + print("✂️ Disconnected from Queue-Times API") |
| 72 | + |
| 73 | + print("\nFinished!") |
| 74 | + print(f"Board Uptime: {time_calc(time.monotonic())}") |
| 75 | + print(f"Next Update: {time_calc(SLEEP_TIME)}") |
| 76 | + print("===============================") |
| 77 | + except (ValueError, RuntimeError) as e: |
| 78 | + print("Failed to get data, retrying\n", e) |
| 79 | + time.sleep(60) |
| 80 | + break |
| 81 | + |
| 82 | + # Loop infinitely until its time to re-poll |
| 83 | + if time.monotonic() - now <= SLEEP_TIME: |
| 84 | + for land in qtimes_json["lands"]: |
| 85 | + qtimes_lands = str(land["name"]) |
| 86 | + print(f" | | Lands: {qtimes_lands}") |
| 87 | + time.sleep(1) |
| 88 | + |
| 89 | + # Loop through each ride in the land |
| 90 | + for ride in land["rides"]: |
| 91 | + qtimes_rides = str(ride["name"]) |
| 92 | + qtimes_queuetime = str(ride["wait_time"]) |
| 93 | + qtimes_isopen = str(ride["is_open"]) |
| 94 | + |
| 95 | + print(f" | | Rides: {qtimes_rides}") |
| 96 | + print(f" | | Queue Time: {qtimes_queuetime} Minutes") |
| 97 | + if qtimes_isopen == "False": |
| 98 | + print(" | | Status: Closed") |
| 99 | + elif qtimes_isopen == "True": |
| 100 | + print(" | | Status: Open") |
| 101 | + else: |
| 102 | + print(" | | Status: Unknown") |
| 103 | + |
| 104 | + time.sleep(1) # delay between list items |
| 105 | + else: # When its time to poll, break to top of while True loop. |
| 106 | + break |
0 commit comments