Skip to content

Commit ddae8af

Browse files
committed
Create requests_adafruit_discord_active_online.py
Example script for Adafruit Discord Active Online Members. Includes some error correction and time based requests. Returns basic json data. Amount of serial prints configurable with debugger booleans.
1 parent 6985b33 commit ddae8af

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# SPDX-FileCopyrightText: 2022 DJDevon3
2+
# SPDX-License-Identifier: MIT
3+
# Coded for Circuit Python 8.0
4+
"""DJDevon3 Adafruit Feather ESP32-S2 Adafruit_Discord_Active_Users_Example"""
5+
# pylint: disable=line-too-long
6+
import gc
7+
import re
8+
import time
9+
import ssl
10+
import wifi
11+
import json
12+
import socketpool
13+
import adafruit_requests
14+
15+
# No user or token required, web scrape.
16+
17+
# Initialize WiFi Pool (There can be only 1 pool & top of script)
18+
pool = socketpool.SocketPool(wifi.radio)
19+
20+
# Time between API refreshes
21+
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
22+
sleep_time = 900
23+
24+
try:
25+
from secrets import secrets
26+
except ImportError:
27+
print("Secrets File Import Error")
28+
raise
29+
30+
if sleep_time < 60:
31+
sleep_time_conversion = "seconds"
32+
sleep_int = sleep_time
33+
elif 60 <= sleep_time < 3600:
34+
sleep_int = sleep_time / 60
35+
sleep_time_conversion = "minutes"
36+
elif 3600 <= sleep_time < 86400:
37+
sleep_int = sleep_time / 60 / 60
38+
sleep_time_conversion = "hours"
39+
else:
40+
sleep_int = sleep_time / 60 / 60 / 24
41+
sleep_time_conversion = "days"
42+
43+
# https://img.shields.io/discord/327254708534116352.svg
44+
ADA_DISCORD_SVG = (
45+
"https://img.shields.io/discord/327254708534116352.json"
46+
)
47+
48+
# Connect to Wi-Fi
49+
print("\n===============================")
50+
print("Connecting to WiFi...")
51+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
52+
while not wifi.radio.ipv4_address:
53+
try:
54+
wifi.radio.connect(secrets['ssid'], secrets['password'])
55+
except ConnectionError as e:
56+
print("Connection Error:", e)
57+
print("Retrying in 10 seconds")
58+
time.sleep(10)
59+
gc.collect()
60+
print("Connected!\n")
61+
62+
while True:
63+
try:
64+
print("\nAttempting to GET DISCORD SVG!") # --------------------------------
65+
# Print Request to Serial
66+
debug_request = True # Set true to see full request
67+
if debug_request:
68+
print("Full API GET URL: ", ADA_DISCORD_SVG)
69+
print("===============================")
70+
try:
71+
ada_SVG_response = requests.get(ADA_DISCORD_SVG).json()
72+
except ConnectionError as e:
73+
print("Connection Error:", e)
74+
print("Retrying in 10 seconds")
75+
76+
# Print Full JSON to Serial
77+
full_ada_SVG_json_response = True # Change to true to see full response
78+
if full_ada_SVG_json_response:
79+
ada_SVG_dump_object = json.dumps(ada_SVG_response)
80+
print("JSON Dump: ", ada_SVG_dump_object)
81+
82+
# Print Debugging to Serial
83+
ada_SVG_debug = True # Set to True to print Serial data
84+
if ada_SVG_debug:
85+
ada_SVG_users = ada_SVG_response['value']
86+
print("SVG Value: ", ada_SVG_users)
87+
regex = " online"
88+
replace_with_nothing = ""
89+
regex_users = re.sub(regex, replace_with_nothing, ada_SVG_users)
90+
print("Regex Value: ", regex_users)
91+
print("Monotonic: ", time.monotonic())
92+
93+
print("\nFinished!")
94+
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion))
95+
print("===============================")
96+
gc.collect()
97+
# pylint: disable=broad-except
98+
except (ValueError, RuntimeError) as e:
99+
print("Failed to get data, retrying\n", e)
100+
time.sleep(60)
101+
continue
102+
time.sleep(sleep_time)

0 commit comments

Comments
 (0)