From 90f7d1aaa4c96e6993dcdb59da9db37d1b8dfedc Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Sat, 22 Oct 2022 23:26:08 -0400 Subject: [PATCH 01/10] Create requests_api_youtube.py Example script for YouTube API. Includes some error correction and time based requests. Returns basic channel stats. Amount of serial prints configurable with debugger booleans. --- examples/requests_api_youtube.py | 120 +++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 examples/requests_api_youtube.py diff --git a/examples/requests_api_youtube.py b/examples/requests_api_youtube.py new file mode 100644 index 0000000..3393a64 --- /dev/null +++ b/examples/requests_api_youtube.py @@ -0,0 +1,120 @@ +# SPDX-FileCopyrightText: 2022 DJDevon3 +# SPDX-License-Identifier: MIT +# Coded for Circuit Python 8.0 +"""DJDevon3 Adafruit Feather ESP32-S2 YouTube_API_Example""" +# pylint: disable=line-too-long +import gc +import time +import ssl +import wifi +import json +import socketpool +import adafruit_requests + +# Ensure these are uncommented and in secrets.py or .env +# "YT_username": "Your YouTube Username", +# "YT_token" : "Your long API developer 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" + +# https://youtube.googleapis.com/youtube/v3/channels?part=statistics&forUsername=[YOUR_USERNAME]&key=[YOUR_API_KEY] +YT_SOURCE = ( + "https://youtube.googleapis.com/youtube/v3/channels?" + + "part=statistics" + + "&forUsername=" + + secrets["YT_username"] + + "&key=" + + secrets["YT_token"] +) + +# 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("Attempting to GET YouTube Stats!") # ---------------------------------- + debug_request = False # Set true to see full request + if debug_request: + print("Full API GET URL: ", YT_SOURCE) + print("===============================") + try: + response = requests.get(YT_SOURCE).json() + except ConnectionError as e: + print("Connection Error:", e) + print("Retrying in 10 seconds") + + # Print Full JSON to Serial + debug_response = False # Set true to see full response + if debug_response: + dump_object = json.dumps(response) + print("JSON Dump: ", dump_object) + + # Print to Serial + yt_debug_keys = True # Set to True to print Serial data + if yt_debug_keys: + print("Matching Results: ", response['pageInfo']['totalResults']) + + YT_request_kind = response['items'][0]['kind'] + print("Request Kind: ", YT_request_kind) + + YT_response_kind = response['kind'] + print("Response Kind: ", YT_response_kind) + + YT_channel_id = response['items'][0]['id'] + print("Channel ID: ", YT_channel_id) + + YT_videoCount = response['items'][0]['statistics']['videoCount'] + print("Videos: ", YT_videoCount) + + YT_viewCount = response['items'][0]['statistics']['viewCount'] + print("Views: ", YT_viewCount) + + YT_subsCount = response['items'][0]['statistics']['subscriberCount'] + print("Subscribers: ", YT_subsCount) + print("Monotonic: ", time.monotonic()) + + print("\nFinished!") + print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) + print("===============================") + gc.collect() + # pylint: disable=broad-except + except (ValueError, RuntimeError) as e: + print("Failed to get data, retrying\n", e) + time.sleep(60) + continue + time.sleep(sleep_time) From 2c38b162d09d0a86abcdf4ae8628eb2d5cc3e60e Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Sun, 23 Oct 2022 00:02:12 -0400 Subject: [PATCH 02/10] Create requests_api_twitter.py Example script for Twitter API. Includes some error correction and time based requests. Returns basic stats. Amount of serial prints configurable with debugger booleans. --- examples/requests_api_twitter.py | 119 +++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 examples/requests_api_twitter.py diff --git a/examples/requests_api_twitter.py b/examples/requests_api_twitter.py new file mode 100644 index 0000000..4903b6f --- /dev/null +++ b/examples/requests_api_twitter.py @@ -0,0 +1,119 @@ +# SPDX-FileCopyrightText: 2022 DJDevon3 +# SPDX-License-Identifier: MIT +# Coded for Circuit Python 8.0 +"""DJDevon3 Adafruit Feather ESP32-S2 Twitter_API_Example""" +# pylint: disable=line-too-long +import gc +import time +import ssl +import wifi +import json +import socketpool +import adafruit_requests + +# Twitter developer account bearer token required. +# Ensure these are uncommented and in secrets.py or .env +# "TW_userid": "Your Twitter user id", # numerical id not username +# "TW_bearer_token": "Your long API Bearer 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" + +# Used with any Twitter 0auth request. +twitter_header = {'Authorization': 'Bearer ' + secrets["TW_bearer_token"]} +TW_SOURCE = ( + "https://api.twitter.com/2/users/" + + secrets["TW_userid"] + + "?user.fields=public_metrics,created_at,pinned_tweet_id" + + "&expansions=pinned_tweet_id" + + "&tweet.fields=created_at,public_metrics,source,context_annotations,entities" +) + +# 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 Twitter Stats!") # -------------------------------- + debug_request = False # Set true to see full request + if debug_request: + print("Full API GET URL: ", TW_SOURCE) + print("===============================") + try: + twitter_response = requests.get(url=TW_SOURCE, headers=twitter_header) + tw_json = twitter_response.json() + except ConnectionError as e: + print("Connection Error:", e) + print("Retrying in 10 seconds") + + # Print Full JSON to Serial + debug_response = False # Set true to see full response + if debug_response: + dump_object = json.dumps(tw_json) + print("JSON Dump: ", dump_object) + + # Print to Serial + tw_debug_keys = True # Set true to print Serial data + if tw_debug_keys: + + tw_userid = tw_json['data']['id'] + print("User ID: ", tw_userid) + + tw_username = tw_json['data']['name'] + print("Name: ", tw_username) + + tw_join_date = tw_json['data']['created_at'] + print("Member Since: ", tw_join_date) + + tw_tweets = tw_json['data']['public_metrics']['tweet_count'] + print("Tweets: ", tw_tweets) + + tw_followers = tw_json['data']['public_metrics']['followers_count'] + print("Followers: ", tw_followers) + + print("Monotonic: ", time.monotonic()) + + print("\nFinished!") + print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) + print("===============================") + gc.collect() + # pylint: disable=broad-except + except (ValueError, RuntimeError) as e: + print("Failed to get data, retrying\n", e) + time.sleep(60) + continue + time.sleep(sleep_time) From 6985b3315ceeb935ea21950f6d8af1571a5929db Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Sun, 23 Oct 2022 00:26:14 -0400 Subject: [PATCH 03/10] Create requests_api_github.py Example script for Github API. Includes some error correction and time based requests. Returns basic stats. Amount of serial prints configurable with debugger booleans. --- examples/requests_api_github.py | 109 ++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 examples/requests_api_github.py diff --git a/examples/requests_api_github.py b/examples/requests_api_github.py new file mode 100644 index 0000000..5016f8e --- /dev/null +++ b/examples/requests_api_github.py @@ -0,0 +1,109 @@ +# SPDX-FileCopyrightText: 2022 DJDevon3 +# SPDX-License-Identifier: MIT +# Coded for Circuit Python 8.0 +"""DJDevon3 Adafruit Feather ESP32-S2 Github_API_Example""" +# pylint: disable=line-too-long +import gc +import time +import ssl +import wifi +import json +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() + # pylint: disable=broad-except + except (ValueError, RuntimeError) as e: + print("Failed to get data, retrying\n", e) + time.sleep(60) + continue + time.sleep(sleep_time) From ddae8af3516ae8cbc61e232635f27b0af218e892 Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Sun, 23 Oct 2022 00:38:11 -0400 Subject: [PATCH 04/10] 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. --- ...requests_adafruit_discord_active_online.py | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 examples/requests_adafruit_discord_active_online.py diff --git a/examples/requests_adafruit_discord_active_online.py b/examples/requests_adafruit_discord_active_online.py new file mode 100644 index 0000000..6c87978 --- /dev/null +++ b/examples/requests_adafruit_discord_active_online.py @@ -0,0 +1,102 @@ +# SPDX-FileCopyrightText: 2022 DJDevon3 +# SPDX-License-Identifier: MIT +# Coded for Circuit Python 8.0 +"""DJDevon3 Adafruit Feather ESP32-S2 Adafruit_Discord_Active_Users_Example""" +# pylint: disable=line-too-long +import gc +import re +import time +import ssl +import wifi +import json +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() + 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() + # pylint: disable=broad-except + except (ValueError, RuntimeError) as e: + print("Failed to get data, retrying\n", e) + time.sleep(60) + continue + time.sleep(sleep_time) From e52c73aa1bb73b943eb201d8dd6a92f089e0f297 Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Sun, 23 Oct 2022 00:55:31 -0400 Subject: [PATCH 05/10] Create requests_api_discord.py Example script for Discord API. Includes some error correction and time based requests. Returns basic stats. Amount of serial prints configurable with debugger booleans. --- examples/requests_api_discord.py | 109 +++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 examples/requests_api_discord.py diff --git a/examples/requests_api_discord.py b/examples/requests_api_discord.py new file mode 100644 index 0000000..1f0f575 --- /dev/null +++ b/examples/requests_api_discord.py @@ -0,0 +1,109 @@ +# SPDX-FileCopyrightText: 2022 DJDevon3 +# SPDX-License-Identifier: MIT +# Coded for Circuit Python 8.0 +"""DJDevon3 Adafruit Feather ESP32-S2 Discord_API_Example""" +# pylint: disable=line-too-long +import gc +import time +import ssl +import wifi +import json +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() + # pylint: disable=broad-except + except (ValueError, RuntimeError) as e: + print("Failed to get data, retrying\n", e) + time.sleep(60) + continue + time.sleep(sleep_time) From 898bc18cb69d9cfc1755d974f69d465244160db3 Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Sun, 23 Oct 2022 10:50:29 -0400 Subject: [PATCH 06/10] fixing minor pre-commit issues reordered json before wifi import --- examples/requests_adafruit_discord_active_online.py | 2 +- examples/requests_api_discord.py | 2 +- examples/requests_api_github.py | 2 +- examples/requests_api_twitter.py | 2 +- examples/requests_api_youtube.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/requests_adafruit_discord_active_online.py b/examples/requests_adafruit_discord_active_online.py index 6c87978..6d7da30 100644 --- a/examples/requests_adafruit_discord_active_online.py +++ b/examples/requests_adafruit_discord_active_online.py @@ -7,8 +7,8 @@ import re import time import ssl -import wifi import json +import wifi import socketpool import adafruit_requests diff --git a/examples/requests_api_discord.py b/examples/requests_api_discord.py index 1f0f575..97086ce 100644 --- a/examples/requests_api_discord.py +++ b/examples/requests_api_discord.py @@ -6,8 +6,8 @@ import gc import time import ssl -import wifi import json +import wifi import socketpool import adafruit_requests diff --git a/examples/requests_api_github.py b/examples/requests_api_github.py index 5016f8e..089e3af 100644 --- a/examples/requests_api_github.py +++ b/examples/requests_api_github.py @@ -6,8 +6,8 @@ import gc import time import ssl -import wifi import json +import wifi import socketpool import adafruit_requests diff --git a/examples/requests_api_twitter.py b/examples/requests_api_twitter.py index 4903b6f..9a72892 100644 --- a/examples/requests_api_twitter.py +++ b/examples/requests_api_twitter.py @@ -6,8 +6,8 @@ import gc import time import ssl -import wifi import json +import wifi import socketpool import adafruit_requests diff --git a/examples/requests_api_youtube.py b/examples/requests_api_youtube.py index 3393a64..09299d9 100644 --- a/examples/requests_api_youtube.py +++ b/examples/requests_api_youtube.py @@ -6,8 +6,8 @@ import gc import time import ssl -import wifi import json +import wifi import socketpool import adafruit_requests From 918aeaa3f9fe90780521dcc6deafdedf2e789de1 Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Sun, 23 Oct 2022 11:19:54 -0400 Subject: [PATCH 07/10] ran black on individual files first pre-commit with black formatting --- ...requests_adafruit_discord_active_online.py | 21 +++++------ examples/requests_api_discord.py | 29 ++++++++------- examples/requests_api_github.py | 36 ++++++++---------- examples/requests_api_twitter.py | 37 +++++++++---------- examples/requests_api_youtube.py | 25 ++++++------- 5 files changed, 70 insertions(+), 78 deletions(-) diff --git a/examples/requests_adafruit_discord_active_online.py b/examples/requests_adafruit_discord_active_online.py index 6d7da30..c7ad471 100644 --- a/examples/requests_adafruit_discord_active_online.py +++ b/examples/requests_adafruit_discord_active_online.py @@ -1,8 +1,7 @@ -# SPDX-FileCopyrightText: 2022 DJDevon3 +# 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""" -# pylint: disable=line-too-long import gc import re import time @@ -41,9 +40,7 @@ sleep_time_conversion = "days" # https://img.shields.io/discord/327254708534116352.svg -ADA_DISCORD_SVG = ( - "https://img.shields.io/discord/327254708534116352.json" -) +ADA_DISCORD_SVG = "https://img.shields.io/discord/327254708534116352.json" # Connect to Wi-Fi print("\n===============================") @@ -51,7 +48,7 @@ requests = adafruit_requests.Session(pool, ssl.create_default_context()) while not wifi.radio.ipv4_address: try: - wifi.radio.connect(secrets['ssid'], secrets['password']) + wifi.radio.connect(secrets["ssid"], secrets["password"]) except ConnectionError as e: print("Connection Error:", e) print("Retrying in 10 seconds") @@ -72,17 +69,17 @@ 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 + 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 + ada_SVG_debug = True # Set to True to print Serial data if ada_SVG_debug: - ada_SVG_users = ada_SVG_response['value'] + ada_SVG_users = ada_SVG_response["value"] print("SVG Value: ", ada_SVG_users) regex = " online" replace_with_nothing = "" @@ -94,7 +91,7 @@ print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) print("===============================") gc.collect() - # pylint: disable=broad-except + except (ValueError, RuntimeError) as e: print("Failed to get data, retrying\n", e) time.sleep(60) diff --git a/examples/requests_api_discord.py b/examples/requests_api_discord.py index 97086ce..2671c27 100644 --- a/examples/requests_api_discord.py +++ b/examples/requests_api_discord.py @@ -1,8 +1,7 @@ -# SPDX-FileCopyrightText: 2022 DJDevon3 +# 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""" -# pylint: disable=line-too-long import gc import time import ssl @@ -44,10 +43,10 @@ sleep_int = sleep_time / 60 / 60 / 24 sleep_time_conversion = "days" -discord_header = {'Authorization': '' + secrets['Discord_Authorization']} +discord_header = {"Authorization": "" + secrets["Discord_Authorization"]} ADA_SOURCE = ( "https://discord.com/api/v10/guilds/" - + secrets['Discord_Adafruit_Channel'] + + secrets["Discord_Adafruit_Channel"] + "/preview" ) @@ -57,7 +56,7 @@ requests = adafruit_requests.Session(pool, ssl.create_default_context()) while not wifi.radio.ipv4_address: try: - wifi.radio.connect(secrets['ssid'], secrets['password']) + wifi.radio.connect(secrets["ssid"], secrets["password"]) except ConnectionError as e: print("Connection Error:", e) print("Retrying in 10 seconds") @@ -67,7 +66,9 @@ while True: try: - print("\nAttempting to GET DISCORD PREVIEW!") # -------------------------------- + print( + "\nAttempting to GET DISCORD PREVIEW!" + ) # -------------------------------- # Print Request to Serial debug_request = False # Set true to see full request if debug_request: @@ -78,30 +79,30 @@ 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'] + + 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'] + + 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() - # pylint: disable=broad-except + except (ValueError, RuntimeError) as e: print("Failed to get data, retrying\n", e) time.sleep(60) diff --git a/examples/requests_api_github.py b/examples/requests_api_github.py index 089e3af..4775a54 100644 --- a/examples/requests_api_github.py +++ b/examples/requests_api_github.py @@ -1,8 +1,7 @@ -# SPDX-FileCopyrightText: 2022 DJDevon3 +# 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""" -# pylint: disable=line-too-long import gc import time import ssl @@ -12,7 +11,7 @@ import adafruit_requests # Github developer token required. -# Ensure these are uncommented and in secrets.py or .env +# Ensure these are uncommented and in secrets.py or .env # "Github_username": "Your Github Username", # "Github_token": "Your long API token", @@ -42,11 +41,8 @@ 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"] -) +github_header = {"Authorization": " token " + secrets["Github_token"]} +GH_SOURCE = "https://api.github.com/users/" + secrets["Github_username"] # Connect to Wi-Fi print("\n===============================") @@ -54,7 +50,7 @@ requests = adafruit_requests.Session(pool, ssl.create_default_context()) while not wifi.radio.ipv4_address: try: - wifi.radio.connect(secrets['ssid'], secrets['password']) + wifi.radio.connect(secrets["ssid"], secrets["password"]) except ConnectionError as e: print("Connection Error:", e) print("Retrying in 10 seconds") @@ -75,33 +71,33 @@ 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 + gh_debug_keys = True # Set True to print Serial data if gh_debug_keys: - - github_id = github_response['id'] + + github_id = github_response["id"] print("UserID: ", github_id) - - github_username = github_response['name'] + + github_username = github_response["name"] print("Username: ", github_username) - - github_followers = github_response['followers'] + + 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() - # pylint: disable=broad-except + except (ValueError, RuntimeError) as e: print("Failed to get data, retrying\n", e) time.sleep(60) diff --git a/examples/requests_api_twitter.py b/examples/requests_api_twitter.py index 9a72892..6d0645e 100644 --- a/examples/requests_api_twitter.py +++ b/examples/requests_api_twitter.py @@ -1,8 +1,7 @@ -# SPDX-FileCopyrightText: 2022 DJDevon3 +# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries # SPDX-License-Identifier: MIT # Coded for Circuit Python 8.0 """DJDevon3 Adafruit Feather ESP32-S2 Twitter_API_Example""" -# pylint: disable=line-too-long import gc import time import ssl @@ -12,7 +11,7 @@ import adafruit_requests # Twitter developer account bearer token required. -# Ensure these are uncommented and in secrets.py or .env +# Ensure these are uncommented and in secrets.py or .env # "TW_userid": "Your Twitter user id", # numerical id not username # "TW_bearer_token": "Your long API Bearer token", @@ -43,7 +42,7 @@ sleep_time_conversion = "days" # Used with any Twitter 0auth request. -twitter_header = {'Authorization': 'Bearer ' + secrets["TW_bearer_token"]} +twitter_header = {"Authorization": "Bearer " + secrets["TW_bearer_token"]} TW_SOURCE = ( "https://api.twitter.com/2/users/" + secrets["TW_userid"] @@ -58,7 +57,7 @@ requests = adafruit_requests.Session(pool, ssl.create_default_context()) while not wifi.radio.ipv4_address: try: - wifi.radio.connect(secrets['ssid'], secrets['password']) + wifi.radio.connect(secrets["ssid"], secrets["password"]) except ConnectionError as e: print("Connection Error:", e) print("Retrying in 10 seconds") @@ -79,39 +78,39 @@ except ConnectionError as e: print("Connection Error:", e) print("Retrying in 10 seconds") - + # Print Full JSON to Serial debug_response = False # Set true to see full response if debug_response: dump_object = json.dumps(tw_json) print("JSON Dump: ", dump_object) - + # Print to Serial tw_debug_keys = True # Set true to print Serial data if tw_debug_keys: - - tw_userid = tw_json['data']['id'] + + tw_userid = tw_json["data"]["id"] print("User ID: ", tw_userid) - - tw_username = tw_json['data']['name'] + + tw_username = tw_json["data"]["name"] print("Name: ", tw_username) - - tw_join_date = tw_json['data']['created_at'] + + tw_join_date = tw_json["data"]["created_at"] print("Member Since: ", tw_join_date) - - tw_tweets = tw_json['data']['public_metrics']['tweet_count'] + + tw_tweets = tw_json["data"]["public_metrics"]["tweet_count"] print("Tweets: ", tw_tweets) - - tw_followers = tw_json['data']['public_metrics']['followers_count'] + + tw_followers = tw_json["data"]["public_metrics"]["followers_count"] print("Followers: ", tw_followers) - + print("Monotonic: ", time.monotonic()) print("\nFinished!") print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) print("===============================") gc.collect() - # pylint: disable=broad-except + except (ValueError, RuntimeError) as e: print("Failed to get data, retrying\n", e) time.sleep(60) diff --git a/examples/requests_api_youtube.py b/examples/requests_api_youtube.py index 09299d9..5fdacbb 100644 --- a/examples/requests_api_youtube.py +++ b/examples/requests_api_youtube.py @@ -1,8 +1,7 @@ -# SPDX-FileCopyrightText: 2022 DJDevon3 +# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries # SPDX-License-Identifier: MIT # Coded for Circuit Python 8.0 """DJDevon3 Adafruit Feather ESP32-S2 YouTube_API_Example""" -# pylint: disable=line-too-long import gc import time import ssl @@ -11,7 +10,7 @@ import socketpool import adafruit_requests -# Ensure these are uncommented and in secrets.py or .env +# Ensure these are uncommented and in secrets.py or .env # "YT_username": "Your YouTube Username", # "YT_token" : "Your long API developer token", @@ -57,7 +56,7 @@ requests = adafruit_requests.Session(pool, ssl.create_default_context()) while not wifi.radio.ipv4_address: try: - wifi.radio.connect(secrets['ssid'], secrets['password']) + wifi.radio.connect(secrets["ssid"], secrets["password"]) except ConnectionError as e: print("Connection Error:", e) print("Retrying in 10 seconds") @@ -87,24 +86,24 @@ # Print to Serial yt_debug_keys = True # Set to True to print Serial data if yt_debug_keys: - print("Matching Results: ", response['pageInfo']['totalResults']) + print("Matching Results: ", response["pageInfo"]["totalResults"]) - YT_request_kind = response['items'][0]['kind'] + YT_request_kind = response["items"][0]["kind"] print("Request Kind: ", YT_request_kind) - - YT_response_kind = response['kind'] + + YT_response_kind = response["kind"] print("Response Kind: ", YT_response_kind) - YT_channel_id = response['items'][0]['id'] + YT_channel_id = response["items"][0]["id"] print("Channel ID: ", YT_channel_id) - YT_videoCount = response['items'][0]['statistics']['videoCount'] + YT_videoCount = response["items"][0]["statistics"]["videoCount"] print("Videos: ", YT_videoCount) - YT_viewCount = response['items'][0]['statistics']['viewCount'] + YT_viewCount = response["items"][0]["statistics"]["viewCount"] print("Views: ", YT_viewCount) - YT_subsCount = response['items'][0]['statistics']['subscriberCount'] + YT_subsCount = response["items"][0]["statistics"]["subscriberCount"] print("Subscribers: ", YT_subsCount) print("Monotonic: ", time.monotonic()) @@ -112,7 +111,7 @@ print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) print("===============================") gc.collect() - # pylint: disable=broad-except + except (ValueError, RuntimeError) as e: print("Failed to get data, retrying\n", e) time.sleep(60) From e76d9fa607ba1b9a32357e3da77a841a1c6be346 Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Wed, 9 Nov 2022 19:31:40 -0500 Subject: [PATCH 08/10] removed regex and SVG references Updated as recommended by FoamyGuy during his livestream. No more regex needed, using string replace. --- ...requests_adafruit_discord_active_online.py | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/examples/requests_adafruit_discord_active_online.py b/examples/requests_adafruit_discord_active_online.py index c7ad471..808cdaf 100644 --- a/examples/requests_adafruit_discord_active_online.py +++ b/examples/requests_adafruit_discord_active_online.py @@ -3,7 +3,6 @@ # 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 @@ -11,7 +10,8 @@ import socketpool import adafruit_requests -# No user or token required, web scrape. +# No user or token required, 100% JSON web scrape from SHIELDS.IO +# Adafruit uses Shields.IO to see online users # Initialize WiFi Pool (There can be only 1 pool & top of script) pool = socketpool.SocketPool(wifi.radio) @@ -39,8 +39,9 @@ sleep_int = sleep_time / 60 / 60 / 24 sleep_time_conversion = "days" +# Originally attempted to use SVG. Found JSON exists with same filename. # https://img.shields.io/discord/327254708534116352.svg -ADA_DISCORD_SVG = "https://img.shields.io/discord/327254708534116352.json" +ADA_DISCORD_JSON = "https://img.shields.io/discord/327254708534116352.json" # Connect to Wi-Fi print("\n===============================") @@ -58,33 +59,33 @@ while True: try: - print("\nAttempting to GET DISCORD SVG!") # -------------------------------- + print("\nAttempting to GET DISCORD SHIELD JSON!") # -------------------------------- # 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("Full API GET URL: ", ADA_DISCORD_JSON) print("===============================") try: - ada_SVG_response = requests.get(ADA_DISCORD_SVG).json() + ada_response = requests.get(ADA_DISCORD_JSON).json() 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) + full_ada_json_response = True # Change to true to see full response + if full_ada_json_response: + ada_dump_object = json.dumps(ada_response) + print("JSON Dump: ", ada_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" + ada_debug = True # Set to True to print Serial data + if ada_debug: + ada_users = ada_response["value"] + print("JSON Value: ", ada_users) + online_string = " online" replace_with_nothing = "" - regex_users = re.sub(regex, replace_with_nothing, ada_SVG_users) - print("Regex Value: ", regex_users) + string_replace_users = ada_users.replace(online_string, replace_with_nothing) + print("Replaced Value: ", string_replace_users) print("Monotonic: ", time.monotonic()) print("\nFinished!") From 70043274f599664cd9e2ac711fa26f1f6ed8542e Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Wed, 9 Nov 2022 21:24:47 -0500 Subject: [PATCH 09/10] ran black --- examples/requests_adafruit_discord_active_online.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/requests_adafruit_discord_active_online.py b/examples/requests_adafruit_discord_active_online.py index 808cdaf..0175593 100644 --- a/examples/requests_adafruit_discord_active_online.py +++ b/examples/requests_adafruit_discord_active_online.py @@ -59,7 +59,9 @@ while True: try: - print("\nAttempting to GET DISCORD SHIELD JSON!") # -------------------------------- + print( + "\nAttempting to GET DISCORD SHIELD JSON!" + ) # -------------------------------- # Print Request to Serial debug_request = True # Set true to see full request if debug_request: @@ -84,7 +86,9 @@ print("JSON Value: ", ada_users) online_string = " online" replace_with_nothing = "" - string_replace_users = ada_users.replace(online_string, replace_with_nothing) + string_replace_users = ada_users.replace( + online_string, replace_with_nothing + ) print("Replaced Value: ", string_replace_users) print("Monotonic: ", time.monotonic()) From c6626055cc7c5025c65e8807bc7287136fe73af3 Mon Sep 17 00:00:00 2001 From: DJDevon3 <49322231+DJDevon3@users.noreply.github.com> Date: Wed, 9 Nov 2022 21:56:41 -0500 Subject: [PATCH 10/10] move channel ID out of secrets It's public info so doesn't need to be in secrets.py --- examples/requests_api_discord.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/requests_api_discord.py b/examples/requests_api_discord.py index 2671c27..e233d8a 100644 --- a/examples/requests_api_discord.py +++ b/examples/requests_api_discord.py @@ -10,11 +10,11 @@ import socketpool import adafruit_requests -# Web scrape authorization key required +# Active Logged in User Account Required, no tokens required +# WEB SCRAPE authorization key required. Visit URL below. # 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 +# Ensure this is in secrets.py or .env # "Discord_Authorization": "Discord Authorization from browser console" # Initialize WiFi Pool (There can be only 1 pool & top of script) @@ -46,7 +46,7 @@ discord_header = {"Authorization": "" + secrets["Discord_Authorization"]} ADA_SOURCE = ( "https://discord.com/api/v10/guilds/" - + secrets["Discord_Adafruit_Channel"] + + "327254708534116352" # Adafruit Discord ID + "/preview" )