diff --git a/examples/wifi/expanded/requests_wifi_api_mastodon.py b/examples/wifi/expanded/requests_wifi_api_mastodon.py index 56f3e8d..f6bce9f 100644 --- a/examples/wifi/expanded/requests_wifi_api_mastodon.py +++ b/examples/wifi/expanded/requests_wifi_api_mastodon.py @@ -1,121 +1,117 @@ -# SPDX-FileCopyrightText: 2022 DJDevon3 +# SPDX-FileCopyrightText: 2024 DJDevon3 # SPDX-License-Identifier: MIT -# Coded for Circuit Python 8.0 -"""DJDevon3 Adafruit Feather ESP32-S2 Mastodon_API_Example""" -import gc +# Coded for Circuit Python 8.2.x +"""Mastodon API Example""" +# pylint: disable=import-error + import os -import ssl import time -import socketpool +import adafruit_connection_manager import wifi import adafruit_requests # Mastodon V1 API - Public access (no dev creds or app required) # Visit https://docs.joinmastodon.org/client/public/ for API docs -# For finding your Mastodon User ID -# Login to your mastodon server in a browser, visit your profile, UserID is in the URL. -# Example: https://mastodon.YOURSERVER/web/accounts/YOURUSERIDISHERE +# For finding your Mastodon numerical UserID +# Example: https://mastodon.YOURSERVER/api/v1/accounts/lookup?acct=YourUserName -Mastodon_Server = "mastodon.social" # Set server instance -Mastodon_UserID = "000000000000000000" # Set User ID you want endpoints from +MASTODON_SERVER = "mastodon.social" # Set server instance +MASTODON_USERID = "000000000000000000" # Numerical UserID you want endpoints from # Test in browser first, this will pull up a JSON webpage # https://mastodon.YOURSERVER/api/v1/accounts/YOURUSERIDHERE/statuses?limit=1 -# Initialize WiFi Pool (There can be only 1 pool & top of script) -pool = socketpool.SocketPool(wifi.radio) - -# Time between API refreshes -# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour -sleep_time = 900 - # Get WiFi details, ensure these are setup in settings.toml ssid = os.getenv("CIRCUITPY_WIFI_SSID") password = os.getenv("CIRCUITPY_WIFI_PASSWORD") +# API Polling Rate +# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour +SLEEP_TIME = 900 + +# Initalize Wifi, Socket Pool, Request Session +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) +requests = adafruit_requests.Session(pool, ssl_context) + -# Converts seconds in minutes/hours/days def time_calc(input_time): + """Converts seconds to minutes/hours/days""" if input_time < 60: - sleep_int = input_time - time_output = f"{sleep_int:.0f} seconds" - elif 60 <= input_time < 3600: - sleep_int = input_time / 60 - time_output = f"{sleep_int:.0f} minutes" - elif 3600 <= input_time < 86400: - sleep_int = input_time / 60 / 60 - time_output = f"{sleep_int:.0f} hours" - elif 86400 <= input_time < 432000: - sleep_int = input_time / 60 / 60 / 24 - time_output = f"{sleep_int:.1f} days" - else: # if > 5 days convert float to int & display whole days - sleep_int = input_time / 60 / 60 / 24 - time_output = f"{sleep_int:.0f} days" - return time_output + return f"{input_time:.0f} seconds" + if input_time < 3600: + return f"{input_time / 60:.0f} minutes" + if input_time < 86400: + return f"{input_time / 60 / 60:.0f} hours" + return f"{input_time / 60 / 60 / 24:.1f} days" # Publicly available data no header required MAST_SOURCE = ( "https://" - + Mastodon_Server + + MASTODON_SERVER + "/api/v1/accounts/" - + Mastodon_UserID + + MASTODON_USERID + "/statuses?limit=1" ) -# Connect to Wi-Fi -print("\n===============================") -print("Connecting to WiFi...") -requests = adafruit_requests.Session(pool, ssl.create_default_context()) -while not wifi.radio.ipv4_address: - try: - wifi.radio.connect(ssid, password) - except ConnectionError as e: - print("Connection Error:", e) - print("Retrying in 10 seconds") - time.sleep(10) - gc.collect() -print("Connected!\n") - while True: + # Connect to Wi-Fi + print("\nConnecting to WiFi...") + while not wifi.radio.ipv4_address: + try: + wifi.radio.connect(ssid, password) + except ConnectionError as e: + print("❌ Connection Error:", e) + print("Retrying in 10 seconds") + print("✅ Wifi!") try: - print("\nAttempting to GET MASTODON Stats!") # ----------------------------- # Print Request to Serial - debug_mastodon_full_response = ( - False # STREAMER WARNING: your client secret will be viewable - ) - print("===============================") - mastodon_response = requests.get(url=MAST_SOURCE) + print(" | Attempting to GET MASTODON JSON!") + + # Set debug to True for full JSON response. + # WARNING: may include visible credentials + # MICROCONTROLLER WARNING: might crash by returning too much data + DEBUG_RESPONSE = False + try: + mastodon_response = requests.get(url=MAST_SOURCE) mastodon_json = mastodon_response.json() except ConnectionError as e: - print("Connection Error:", e) + print(f"Connection Error: {e}") print("Retrying in 10 seconds") mastodon_json = mastodon_json[0] - if debug_mastodon_full_response: - print("Full API GET URL: ", MAST_SOURCE) - print(mastodon_json) + print(" | ✅ Mastodon JSON!") + + if DEBUG_RESPONSE: + print(" | | Full API GET URL: ", MAST_SOURCE) mastodon_userid = mastodon_json["account"]["id"] - print("User ID: ", mastodon_userid) + print(f" | | User ID: {mastodon_userid}") + print(mastodon_json) - mastodon_username = mastodon_json["account"]["display_name"] - print("Name: ", mastodon_username) + mastodon_name = mastodon_json["account"]["display_name"] + print(f" | | Name: {mastodon_name}") mastodon_join_date = mastodon_json["account"]["created_at"] - print("Member Since: ", mastodon_join_date) - mastodon_toot_count = mastodon_json["account"]["statuses_count"] - print("Toots: ", mastodon_toot_count) + print(f" | | Member Since: {mastodon_join_date}") mastodon_follower_count = mastodon_json["account"]["followers_count"] - print("Followers: ", mastodon_follower_count) - print("Monotonic: ", time.monotonic()) + print(f" | | Followers: {mastodon_follower_count}") + mastodon_following_count = mastodon_json["account"]["following_count"] + print(f" | | Following: {mastodon_following_count}") + mastodon_toot_count = mastodon_json["account"]["statuses_count"] + print(f" | | Toots: {mastodon_toot_count}") + mastodon_last_toot = mastodon_json["account"]["last_status_at"] + print(f" | | Last Toot: {mastodon_last_toot}") + mastodon_bio = mastodon_json["account"]["note"] + print(f" | | Bio: {mastodon_bio[3:-4]}") # removes included html "

&

" print("\nFinished!") - print("Next Update in: ", time_calc(sleep_time)) + print(f"Board Uptime: {time.monotonic()}") + print(f"Next Update: {time_calc(SLEEP_TIME)}") print("===============================") - gc.collect() except (ValueError, RuntimeError) as e: - print("Failed to get data, retrying\n", e) + print(f"Failed to get data, retrying\n {e}") time.sleep(60) - continue - time.sleep(sleep_time) + break + time.sleep(SLEEP_TIME)