Skip to content

Commit 35464ee

Browse files
authored
Merge pull request #182 from DJDevon3/DJDevon3-QueueTimesAPI
add Queue-Times API Example
2 parents 0f1b912 + c32d582 commit 35464ee

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# SPDX-FileCopyrightText: 2024 DJDevon3
2+
# SPDX-License-Identifier: MIT
3+
# Coded for Circuit Python 9.x
4+
"""Queue-Times.com API Example"""
5+
6+
import os
7+
8+
import adafruit_connection_manager
9+
import wifi
10+
11+
import adafruit_requests
12+
13+
# Initalize Wifi, Socket Pool, Request Session
14+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
15+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
16+
requests = adafruit_requests.Session(pool, ssl_context)
17+
18+
# Time between API refreshes
19+
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
20+
SLEEP_TIME = 300
21+
22+
# Get WiFi details, ensure these are setup in settings.toml
23+
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
24+
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
25+
26+
# Publicly Open API (no credentials required)
27+
QTIMES_SOURCE = "https://queue-times.com/parks/16/queue_times.json"
28+
29+
30+
def time_calc(input_time):
31+
"""Converts seconds to minutes/hours/days"""
32+
if input_time < 60:
33+
return f"{input_time:.0f} seconds"
34+
if input_time < 3600:
35+
return f"{input_time / 60:.0f} minutes"
36+
if input_time < 86400:
37+
return f"{input_time / 60 / 60:.0f} hours"
38+
return f"{input_time / 60 / 60 / 24:.1f} days"
39+
40+
41+
qtimes_json = {}
42+
43+
# Connect to Wi-Fi
44+
print("\n===============================")
45+
print("Connecting to WiFi...")
46+
while not wifi.radio.ipv4_address:
47+
try:
48+
wifi.radio.connect(ssid, password)
49+
except ConnectionError as e:
50+
print("❌ Connection Error:", e)
51+
print("Retrying in 10 seconds")
52+
print("✅ WiFi!")
53+
54+
try:
55+
with requests.get(url=QTIMES_SOURCE) as qtimes_response:
56+
qtimes_json = qtimes_response.json()
57+
58+
print(" | ✅ Queue-Times JSON\n")
59+
DEBUG_QTIMES = False
60+
if DEBUG_QTIMES:
61+
print("Full API GET URL: ", QTIMES_SOURCE)
62+
print(qtimes_json)
63+
64+
# Poll Once and end script
65+
for land in qtimes_json["lands"]:
66+
qtimes_lands = str(land["name"])
67+
print(f" | Land: {qtimes_lands}")
68+
69+
# Loop through each ride in the land
70+
for ride in land["rides"]:
71+
qtimes_rides = str(ride["name"])
72+
qtimes_queuetime = str(ride["wait_time"])
73+
qtimes_isopen = str(ride["is_open"])
74+
75+
print(f" | | Ride: {qtimes_rides}")
76+
print(f" | | Queue Time: {qtimes_queuetime} Minutes")
77+
if qtimes_isopen == "False":
78+
print(" | | Status: Closed\n")
79+
elif qtimes_isopen == "True":
80+
print(" | | Status: Open\n")
81+
else:
82+
print(" | | Status: Unknown\n")
83+
84+
85+
except ConnectionError as e:
86+
print("Connection Error:", e)

0 commit comments

Comments
 (0)