Skip to content

Commit 9371463

Browse files
authored
Merge pull request #161 from DJDevon3/MastodonBranch
Update Mastodon API Example with Connection Manager
2 parents 9544d1f + f88e6b4 commit 9371463

File tree

1 file changed

+69
-73
lines changed

1 file changed

+69
-73
lines changed
Lines changed: 69 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,117 @@
1-
# SPDX-FileCopyrightText: 2022 DJDevon3
1+
# SPDX-FileCopyrightText: 2024 DJDevon3
22
# 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+
67
import os
7-
import ssl
88
import time
99

10-
import socketpool
10+
import adafruit_connection_manager
1111
import wifi
1212

1313
import adafruit_requests
1414

1515
# Mastodon V1 API - Public access (no dev creds or app required)
1616
# 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
2019

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
2322
# Test in browser first, this will pull up a JSON webpage
2423
# https://mastodon.YOURSERVER/api/v1/accounts/YOURUSERIDHERE/statuses?limit=1
2524

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-
3325
# Get WiFi details, ensure these are setup in settings.toml
3426
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
3527
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
3628

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+
3738

38-
# Converts seconds in minutes/hours/days
3939
def time_calc(input_time):
40+
"""Converts seconds to minutes/hours/days"""
4041
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"
5648

5749

5850
# Publicly available data no header required
5951
MAST_SOURCE = (
6052
"https://"
61-
+ Mastodon_Server
53+
+ MASTODON_SERVER
6254
+ "/api/v1/accounts/"
63-
+ Mastodon_UserID
55+
+ MASTODON_USERID
6456
+ "/statuses?limit=1"
6557
)
6658

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-
8159
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!")
8269
try:
83-
print("\nAttempting to GET MASTODON Stats!") # -----------------------------
8470
# 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+
9078
try:
79+
mastodon_response = requests.get(url=MAST_SOURCE)
9180
mastodon_json = mastodon_response.json()
9281
except ConnectionError as e:
93-
print("Connection Error:", e)
82+
print(f"Connection Error: {e}")
9483
print("Retrying in 10 seconds")
9584
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)
9989
mastodon_userid = mastodon_json["account"]["id"]
100-
print("User ID: ", mastodon_userid)
90+
print(f" | | User ID: {mastodon_userid}")
91+
print(mastodon_json)
10192

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}")
10495
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}")
10897
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>"
111107

112108
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)}")
114111
print("===============================")
115-
gc.collect()
116112

117113
except (ValueError, RuntimeError) as e:
118-
print("Failed to get data, retrying\n", e)
114+
print(f"Failed to get data, retrying\n {e}")
119115
time.sleep(60)
120-
continue
121-
time.sleep(sleep_time)
116+
break
117+
time.sleep(SLEEP_TIME)

0 commit comments

Comments
 (0)