|
| 1 | +# SPDX-FileCopyrightText: 2024 DJDevon3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Coded for Circuit Python 8.2.x |
| 4 | +"""Premiere League Total Players API Example""" |
| 5 | +# pylint: disable=import-error |
| 6 | + |
| 7 | +import os |
| 8 | +import time |
| 9 | + |
| 10 | +import adafruit_connection_manager |
| 11 | +import adafruit_json_stream as json_stream |
| 12 | +import wifi |
| 13 | + |
| 14 | +import adafruit_requests |
| 15 | + |
| 16 | +# Public API. No user or token required |
| 17 | + |
| 18 | +# Get WiFi details, ensure these are setup in settings.toml |
| 19 | +ssid = os.getenv("CIRCUITPY_WIFI_SSID") |
| 20 | +password = os.getenv("CIRCUITPY_WIFI_PASSWORD") |
| 21 | + |
| 22 | +# API Polling Rate |
| 23 | +# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
| 24 | +SLEEP_TIME = 900 |
| 25 | + |
| 26 | +# Initalize Wifi, Socket Pool, Request Session |
| 27 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 28 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
| 29 | +requests = adafruit_requests.Session(pool, ssl_context) |
| 30 | + |
| 31 | +# Publicly available data no header required |
| 32 | +PREMIERE_LEAGUE_SOURCE = "https://fantasy.premierleague.com/api/bootstrap-static/" |
| 33 | + |
| 34 | + |
| 35 | +def time_calc(input_time): |
| 36 | + """Converts seconds to minutes/hours/days""" |
| 37 | + if input_time < 60: |
| 38 | + return f"{input_time:.0f} seconds" |
| 39 | + if input_time < 3600: |
| 40 | + return f"{input_time / 60:.0f} minutes" |
| 41 | + if input_time < 86400: |
| 42 | + return f"{input_time / 60 / 60:.0f} hours" |
| 43 | + return f"{input_time / 60 / 60 / 24:.1f} days" |
| 44 | + |
| 45 | + |
| 46 | +while True: |
| 47 | + # Connect to Wi-Fi |
| 48 | + print("\nConnecting to WiFi...") |
| 49 | + while not wifi.radio.ipv4_address: |
| 50 | + try: |
| 51 | + wifi.radio.connect(ssid, password) |
| 52 | + except ConnectionError as e: |
| 53 | + print("❌ Connection Error:", e) |
| 54 | + print("Retrying in 10 seconds") |
| 55 | + print("✅ Wifi!") |
| 56 | + |
| 57 | + try: |
| 58 | + print(" | Attempting to GET Premiere League JSON!") |
| 59 | + |
| 60 | + # Set debug to True for full JSON response. |
| 61 | + # WARNING: may include visible credentials |
| 62 | + # MICROCONTROLLER WARNING: might crash by returning too much data |
| 63 | + DEBUG_RESPONSE = False |
| 64 | + |
| 65 | + try: |
| 66 | + PREMIERE_LEAGUE_RESPONSE = requests.get(url=PREMIERE_LEAGUE_SOURCE) |
| 67 | + pl_json = json_stream.load(PREMIERE_LEAGUE_RESPONSE.iter_content(32)) |
| 68 | + except ConnectionError as e: |
| 69 | + print(f"Connection Error: {e}") |
| 70 | + print("Retrying in 10 seconds") |
| 71 | + print(" | ✅ Premiere League JSON!") |
| 72 | + |
| 73 | + print(f" | Total Premiere League Players: {pl_json['total_players']}") |
| 74 | + PREMIERE_LEAGUE_RESPONSE.close() |
| 75 | + print("✂️ Disconnected from Premiere League") |
| 76 | + |
| 77 | + print("\nFinished!") |
| 78 | + print(f"Board Uptime: {time.monotonic()}") |
| 79 | + print(f"Next Update: {time_calc(SLEEP_TIME)}") |
| 80 | + print("===============================") |
| 81 | + |
| 82 | + except (ValueError, RuntimeError) as e: |
| 83 | + print(f"Failed to get data, retrying\n {e}") |
| 84 | + time.sleep(60) |
| 85 | + break |
| 86 | + time.sleep(SLEEP_TIME) |
0 commit comments