Skip to content

Commit 640bfac

Browse files
authored
Merge pull request #115 from DJDevon3/main
Add API examples for various social media sites
2 parents 8a19521 + c662605 commit 640bfac

File tree

5 files changed

+556
-0
lines changed

5 files changed

+556
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
# Coded for Circuit Python 8.0
4+
"""DJDevon3 Adafruit Feather ESP32-S2 Adafruit_Discord_Active_Users_Example"""
5+
import gc
6+
import time
7+
import ssl
8+
import json
9+
import wifi
10+
import socketpool
11+
import adafruit_requests
12+
13+
# No user or token required, 100% JSON web scrape from SHIELDS.IO
14+
# Adafruit uses Shields.IO to see online users
15+
16+
# Initialize WiFi Pool (There can be only 1 pool & top of script)
17+
pool = socketpool.SocketPool(wifi.radio)
18+
19+
# Time between API refreshes
20+
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
21+
sleep_time = 900
22+
23+
try:
24+
from secrets import secrets
25+
except ImportError:
26+
print("Secrets File Import Error")
27+
raise
28+
29+
if sleep_time < 60:
30+
sleep_time_conversion = "seconds"
31+
sleep_int = sleep_time
32+
elif 60 <= sleep_time < 3600:
33+
sleep_int = sleep_time / 60
34+
sleep_time_conversion = "minutes"
35+
elif 3600 <= sleep_time < 86400:
36+
sleep_int = sleep_time / 60 / 60
37+
sleep_time_conversion = "hours"
38+
else:
39+
sleep_int = sleep_time / 60 / 60 / 24
40+
sleep_time_conversion = "days"
41+
42+
# Originally attempted to use SVG. Found JSON exists with same filename.
43+
# https://img.shields.io/discord/327254708534116352.svg
44+
ADA_DISCORD_JSON = "https://img.shields.io/discord/327254708534116352.json"
45+
46+
# Connect to Wi-Fi
47+
print("\n===============================")
48+
print("Connecting to WiFi...")
49+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
50+
while not wifi.radio.ipv4_address:
51+
try:
52+
wifi.radio.connect(secrets["ssid"], secrets["password"])
53+
except ConnectionError as e:
54+
print("Connection Error:", e)
55+
print("Retrying in 10 seconds")
56+
time.sleep(10)
57+
gc.collect()
58+
print("Connected!\n")
59+
60+
while True:
61+
try:
62+
print(
63+
"\nAttempting to GET DISCORD SHIELD JSON!"
64+
) # --------------------------------
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_JSON)
69+
print("===============================")
70+
try:
71+
ada_response = requests.get(ADA_DISCORD_JSON).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_json_response = True # Change to true to see full response
78+
if full_ada_json_response:
79+
ada_dump_object = json.dumps(ada_response)
80+
print("JSON Dump: ", ada_dump_object)
81+
82+
# Print Debugging to Serial
83+
ada_debug = True # Set to True to print Serial data
84+
if ada_debug:
85+
ada_users = ada_response["value"]
86+
print("JSON Value: ", ada_users)
87+
online_string = " online"
88+
replace_with_nothing = ""
89+
string_replace_users = ada_users.replace(
90+
online_string, replace_with_nothing
91+
)
92+
print("Replaced Value: ", string_replace_users)
93+
print("Monotonic: ", time.monotonic())
94+
95+
print("\nFinished!")
96+
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion))
97+
print("===============================")
98+
gc.collect()
99+
100+
except (ValueError, RuntimeError) as e:
101+
print("Failed to get data, retrying\n", e)
102+
time.sleep(60)
103+
continue
104+
time.sleep(sleep_time)

examples/requests_api_discord.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
# Coded for Circuit Python 8.0
4+
"""DJDevon3 Adafruit Feather ESP32-S2 Discord_API_Example"""
5+
import gc
6+
import time
7+
import ssl
8+
import json
9+
import wifi
10+
import socketpool
11+
import adafruit_requests
12+
13+
# Active Logged in User Account Required, no tokens required
14+
# WEB SCRAPE authorization key required. Visit URL below.
15+
# Learn how: https://github.com/lorenz234/Discord-Data-Scraping
16+
17+
# Ensure this is in secrets.py or .env
18+
# "Discord_Authorization": "Discord Authorization from browser console"
19+
20+
# Initialize WiFi Pool (There can be only 1 pool & top of script)
21+
pool = socketpool.SocketPool(wifi.radio)
22+
23+
# Time between API refreshes
24+
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
25+
sleep_time = 900
26+
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"]}
47+
ADA_SOURCE = (
48+
"https://discord.com/api/v10/guilds/"
49+
+ "327254708534116352" # Adafruit Discord ID
50+
+ "/preview"
51+
)
52+
53+
# Connect to Wi-Fi
54+
print("\n===============================")
55+
print("Connecting to WiFi...")
56+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
57+
while not wifi.radio.ipv4_address:
58+
try:
59+
wifi.radio.connect(secrets["ssid"], secrets["password"])
60+
except ConnectionError as e:
61+
print("Connection Error:", e)
62+
print("Retrying in 10 seconds")
63+
time.sleep(10)
64+
gc.collect()
65+
print("Connected!\n")
66+
67+
while True:
68+
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
74+
if debug_request:
75+
print("Full API GET URL: ", ADA_SOURCE)
76+
print("===============================")
77+
try:
78+
ada_res = requests.get(url=ADA_SOURCE, headers=discord_header).json()
79+
except ConnectionError as e:
80+
print("Connection Error:", e)
81+
print("Retrying in 10 seconds")
82+
83+
# Print Full JSON to Serial
84+
discord_debug_response = False # Change to true to see full response
85+
if discord_debug_response:
86+
ada_discord_dump_object = json.dumps(ada_res)
87+
print("JSON Dump: ", ada_discord_dump_object)
88+
89+
# Print keys to Serial
90+
discord_debug_keys = True # Set to True to print Serial data
91+
if discord_debug_keys:
92+
93+
ada_discord_all_members = ada_res["approximate_member_count"]
94+
print("Members: ", ada_discord_all_members)
95+
96+
ada_discord_all_members_online = ada_res["approximate_presence_count"]
97+
print("Online: ", ada_discord_all_members_online)
98+
99+
print("Monotonic: ", time.monotonic())
100+
101+
print("\nFinished!")
102+
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion))
103+
print("===============================")
104+
gc.collect()
105+
106+
except (ValueError, RuntimeError) as e:
107+
print("Failed to get data, retrying\n", e)
108+
time.sleep(60)
109+
continue
110+
time.sleep(sleep_time)

examples/requests_api_github.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
# Coded for Circuit Python 8.0
4+
"""DJDevon3 Adafruit Feather ESP32-S2 Github_API_Example"""
5+
import gc
6+
import time
7+
import ssl
8+
import json
9+
import wifi
10+
import socketpool
11+
import adafruit_requests
12+
13+
# Github developer token required.
14+
# Ensure these are uncommented and in secrets.py or .env
15+
# "Github_username": "Your Github Username",
16+
# "Github_token": "Your long API token",
17+
18+
# Initialize WiFi Pool (There can be only 1 pool & top of script)
19+
pool = socketpool.SocketPool(wifi.radio)
20+
21+
# Time between API refreshes
22+
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
23+
sleep_time = 900
24+
25+
try:
26+
from secrets import secrets
27+
except ImportError:
28+
print("Secrets File Import Error")
29+
raise
30+
31+
if sleep_time < 60:
32+
sleep_time_conversion = "seconds"
33+
sleep_int = sleep_time
34+
elif 60 <= sleep_time < 3600:
35+
sleep_int = sleep_time / 60
36+
sleep_time_conversion = "minutes"
37+
elif 3600 <= sleep_time < 86400:
38+
sleep_int = sleep_time / 60 / 60
39+
sleep_time_conversion = "hours"
40+
else:
41+
sleep_int = sleep_time / 60 / 60 / 24
42+
sleep_time_conversion = "days"
43+
44+
github_header = {"Authorization": " token " + secrets["Github_token"]}
45+
GH_SOURCE = "https://api.github.com/users/" + secrets["Github_username"]
46+
47+
# Connect to Wi-Fi
48+
print("\n===============================")
49+
print("Connecting to WiFi...")
50+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
51+
while not wifi.radio.ipv4_address:
52+
try:
53+
wifi.radio.connect(secrets["ssid"], secrets["password"])
54+
except ConnectionError as e:
55+
print("Connection Error:", e)
56+
print("Retrying in 10 seconds")
57+
time.sleep(10)
58+
gc.collect()
59+
print("Connected!\n")
60+
61+
while True:
62+
try:
63+
print("\nAttempting to GET GITHUB Stats!") # --------------------------------
64+
# Print Request to Serial
65+
debug_request = False # Set true to see full request
66+
if debug_request:
67+
print("Full API GET URL: ", GH_SOURCE)
68+
print("===============================")
69+
try:
70+
github_response = requests.get(url=GH_SOURCE, headers=github_header).json()
71+
except ConnectionError as e:
72+
print("Connection Error:", e)
73+
print("Retrying in 10 seconds")
74+
75+
# Print Response to Serial
76+
debug_response = False # Set true to see full response
77+
if debug_response:
78+
dump_object = json.dumps(github_response)
79+
print("JSON Dump: ", dump_object)
80+
81+
# Print Keys to Serial
82+
gh_debug_keys = True # Set True to print Serial data
83+
if gh_debug_keys:
84+
85+
github_id = github_response["id"]
86+
print("UserID: ", github_id)
87+
88+
github_username = github_response["name"]
89+
print("Username: ", github_username)
90+
91+
github_followers = github_response["followers"]
92+
print("Followers: ", github_followers)
93+
94+
print("Monotonic: ", time.monotonic())
95+
96+
print("\nFinished!")
97+
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion))
98+
print("===============================")
99+
gc.collect()
100+
101+
except (ValueError, RuntimeError) as e:
102+
print("Failed to get data, retrying\n", e)
103+
time.sleep(60)
104+
continue
105+
time.sleep(sleep_time)

0 commit comments

Comments
 (0)