Skip to content

Commit e76d9fa

Browse files
committed
removed regex and SVG references
Updated as recommended by FoamyGuy during his livestream. No more regex needed, using string replace.
1 parent 918aeaa commit e76d9fa

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

examples/requests_adafruit_discord_active_online.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
# Coded for Circuit Python 8.0
44
"""DJDevon3 Adafruit Feather ESP32-S2 Adafruit_Discord_Active_Users_Example"""
55
import gc
6-
import re
76
import time
87
import ssl
98
import json
109
import wifi
1110
import socketpool
1211
import adafruit_requests
1312

14-
# No user or token required, web scrape.
13+
# No user or token required, 100% JSON web scrape from SHIELDS.IO
14+
# Adafruit uses Shields.IO to see online users
1515

1616
# Initialize WiFi Pool (There can be only 1 pool & top of script)
1717
pool = socketpool.SocketPool(wifi.radio)
@@ -39,8 +39,9 @@
3939
sleep_int = sleep_time / 60 / 60 / 24
4040
sleep_time_conversion = "days"
4141

42+
# Originally attempted to use SVG. Found JSON exists with same filename.
4243
# https://img.shields.io/discord/327254708534116352.svg
43-
ADA_DISCORD_SVG = "https://img.shields.io/discord/327254708534116352.json"
44+
ADA_DISCORD_JSON = "https://img.shields.io/discord/327254708534116352.json"
4445

4546
# Connect to Wi-Fi
4647
print("\n===============================")
@@ -58,33 +59,33 @@
5859

5960
while True:
6061
try:
61-
print("\nAttempting to GET DISCORD SVG!") # --------------------------------
62+
print("\nAttempting to GET DISCORD SHIELD JSON!") # --------------------------------
6263
# Print Request to Serial
6364
debug_request = True # Set true to see full request
6465
if debug_request:
65-
print("Full API GET URL: ", ADA_DISCORD_SVG)
66+
print("Full API GET URL: ", ADA_DISCORD_JSON)
6667
print("===============================")
6768
try:
68-
ada_SVG_response = requests.get(ADA_DISCORD_SVG).json()
69+
ada_response = requests.get(ADA_DISCORD_JSON).json()
6970
except ConnectionError as e:
7071
print("Connection Error:", e)
7172
print("Retrying in 10 seconds")
7273

7374
# Print Full JSON to Serial
74-
full_ada_SVG_json_response = True # Change to true to see full response
75-
if full_ada_SVG_json_response:
76-
ada_SVG_dump_object = json.dumps(ada_SVG_response)
77-
print("JSON Dump: ", ada_SVG_dump_object)
75+
full_ada_json_response = True # Change to true to see full response
76+
if full_ada_json_response:
77+
ada_dump_object = json.dumps(ada_response)
78+
print("JSON Dump: ", ada_dump_object)
7879

7980
# Print Debugging to Serial
80-
ada_SVG_debug = True # Set to True to print Serial data
81-
if ada_SVG_debug:
82-
ada_SVG_users = ada_SVG_response["value"]
83-
print("SVG Value: ", ada_SVG_users)
84-
regex = " online"
81+
ada_debug = True # Set to True to print Serial data
82+
if ada_debug:
83+
ada_users = ada_response["value"]
84+
print("JSON Value: ", ada_users)
85+
online_string = " online"
8586
replace_with_nothing = ""
86-
regex_users = re.sub(regex, replace_with_nothing, ada_SVG_users)
87-
print("Regex Value: ", regex_users)
87+
string_replace_users = ada_users.replace(online_string, replace_with_nothing)
88+
print("Replaced Value: ", string_replace_users)
8889
print("Monotonic: ", time.monotonic())
8990

9091
print("\nFinished!")

0 commit comments

Comments
 (0)