|
1 |
| -# SPDX-FileCopyrightText: 2022 DJDevon3 |
| 1 | +# SPDX-FileCopyrightText: 2024 DJDevon3 |
2 | 2 | # SPDX-License-Identifier: MIT
|
3 |
| -# Coded for Circuit Python 8.0 |
4 |
| -"""DJDevon3 Adafruit Feather ESP32-S2 Mastodon_API_Example""" |
5 |
| -import gc |
| 3 | +# Coded for Circuit Python 8.2.x |
| 4 | +"""Mastodon API Example""" |
| 5 | +# pylint: disable=import-error |
| 6 | + |
6 | 7 | import os
|
7 |
| -import ssl |
8 | 8 | import time
|
9 | 9 |
|
10 |
| -import socketpool |
| 10 | +import adafruit_connection_manager |
11 | 11 | import wifi
|
12 | 12 |
|
13 | 13 | import adafruit_requests
|
14 | 14 |
|
15 | 15 | # Mastodon V1 API - Public access (no dev creds or app required)
|
16 | 16 | # Visit https://docs.joinmastodon.org/client/public/ for API docs
|
17 |
| -# For finding your Mastodon User ID |
18 |
| -# Login to your mastodon server in a browser, visit your profile, UserID is in the URL. |
19 |
| -# Example: https://mastodon.YOURSERVER/web/accounts/YOURUSERIDISHERE |
| 17 | +# For finding your Mastodon numerical UserID |
| 18 | +# Example: https://mastodon.YOURSERVER/api/v1/accounts/lookup?acct=YourUserName |
20 | 19 |
|
21 |
| -Mastodon_Server = "mastodon.social" # Set server instance |
22 |
| -Mastodon_UserID = "000000000000000000" # Set User ID you want endpoints from |
| 20 | +MASTODON_SERVER = "mastodon.social" # Set server instance |
| 21 | +MASTODON_USERID = "000000000000000000" # Numerical UserID you want endpoints from |
23 | 22 | # Test in browser first, this will pull up a JSON webpage
|
24 | 23 | # https://mastodon.YOURSERVER/api/v1/accounts/YOURUSERIDHERE/statuses?limit=1
|
25 | 24 |
|
26 |
| -# Initialize WiFi Pool (There can be only 1 pool & top of script) |
27 |
| -pool = socketpool.SocketPool(wifi.radio) |
28 |
| - |
29 |
| -# Time between API refreshes |
30 |
| -# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
31 |
| -sleep_time = 900 |
32 |
| - |
33 | 25 | # Get WiFi details, ensure these are setup in settings.toml
|
34 | 26 | ssid = os.getenv("CIRCUITPY_WIFI_SSID")
|
35 | 27 | password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
|
36 | 28 |
|
| 29 | +# API Polling Rate |
| 30 | +# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
| 31 | +SLEEP_TIME = 900 |
| 32 | + |
| 33 | +# Initalize Wifi, Socket Pool, Request Session |
| 34 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 35 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
| 36 | +requests = adafruit_requests.Session(pool, ssl_context) |
| 37 | + |
37 | 38 |
|
38 |
| -# Converts seconds in minutes/hours/days |
39 | 39 | def time_calc(input_time):
|
| 40 | + """Converts seconds to minutes/hours/days""" |
40 | 41 | if input_time < 60:
|
41 |
| - sleep_int = input_time |
42 |
| - time_output = f"{sleep_int:.0f} seconds" |
43 |
| - elif 60 <= input_time < 3600: |
44 |
| - sleep_int = input_time / 60 |
45 |
| - time_output = f"{sleep_int:.0f} minutes" |
46 |
| - elif 3600 <= input_time < 86400: |
47 |
| - sleep_int = input_time / 60 / 60 |
48 |
| - time_output = f"{sleep_int:.0f} hours" |
49 |
| - elif 86400 <= input_time < 432000: |
50 |
| - sleep_int = input_time / 60 / 60 / 24 |
51 |
| - time_output = f"{sleep_int:.1f} days" |
52 |
| - else: # if > 5 days convert float to int & display whole days |
53 |
| - sleep_int = input_time / 60 / 60 / 24 |
54 |
| - time_output = f"{sleep_int:.0f} days" |
55 |
| - return time_output |
| 42 | + return f"{input_time:.0f} seconds" |
| 43 | + if input_time < 3600: |
| 44 | + return f"{input_time / 60:.0f} minutes" |
| 45 | + if input_time < 86400: |
| 46 | + return f"{input_time / 60 / 60:.0f} hours" |
| 47 | + return f"{input_time / 60 / 60 / 24:.1f} days" |
56 | 48 |
|
57 | 49 |
|
58 | 50 | # Publicly available data no header required
|
59 | 51 | MAST_SOURCE = (
|
60 | 52 | "https://"
|
61 |
| - + Mastodon_Server |
| 53 | + + MASTODON_SERVER |
62 | 54 | + "/api/v1/accounts/"
|
63 |
| - + Mastodon_UserID |
| 55 | + + MASTODON_USERID |
64 | 56 | + "/statuses?limit=1"
|
65 | 57 | )
|
66 | 58 |
|
67 |
| -# Connect to Wi-Fi |
68 |
| -print("\n===============================") |
69 |
| -print("Connecting to WiFi...") |
70 |
| -requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
71 |
| -while not wifi.radio.ipv4_address: |
72 |
| - try: |
73 |
| - wifi.radio.connect(ssid, password) |
74 |
| - except ConnectionError as e: |
75 |
| - print("Connection Error:", e) |
76 |
| - print("Retrying in 10 seconds") |
77 |
| - time.sleep(10) |
78 |
| - gc.collect() |
79 |
| -print("Connected!\n") |
80 |
| - |
81 | 59 | while True:
|
| 60 | + # Connect to Wi-Fi |
| 61 | + print("\nConnecting to WiFi...") |
| 62 | + while not wifi.radio.ipv4_address: |
| 63 | + try: |
| 64 | + wifi.radio.connect(ssid, password) |
| 65 | + except ConnectionError as e: |
| 66 | + print("❌ Connection Error:", e) |
| 67 | + print("Retrying in 10 seconds") |
| 68 | + print("✅ Wifi!") |
82 | 69 | try:
|
83 |
| - print("\nAttempting to GET MASTODON Stats!") # ----------------------------- |
84 | 70 | # Print Request to Serial
|
85 |
| - debug_mastodon_full_response = ( |
86 |
| - False # STREAMER WARNING: your client secret will be viewable |
87 |
| - ) |
88 |
| - print("===============================") |
89 |
| - mastodon_response = requests.get(url=MAST_SOURCE) |
| 71 | + print(" | Attempting to GET MASTODON JSON!") |
| 72 | + |
| 73 | + # Set debug to True for full JSON response. |
| 74 | + # WARNING: may include visible credentials |
| 75 | + # MICROCONTROLLER WARNING: might crash by returning too much data |
| 76 | + DEBUG_RESPONSE = False |
| 77 | + |
90 | 78 | try:
|
| 79 | + mastodon_response = requests.get(url=MAST_SOURCE) |
91 | 80 | mastodon_json = mastodon_response.json()
|
92 | 81 | except ConnectionError as e:
|
93 |
| - print("Connection Error:", e) |
| 82 | + print(f"Connection Error: {e}") |
94 | 83 | print("Retrying in 10 seconds")
|
95 | 84 | mastodon_json = mastodon_json[0]
|
96 |
| - if debug_mastodon_full_response: |
97 |
| - print("Full API GET URL: ", MAST_SOURCE) |
98 |
| - print(mastodon_json) |
| 85 | + print(" | ✅ Mastodon JSON!") |
| 86 | + |
| 87 | + if DEBUG_RESPONSE: |
| 88 | + print(" | | Full API GET URL: ", MAST_SOURCE) |
99 | 89 | mastodon_userid = mastodon_json["account"]["id"]
|
100 |
| - print("User ID: ", mastodon_userid) |
| 90 | + print(f" | | User ID: {mastodon_userid}") |
| 91 | + print(mastodon_json) |
101 | 92 |
|
102 |
| - mastodon_username = mastodon_json["account"]["display_name"] |
103 |
| - print("Name: ", mastodon_username) |
| 93 | + mastodon_name = mastodon_json["account"]["display_name"] |
| 94 | + print(f" | | Name: {mastodon_name}") |
104 | 95 | mastodon_join_date = mastodon_json["account"]["created_at"]
|
105 |
| - print("Member Since: ", mastodon_join_date) |
106 |
| - mastodon_toot_count = mastodon_json["account"]["statuses_count"] |
107 |
| - print("Toots: ", mastodon_toot_count) |
| 96 | + print(f" | | Member Since: {mastodon_join_date}") |
108 | 97 | mastodon_follower_count = mastodon_json["account"]["followers_count"]
|
109 |
| - print("Followers: ", mastodon_follower_count) |
110 |
| - print("Monotonic: ", time.monotonic()) |
| 98 | + print(f" | | Followers: {mastodon_follower_count}") |
| 99 | + mastodon_following_count = mastodon_json["account"]["following_count"] |
| 100 | + print(f" | | Following: {mastodon_following_count}") |
| 101 | + mastodon_toot_count = mastodon_json["account"]["statuses_count"] |
| 102 | + print(f" | | Toots: {mastodon_toot_count}") |
| 103 | + mastodon_last_toot = mastodon_json["account"]["last_status_at"] |
| 104 | + print(f" | | Last Toot: {mastodon_last_toot}") |
| 105 | + mastodon_bio = mastodon_json["account"]["note"] |
| 106 | + print(f" | | Bio: {mastodon_bio[3:-4]}") # removes included html "<p> & </p>" |
111 | 107 |
|
112 | 108 | print("\nFinished!")
|
113 |
| - print("Next Update in: ", time_calc(sleep_time)) |
| 109 | + print(f"Board Uptime: {time.monotonic()}") |
| 110 | + print(f"Next Update: {time_calc(SLEEP_TIME)}") |
114 | 111 | print("===============================")
|
115 |
| - gc.collect() |
116 | 112 |
|
117 | 113 | except (ValueError, RuntimeError) as e:
|
118 |
| - print("Failed to get data, retrying\n", e) |
| 114 | + print(f"Failed to get data, retrying\n {e}") |
119 | 115 | time.sleep(60)
|
120 |
| - continue |
121 |
| - time.sleep(sleep_time) |
| 116 | + break |
| 117 | + time.sleep(SLEEP_TIME) |
0 commit comments