|
| 1 | +# SPDX-FileCopyrightText: 2022 DJDevon3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Coded for Circuit Python 8.0 |
| 4 | +"""DJDevon3 Adafruit Feather ESP32-S2 Mastodon_API_Example""" |
| 5 | +import gc |
| 6 | +import time |
| 7 | +import ssl |
| 8 | +import wifi |
| 9 | +import socketpool |
| 10 | +import adafruit_requests |
| 11 | + |
| 12 | +# Mastodon V1 API - Public access (no dev creds or app required) |
| 13 | +# Visit https://docs.joinmastodon.org/client/public/ for API docs |
| 14 | +# For finding your Mastodon User ID |
| 15 | +# Login to your mastodon server in a browser, visit your profile, UserID is in the URL. |
| 16 | +# Example: https://mastodon.YOURSERVER/web/accounts/YOURUSERIDISHERE |
| 17 | + |
| 18 | +Mastodon_UserID = "000000000000000000" # Set User ID you want endpoints from |
| 19 | +# Test in browser first, this will pull up a JSON webpage |
| 20 | +# https://mastodon.YOURSERVER/api/v1/accounts/YOURUSERIDHERE/statuses?limit=1 |
| 21 | + |
| 22 | +# Initialize WiFi Pool (There can be only 1 pool & top of script) |
| 23 | +pool = socketpool.SocketPool(wifi.radio) |
| 24 | + |
| 25 | +# Time between API refreshes |
| 26 | +# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
| 27 | +sleep_time = 900 |
| 28 | + |
| 29 | +try: |
| 30 | + from secrets import secrets |
| 31 | +except ImportError: |
| 32 | + print("Secrets File Import Error") |
| 33 | + raise |
| 34 | + |
| 35 | +# Converts seconds in minutes/hours/days |
| 36 | +def time_calc(input_time): |
| 37 | + if input_time < 60: |
| 38 | + sleep_int = input_time |
| 39 | + time_output = f"{sleep_int:.0f} seconds" |
| 40 | + elif 60 <= input_time < 3600: |
| 41 | + sleep_int = input_time / 60 |
| 42 | + time_output = f"{sleep_int:.0f} minutes" |
| 43 | + elif 3600 <= input_time < 86400: |
| 44 | + sleep_int = input_time / 60 / 60 |
| 45 | + time_output = f"{sleep_int:.0f} hours" |
| 46 | + elif 86400 <= input_time < 432000: |
| 47 | + sleep_int = input_time / 60 / 60 / 24 |
| 48 | + time_output = f"{sleep_int:.1f} days" |
| 49 | + else: # if > 5 days convert float to int & display whole days |
| 50 | + sleep_int = input_time / 60 / 60 / 24 |
| 51 | + time_output = f"{sleep_int:.0f} days" |
| 52 | + return time_output |
| 53 | + |
| 54 | + |
| 55 | +# Publicly available data no header required |
| 56 | +MAST_SOURCE = ( |
| 57 | + "https://mastodon.cloud/api/v1/accounts/" |
| 58 | + + Mastodon_UserID |
| 59 | + + "/statuses?limit=1" |
| 60 | +) |
| 61 | + |
| 62 | +# Connect to Wi-Fi |
| 63 | +print("\n===============================") |
| 64 | +print("Connecting to WiFi...") |
| 65 | +requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
| 66 | +while not wifi.radio.ipv4_address: |
| 67 | + try: |
| 68 | + wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 69 | + except ConnectionError as e: |
| 70 | + print("Connection Error:", e) |
| 71 | + print("Retrying in 10 seconds") |
| 72 | + time.sleep(10) |
| 73 | + gc.collect() |
| 74 | +print("Connected!\n") |
| 75 | + |
| 76 | +while True: |
| 77 | + try: |
| 78 | + print("\nAttempting to GET MASTODON Stats!") # ----------------------------- |
| 79 | + # Print Request to Serial |
| 80 | + debug_mastodon_full_response = ( |
| 81 | + False # STREAMER WARNING: your client secret will be viewable |
| 82 | + ) |
| 83 | + print("===============================") |
| 84 | + mastodon_response = requests.get(url=MAST_SOURCE) |
| 85 | + try: |
| 86 | + mastodon_json = mastodon_response.json() |
| 87 | + except ConnectionError as e: |
| 88 | + print("Connection Error:", e) |
| 89 | + print("Retrying in 10 seconds") |
| 90 | + mastodon_json = mastodon_json[0] |
| 91 | + if debug_mastodon_full_response: |
| 92 | + print("Full API GET URL: ", MAST_SOURCE) |
| 93 | + print(mastodon_json) |
| 94 | + mastodon_userid = mastodon_json['account']['id'] |
| 95 | + print("User ID: ", mastodon_userid) |
| 96 | + |
| 97 | + mastodon_username = mastodon_json['account']['display_name'] |
| 98 | + print("Name: ", mastodon_username) |
| 99 | + mastodon_join_date = mastodon_json['account']['created_at'] |
| 100 | + print("Member Since: ", mastodon_join_date) |
| 101 | + mastodon_toot_count = mastodon_json['account']['statuses_count'] |
| 102 | + print("Toots: ", mastodon_toot_count) |
| 103 | + mastodon_follower_count = mastodon_json['account']['followers_count'] |
| 104 | + print("Followers: ", mastodon_follower_count) |
| 105 | + print("Monotonic: ", time.monotonic()) |
| 106 | + |
| 107 | + print("\nFinished!") |
| 108 | + print("Next Update in: ", time_calc(sleep_time)) |
| 109 | + print("===============================") |
| 110 | + gc.collect() |
| 111 | + |
| 112 | + except (ValueError, RuntimeError) as e: |
| 113 | + print("Failed to get data, retrying\n", e) |
| 114 | + time.sleep(60) |
| 115 | + continue |
| 116 | + time.sleep(sleep_time) |
0 commit comments