|
1 |
| -# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries |
| 1 | +# SPDX-FileCopyrightText: 2023 DJDevon3 |
2 | 2 | # SPDX-License-Identifier: MIT
|
3 |
| -# Coded for Circuit Python 8.0 |
4 |
| -"""DJDevon3 Adafruit Feather ESP32-S2 Discord_API_Example""" |
5 |
| -import gc |
| 3 | +# Coded for Circuit Python 8.2 |
| 4 | +# DJDevon3 Adafruit Feather ESP32-S3 Discord API Example |
| 5 | +import os |
6 | 6 | import time
|
7 | 7 | import ssl
|
8 | 8 | import json
|
|
14 | 14 | # WEB SCRAPE authorization key required. Visit URL below.
|
15 | 15 | # Learn how: https://github.com/lorenz234/Discord-Data-Scraping
|
16 | 16 |
|
17 |
| -# Ensure this is in secrets.py or .env |
18 |
| -# "Discord_Authorization": "Discord Authorization from browser console" |
| 17 | +# Ensure this is in settings.toml |
| 18 | +# "Discord_Authorization": "Request Header Auth here" |
| 19 | + |
| 20 | +# Uses settings.toml for credentials |
| 21 | +ssid = os.getenv("CIRCUITPY_WIFI_SSID") |
| 22 | +appw = os.getenv("CIRCUITPY_WIFI_PASSWORD") |
| 23 | +Discord_Auth = os.getenv("Discord_Authorization") |
19 | 24 |
|
20 | 25 | # Initialize WiFi Pool (There can be only 1 pool & top of script)
|
21 | 26 | pool = socketpool.SocketPool(wifi.radio)
|
22 | 27 |
|
23 |
| -# Time between API refreshes |
| 28 | +# API Polling Rate |
24 | 29 | # 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
|
25 | 30 | sleep_time = 900
|
26 | 31 |
|
27 |
| -try: |
28 |
| - from secrets import secrets |
29 |
| -except ImportError: |
30 |
| - print("Secrets File Import Error") |
31 |
| - raise |
32 |
| - |
33 |
| -if sleep_time < 60: |
34 |
| - sleep_time_conversion = "seconds" |
35 |
| - sleep_int = sleep_time |
36 |
| -elif 60 <= sleep_time < 3600: |
37 |
| - sleep_int = sleep_time / 60 |
38 |
| - sleep_time_conversion = "minutes" |
39 |
| -elif 3600 <= sleep_time < 86400: |
40 |
| - sleep_int = sleep_time / 60 / 60 |
41 |
| - sleep_time_conversion = "hours" |
42 |
| -else: |
43 |
| - sleep_int = sleep_time / 60 / 60 / 24 |
44 |
| - sleep_time_conversion = "days" |
45 |
| - |
46 |
| -discord_header = {"Authorization": "" + secrets["Discord_Authorization"]} |
| 32 | + |
| 33 | +# Converts seconds to human readable minutes/hours/days |
| 34 | +def time_calc(input_time): # input_time in seconds |
| 35 | + if input_time < 60: |
| 36 | + sleep_int = input_time |
| 37 | + time_output = f"{sleep_int:.0f} seconds" |
| 38 | + elif 60 <= input_time < 3600: |
| 39 | + sleep_int = input_time / 60 |
| 40 | + time_output = f"{sleep_int:.0f} minutes" |
| 41 | + elif 3600 <= input_time < 86400: |
| 42 | + sleep_int = input_time / 60 / 60 |
| 43 | + time_output = f"{sleep_int:.1f} hours" |
| 44 | + else: |
| 45 | + sleep_int = input_time / 60 / 60 / 24 |
| 46 | + time_output = f"{sleep_int:.1f} days" |
| 47 | + return time_output |
| 48 | + |
| 49 | + |
| 50 | +discord_header = {"Authorization": "" + Discord_Auth} |
47 | 51 | ADA_SOURCE = (
|
48 | 52 | "https://discord.com/api/v10/guilds/"
|
49 | 53 | + "327254708534116352" # Adafruit Discord ID
|
|
56 | 60 | requests = adafruit_requests.Session(pool, ssl.create_default_context())
|
57 | 61 | while not wifi.radio.ipv4_address:
|
58 | 62 | try:
|
59 |
| - wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 63 | + wifi.radio.connect(ssid, appw) |
60 | 64 | except ConnectionError as e:
|
61 | 65 | print("Connection Error:", e)
|
62 | 66 | print("Retrying in 10 seconds")
|
63 | 67 | time.sleep(10)
|
64 |
| - gc.collect() |
65 |
| -print("Connected!\n") |
| 68 | +print("Connected!✅") |
66 | 69 |
|
67 | 70 | while True:
|
68 | 71 | try:
|
69 |
| - print( |
70 |
| - "\nAttempting to GET DISCORD PREVIEW!" |
71 |
| - ) # -------------------------------- |
72 |
| - # Print Request to Serial |
73 |
| - debug_request = False # Set true to see full request |
| 72 | + print("\nAttempting to GET Discord Data!") # -------------------------------- |
| 73 | + # STREAMER WARNING this will show your credentials! |
| 74 | + debug_request = False # Set True to see full request |
74 | 75 | if debug_request:
|
75 | 76 | print("Full API GET URL: ", ADA_SOURCE)
|
76 | 77 | print("===============================")
|
|
81 | 82 | print("Retrying in 10 seconds")
|
82 | 83 |
|
83 | 84 | # Print Full JSON to Serial
|
84 |
| - discord_debug_response = False # Change to true to see full response |
| 85 | + discord_debug_response = False # Set True to see full response |
85 | 86 | if discord_debug_response:
|
86 | 87 | ada_discord_dump_object = json.dumps(ada_res)
|
87 | 88 | print("JSON Dump: ", ada_discord_dump_object)
|
88 | 89 |
|
89 | 90 | # Print keys to Serial
|
90 |
| - discord_debug_keys = True # Set to True to print Serial data |
| 91 | + discord_debug_keys = True # Set True to print Serial data |
91 | 92 | if discord_debug_keys:
|
92 | 93 | ada_discord_all_members = ada_res["approximate_member_count"]
|
93 | 94 | print("Members: ", ada_discord_all_members)
|
94 | 95 |
|
95 | 96 | ada_discord_all_members_online = ada_res["approximate_presence_count"]
|
96 | 97 | print("Online: ", ada_discord_all_members_online)
|
97 | 98 |
|
98 |
| - print("Monotonic: ", time.monotonic()) |
99 |
| - |
100 |
| - print("\nFinished!") |
101 |
| - print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) |
| 99 | + print("Finished ✅") |
| 100 | + print("Board Uptime: ", time_calc(time.monotonic())) |
| 101 | + print("Next Update: ", time_calc(sleep_time)) |
102 | 102 | print("===============================")
|
103 |
| - gc.collect() |
104 | 103 |
|
105 |
| - except (ValueError, RuntimeError) as e: |
| 104 | + except (ConnectionError, ValueError, NameError) as e: |
106 | 105 | print("Failed to get data, retrying\n", e)
|
107 | 106 | time.sleep(60)
|
108 | 107 | continue
|
|
0 commit comments