Skip to content

Commit 019f126

Browse files
committed
Merge branch 'main' into DJDevon3-openskynetwork_public
# Conflicts: # examples/wifi/expanded/requests_wifi_api_openskynetwork_private.py # examples/wifi/expanded/requests_wifi_api_openskynetwork_public.py
2 parents e9ca5b2 + 0c193c6 commit 019f126

14 files changed

+762
-624
lines changed

examples/wifi/expanded/requests_wifi_adafruit_discord_active_online.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# SPDX-License-Identifier: MIT
33
# Coded for Circuit Python 8.2.x
44
"""Discord Active Online Shields.IO Example"""
5-
# pylint: disable=import-error
65

76
import os
87
import time

examples/wifi/expanded/requests_wifi_api_discord.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# SPDX-License-Identifier: MIT
33
# Coded for Circuit Python 8.2.x
44
"""Discord Web Scrape Example"""
5-
# pylint: disable=import-error
65

76
import os
87
import time

examples/wifi/expanded/requests_wifi_api_fitbit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: MIT
33
# Coded for Circuit Python 8.2.x
44
"""Fitbit API Example"""
5-
# pylint: disable=import-error, disable=no-member
5+
# pylint: disable=no-member
66

77
import os
88
import time
Lines changed: 77 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,107 @@
1-
# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2024 DJDevon3
22
# 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 json
3+
# Coded for Circuit Python 9.x
4+
"""Github API Example"""
5+
76
import os
8-
import ssl
97
import time
108

11-
import socketpool
9+
import adafruit_connection_manager
1210
import wifi
1311

1412
import adafruit_requests
1513

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
14+
# Github developer token required.
15+
username = os.getenv("GITHUB_USERNAME")
16+
token = os.getenv("GITHUB_TOKEN")
2217

2318
# Get WiFi details, ensure these are setup in settings.toml
2419
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
2520
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
26-
# Github developer token required.
27-
github_username = os.getenv("Github_username")
28-
github_token = os.getenv("Github_token")
29-
30-
if sleep_time < 60:
31-
sleep_time_conversion = "seconds"
32-
sleep_int = sleep_time
33-
elif 60 <= sleep_time < 3600:
34-
sleep_int = sleep_time / 60
35-
sleep_time_conversion = "minutes"
36-
elif 3600 <= sleep_time < 86400:
37-
sleep_int = sleep_time / 60 / 60
38-
sleep_time_conversion = "hours"
39-
else:
40-
sleep_int = sleep_time / 60 / 60 / 24
41-
sleep_time_conversion = "days"
42-
43-
github_header = {"Authorization": " token " + github_token}
44-
GH_SOURCE = "https://api.github.com/users/" + github_username
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(ssid, 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")
21+
22+
# API Polling Rate
23+
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
24+
SLEEP_TIME = 900
25+
26+
# Set debug to True for full JSON response.
27+
# WARNING: may include visible credentials
28+
DEBUG = False
29+
30+
# Initalize Wifi, Socket Pool, Request Session
31+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
32+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
33+
requests = adafruit_requests.Session(pool, ssl_context)
34+
35+
GITHUB_HEADER = {"Authorization": " token " + token}
36+
GITHUB_SOURCE = "https://api.github.com/users/" + username
37+
38+
39+
def time_calc(input_time):
40+
"""Converts seconds to minutes/hours/days"""
41+
if input_time < 60:
42+
return f"{input_time:.0f} seconds"
43+
if input_time < 3600:
44+
return f"{input_time / 60:.0f} minutes"
45+
if input_time < 86400:
46+
return f"{input_time / 60 / 60:.0f} hours"
47+
return f"{input_time / 60 / 60 / 24:.1f} days"
48+
5949

6050
while True:
51+
# Connect to Wi-Fi
52+
print("\nConnecting to WiFi...")
53+
while not wifi.radio.ipv4_address:
54+
try:
55+
wifi.radio.connect(ssid, password)
56+
except ConnectionError as e:
57+
print("❌ Connection Error:", e)
58+
print("Retrying in 10 seconds")
59+
print("✅ Wifi!")
60+
6161
try:
62-
print("\nAttempting to GET GITHUB Stats!") # --------------------------------
63-
# Print Request to Serial
64-
debug_request = False # Set true to see full request
65-
if debug_request:
66-
print("Full API GET URL: ", GH_SOURCE)
67-
print("===============================")
62+
print(" | Attempting to GET Github JSON!")
6863
try:
69-
github_response = requests.get(url=GH_SOURCE, headers=github_header).json()
64+
github_response = requests.get(url=GITHUB_SOURCE, headers=GITHUB_HEADER)
65+
github_json = github_response.json()
7066
except ConnectionError as e:
7167
print("Connection Error:", e)
7268
print("Retrying in 10 seconds")
69+
print(" | ✅ Github JSON!")
70+
71+
github_joined = github_json["created_at"]
72+
print(" | | Join Date: ", github_joined)
73+
74+
github_id = github_json["id"]
75+
print(" | | UserID: ", github_id)
76+
77+
github_location = github_json["location"]
78+
print(" | | Location: ", github_location)
7379

74-
# Print Response to Serial
75-
debug_response = False # Set true to see full response
76-
if debug_response:
77-
dump_object = json.dumps(github_response)
78-
print("JSON Dump: ", dump_object)
80+
github_name = github_json["name"]
81+
print(" | | Username: ", github_name)
7982

80-
# Print Keys to Serial
81-
gh_debug_keys = True # Set True to print Serial data
82-
if gh_debug_keys:
83-
github_id = github_response["id"]
84-
print("UserID: ", github_id)
83+
github_repos = github_json["public_repos"]
84+
print(" | | Respositores: ", github_repos)
8585

86-
github_username = github_response["name"]
87-
print("Username: ", github_username)
86+
github_followers = github_json["followers"]
87+
print(" | | Followers: ", github_followers)
88+
github_bio = github_json["bio"]
89+
print(" | | Bio: ", github_bio)
8890

89-
github_followers = github_response["followers"]
90-
print("Followers: ", github_followers)
91+
if DEBUG:
92+
print("Full API GET URL: ", GITHUB_SOURCE)
93+
print(github_json)
9194

92-
print("Monotonic: ", time.monotonic())
95+
github_response.close()
96+
print("✂️ Disconnected from Github API")
9397

9498
print("\nFinished!")
95-
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion))
99+
print(f"Board Uptime: {time_calc(time.monotonic())}")
100+
print(f"Next Update: {time_calc(SLEEP_TIME)}")
96101
print("===============================")
97-
gc.collect()
98102

99103
except (ValueError, RuntimeError) as e:
100-
print("Failed to get data, retrying\n", e)
104+
print(f"Failed to get data, retrying\n {e}")
101105
time.sleep(60)
102-
continue
103-
time.sleep(sleep_time)
106+
break
107+
time.sleep(SLEEP_TIME)

examples/wifi/expanded/requests_wifi_api_mastodon.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# SPDX-License-Identifier: MIT
33
# Coded for Circuit Python 8.2.x
44
"""Mastodon API Example"""
5-
# pylint: disable=import-error
65

76
import os
87
import time

0 commit comments

Comments
 (0)