-
Notifications
You must be signed in to change notification settings - Fork 35
Add API examples for various social media sites #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
90f7d1a
2c38b16
6985b33
ddae8af
e52c73a
898bc18
918aeaa
e76d9fa
7004327
c662605
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries | ||
# SPDX-License-Identifier: MIT | ||
# Coded for Circuit Python 8.0 | ||
"""DJDevon3 Adafruit Feather ESP32-S2 Adafruit_Discord_Active_Users_Example""" | ||
import gc | ||
import re | ||
import time | ||
import ssl | ||
import json | ||
import wifi | ||
import socketpool | ||
import adafruit_requests | ||
|
||
# No user or token required, web scrape. | ||
|
||
# Initialize WiFi Pool (There can be only 1 pool & top of script) | ||
pool = socketpool.SocketPool(wifi.radio) | ||
|
||
# Time between API refreshes | ||
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour | ||
sleep_time = 900 | ||
|
||
try: | ||
from secrets import secrets | ||
except ImportError: | ||
print("Secrets File Import Error") | ||
raise | ||
|
||
if sleep_time < 60: | ||
sleep_time_conversion = "seconds" | ||
sleep_int = sleep_time | ||
elif 60 <= sleep_time < 3600: | ||
sleep_int = sleep_time / 60 | ||
sleep_time_conversion = "minutes" | ||
elif 3600 <= sleep_time < 86400: | ||
sleep_int = sleep_time / 60 / 60 | ||
sleep_time_conversion = "hours" | ||
else: | ||
sleep_int = sleep_time / 60 / 60 / 24 | ||
sleep_time_conversion = "days" | ||
|
||
# https://img.shields.io/discord/327254708534116352.svg | ||
ADA_DISCORD_SVG = "https://img.shields.io/discord/327254708534116352.json" | ||
|
||
# Connect to Wi-Fi | ||
print("\n===============================") | ||
print("Connecting to WiFi...") | ||
requests = adafruit_requests.Session(pool, ssl.create_default_context()) | ||
while not wifi.radio.ipv4_address: | ||
try: | ||
wifi.radio.connect(secrets["ssid"], secrets["password"]) | ||
except ConnectionError as e: | ||
print("Connection Error:", e) | ||
print("Retrying in 10 seconds") | ||
time.sleep(10) | ||
gc.collect() | ||
print("Connected!\n") | ||
|
||
while True: | ||
try: | ||
print("\nAttempting to GET DISCORD SVG!") # -------------------------------- | ||
# Print Request to Serial | ||
debug_request = True # Set true to see full request | ||
if debug_request: | ||
print("Full API GET URL: ", ADA_DISCORD_SVG) | ||
print("===============================") | ||
try: | ||
ada_SVG_response = requests.get(ADA_DISCORD_SVG).json() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one can get rid of svg in the name of the variable as well. it could switch to json, or use 'discord' so that it's not tied to a specific type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes I originally started naming everything SVG and about halfway through found the JSON file. Forgot to rename everything. Will do. BTW I never did figure out how to work with the SVG directly. Typed in .json at the end of the filename in a fit of hair pulling desperation... and like a miracle it actually worked. Felt like I'd found a hidden cheat code. :P |
||
except ConnectionError as e: | ||
print("Connection Error:", e) | ||
print("Retrying in 10 seconds") | ||
|
||
# Print Full JSON to Serial | ||
full_ada_SVG_json_response = True # Change to true to see full response | ||
if full_ada_SVG_json_response: | ||
ada_SVG_dump_object = json.dumps(ada_SVG_response) | ||
print("JSON Dump: ", ada_SVG_dump_object) | ||
|
||
# Print Debugging to Serial | ||
ada_SVG_debug = True # Set to True to print Serial data | ||
if ada_SVG_debug: | ||
ada_SVG_users = ada_SVG_response["value"] | ||
print("SVG Value: ", ada_SVG_users) | ||
regex = " online" | ||
replace_with_nothing = "" | ||
regex_users = re.sub(regex, replace_with_nothing, ada_SVG_users) | ||
print("Regex Value: ", regex_users) | ||
print("Monotonic: ", time.monotonic()) | ||
|
||
print("\nFinished!") | ||
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) | ||
print("===============================") | ||
gc.collect() | ||
|
||
except (ValueError, RuntimeError) as e: | ||
print("Failed to get data, retrying\n", e) | ||
time.sleep(60) | ||
continue | ||
time.sleep(sleep_time) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries | ||
# SPDX-License-Identifier: MIT | ||
# Coded for Circuit Python 8.0 | ||
"""DJDevon3 Adafruit Feather ESP32-S2 Discord_API_Example""" | ||
import gc | ||
import time | ||
import ssl | ||
import json | ||
import wifi | ||
import socketpool | ||
import adafruit_requests | ||
|
||
# Web scrape authorization key required | ||
# Learn how: https://github.com/lorenz234/Discord-Data-Scraping | ||
|
||
# Ensure these are uncommented and in secrets.py or .env | ||
# "Discord_Adafruit_Channel": "327254708534116352", # Adafruit Channel ID | ||
# "Discord_Authorization": "Discord Authorization from browser console" | ||
|
||
# Initialize WiFi Pool (There can be only 1 pool & top of script) | ||
pool = socketpool.SocketPool(wifi.radio) | ||
|
||
# Time between API refreshes | ||
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour | ||
sleep_time = 900 | ||
|
||
try: | ||
from secrets import secrets | ||
except ImportError: | ||
print("Secrets File Import Error") | ||
raise | ||
|
||
if sleep_time < 60: | ||
sleep_time_conversion = "seconds" | ||
sleep_int = sleep_time | ||
elif 60 <= sleep_time < 3600: | ||
sleep_int = sleep_time / 60 | ||
sleep_time_conversion = "minutes" | ||
elif 3600 <= sleep_time < 86400: | ||
sleep_int = sleep_time / 60 / 60 | ||
sleep_time_conversion = "hours" | ||
else: | ||
sleep_int = sleep_time / 60 / 60 / 24 | ||
sleep_time_conversion = "days" | ||
|
||
discord_header = {"Authorization": "" + secrets["Discord_Authorization"]} | ||
ADA_SOURCE = ( | ||
"https://discord.com/api/v10/guilds/" | ||
+ secrets["Discord_Adafruit_Channel"] | ||
+ "/preview" | ||
) | ||
|
||
# Connect to Wi-Fi | ||
print("\n===============================") | ||
print("Connecting to WiFi...") | ||
requests = adafruit_requests.Session(pool, ssl.create_default_context()) | ||
while not wifi.radio.ipv4_address: | ||
try: | ||
wifi.radio.connect(secrets["ssid"], secrets["password"]) | ||
except ConnectionError as e: | ||
print("Connection Error:", e) | ||
print("Retrying in 10 seconds") | ||
time.sleep(10) | ||
gc.collect() | ||
print("Connected!\n") | ||
|
||
while True: | ||
try: | ||
print( | ||
"\nAttempting to GET DISCORD PREVIEW!" | ||
) # -------------------------------- | ||
# Print Request to Serial | ||
debug_request = False # Set true to see full request | ||
if debug_request: | ||
print("Full API GET URL: ", ADA_SOURCE) | ||
print("===============================") | ||
try: | ||
ada_res = requests.get(url=ADA_SOURCE, headers=discord_header).json() | ||
except ConnectionError as e: | ||
print("Connection Error:", e) | ||
print("Retrying in 10 seconds") | ||
|
||
# Print Full JSON to Serial | ||
discord_debug_response = False # Change to true to see full response | ||
if discord_debug_response: | ||
ada_discord_dump_object = json.dumps(ada_res) | ||
print("JSON Dump: ", ada_discord_dump_object) | ||
|
||
# Print keys to Serial | ||
discord_debug_keys = True # Set to True to print Serial data | ||
if discord_debug_keys: | ||
|
||
ada_discord_all_members = ada_res["approximate_member_count"] | ||
print("Members: ", ada_discord_all_members) | ||
|
||
ada_discord_all_members_online = ada_res["approximate_presence_count"] | ||
print("Online: ", ada_discord_all_members_online) | ||
|
||
print("Monotonic: ", time.monotonic()) | ||
|
||
print("\nFinished!") | ||
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) | ||
print("===============================") | ||
gc.collect() | ||
|
||
except (ValueError, RuntimeError) as e: | ||
print("Failed to get data, retrying\n", e) | ||
time.sleep(60) | ||
continue | ||
time.sleep(sleep_time) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries | ||
# SPDX-License-Identifier: MIT | ||
# Coded for Circuit Python 8.0 | ||
"""DJDevon3 Adafruit Feather ESP32-S2 Github_API_Example""" | ||
import gc | ||
import time | ||
import ssl | ||
import json | ||
import wifi | ||
import socketpool | ||
import adafruit_requests | ||
|
||
# Github developer token required. | ||
# Ensure these are uncommented and in secrets.py or .env | ||
# "Github_username": "Your Github Username", | ||
# "Github_token": "Your long API token", | ||
|
||
# Initialize WiFi Pool (There can be only 1 pool & top of script) | ||
pool = socketpool.SocketPool(wifi.radio) | ||
|
||
# Time between API refreshes | ||
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour | ||
sleep_time = 900 | ||
|
||
try: | ||
from secrets import secrets | ||
except ImportError: | ||
print("Secrets File Import Error") | ||
raise | ||
|
||
if sleep_time < 60: | ||
sleep_time_conversion = "seconds" | ||
sleep_int = sleep_time | ||
elif 60 <= sleep_time < 3600: | ||
sleep_int = sleep_time / 60 | ||
sleep_time_conversion = "minutes" | ||
elif 3600 <= sleep_time < 86400: | ||
sleep_int = sleep_time / 60 / 60 | ||
sleep_time_conversion = "hours" | ||
else: | ||
sleep_int = sleep_time / 60 / 60 / 24 | ||
sleep_time_conversion = "days" | ||
|
||
github_header = {"Authorization": " token " + secrets["Github_token"]} | ||
GH_SOURCE = "https://api.github.com/users/" + secrets["Github_username"] | ||
|
||
# Connect to Wi-Fi | ||
print("\n===============================") | ||
print("Connecting to WiFi...") | ||
requests = adafruit_requests.Session(pool, ssl.create_default_context()) | ||
while not wifi.radio.ipv4_address: | ||
try: | ||
wifi.radio.connect(secrets["ssid"], secrets["password"]) | ||
except ConnectionError as e: | ||
print("Connection Error:", e) | ||
print("Retrying in 10 seconds") | ||
time.sleep(10) | ||
gc.collect() | ||
print("Connected!\n") | ||
|
||
while True: | ||
try: | ||
print("\nAttempting to GET GITHUB Stats!") # -------------------------------- | ||
# Print Request to Serial | ||
debug_request = False # Set true to see full request | ||
if debug_request: | ||
print("Full API GET URL: ", GH_SOURCE) | ||
print("===============================") | ||
try: | ||
github_response = requests.get(url=GH_SOURCE, headers=github_header).json() | ||
except ConnectionError as e: | ||
print("Connection Error:", e) | ||
print("Retrying in 10 seconds") | ||
|
||
# Print Response to Serial | ||
debug_response = False # Set true to see full response | ||
if debug_response: | ||
dump_object = json.dumps(github_response) | ||
print("JSON Dump: ", dump_object) | ||
|
||
# Print Keys to Serial | ||
gh_debug_keys = True # Set True to print Serial data | ||
if gh_debug_keys: | ||
|
||
github_id = github_response["id"] | ||
print("UserID: ", github_id) | ||
|
||
github_username = github_response["name"] | ||
print("Username: ", github_username) | ||
|
||
github_followers = github_response["followers"] | ||
print("Followers: ", github_followers) | ||
|
||
print("Monotonic: ", time.monotonic()) | ||
|
||
print("\nFinished!") | ||
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) | ||
print("===============================") | ||
gc.collect() | ||
|
||
except (ValueError, RuntimeError) as e: | ||
print("Failed to get data, retrying\n", e) | ||
time.sleep(60) | ||
continue | ||
time.sleep(sleep_time) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice find! I didn't know there was JSON data available directly. Since it's not using an svg anymore lets change the variable name to
ADA_DISCORD_JSON
or something that matches the new type instead.