|
3 | 3 | # Coded for Circuit Python 8.0 |
4 | 4 | """DJDevon3 Adafruit Feather ESP32-S2 Adafruit_Discord_Active_Users_Example""" |
5 | 5 | import gc |
6 | | -import re |
7 | 6 | import time |
8 | 7 | import ssl |
9 | 8 | import json |
10 | 9 | import wifi |
11 | 10 | import socketpool |
12 | 11 | import adafruit_requests |
13 | 12 |
|
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 |
15 | 15 |
|
16 | 16 | # Initialize WiFi Pool (There can be only 1 pool & top of script) |
17 | 17 | pool = socketpool.SocketPool(wifi.radio) |
|
39 | 39 | sleep_int = sleep_time / 60 / 60 / 24 |
40 | 40 | sleep_time_conversion = "days" |
41 | 41 |
|
| 42 | +# Originally attempted to use SVG. Found JSON exists with same filename. |
42 | 43 | # 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" |
44 | 45 |
|
45 | 46 | # Connect to Wi-Fi |
46 | 47 | print("\n===============================") |
|
58 | 59 |
|
59 | 60 | while True: |
60 | 61 | try: |
61 | | - print("\nAttempting to GET DISCORD SVG!") # -------------------------------- |
| 62 | + print("\nAttempting to GET DISCORD SHIELD JSON!") # -------------------------------- |
62 | 63 | # Print Request to Serial |
63 | 64 | debug_request = True # Set true to see full request |
64 | 65 | if debug_request: |
65 | | - print("Full API GET URL: ", ADA_DISCORD_SVG) |
| 66 | + print("Full API GET URL: ", ADA_DISCORD_JSON) |
66 | 67 | print("===============================") |
67 | 68 | try: |
68 | | - ada_SVG_response = requests.get(ADA_DISCORD_SVG).json() |
| 69 | + ada_response = requests.get(ADA_DISCORD_JSON).json() |
69 | 70 | except ConnectionError as e: |
70 | 71 | print("Connection Error:", e) |
71 | 72 | print("Retrying in 10 seconds") |
72 | 73 |
|
73 | 74 | # 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) |
78 | 79 |
|
79 | 80 | # 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" |
85 | 86 | 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) |
88 | 89 | print("Monotonic: ", time.monotonic()) |
89 | 90 |
|
90 | 91 | print("\nFinished!") |
|
0 commit comments