|
1 |
| -# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries |
| 1 | +# SPDX-FileCopyrightText: 2024 DJDevon3 |
2 | 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 json |
| 3 | +# Coded for Circuit Python 8.2.x |
| 4 | +"""Github API Example""" |
| 5 | +# pylint: disable=import-error |
| 6 | + |
7 | 7 | import os
|
8 |
| -import ssl |
9 | 8 | import time
|
10 | 9 |
|
11 |
| -import socketpool |
| 10 | +import adafruit_connection_manager |
12 | 11 | import wifi
|
13 | 12 |
|
14 | 13 | import adafruit_requests
|
15 | 14 |
|
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 |
| 15 | +# Github developer token required. |
| 16 | +username = os.getenv("GITHUB_USERNAME") |
| 17 | +token = os.getenv("GITHUB_TOKEN") |
22 | 18 |
|
23 | 19 | # Get WiFi details, ensure these are setup in settings.toml
|
24 | 20 | ssid = os.getenv("CIRCUITPY_WIFI_SSID")
|
25 | 21 | 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") |
| 22 | + |
| 23 | +# API Polling Rate |
| 24 | +# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
| 25 | +SLEEP_TIME = 900 |
| 26 | + |
| 27 | +# Set debug to True for full JSON response. |
| 28 | +# WARNING: may include visible credentials |
| 29 | +DEBUG = False |
| 30 | + |
| 31 | +# Initalize Wifi, Socket Pool, Request Session |
| 32 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 33 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
| 34 | +requests = adafruit_requests.Session(pool, ssl_context) |
| 35 | + |
| 36 | +GITHUB_HEADER = {"Authorization": " token " + token} |
| 37 | +GITHUB_SOURCE = "https://api.github.com/users/" + username |
| 38 | + |
| 39 | + |
| 40 | +def time_calc(input_time): |
| 41 | + """Converts seconds to minutes/hours/days""" |
| 42 | + if input_time < 60: |
| 43 | + return f"{input_time:.0f} seconds" |
| 44 | + if input_time < 3600: |
| 45 | + return f"{input_time / 60:.0f} minutes" |
| 46 | + if input_time < 86400: |
| 47 | + return f"{input_time / 60 / 60:.0f} hours" |
| 48 | + return f"{input_time / 60 / 60 / 24:.1f} days" |
| 49 | + |
59 | 50 |
|
60 | 51 | while True:
|
| 52 | + # Connect to Wi-Fi |
| 53 | + print("\nConnecting to WiFi...") |
| 54 | + while not wifi.radio.ipv4_address: |
| 55 | + try: |
| 56 | + wifi.radio.connect(ssid, password) |
| 57 | + except ConnectionError as e: |
| 58 | + print("❌ Connection Error:", e) |
| 59 | + print("Retrying in 10 seconds") |
| 60 | + print("✅ Wifi!") |
| 61 | + |
61 | 62 | 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("===============================") |
| 63 | + print(" | Attempting to GET Github JSON!") |
68 | 64 | try:
|
69 |
| - github_response = requests.get(url=GH_SOURCE, headers=github_header).json() |
| 65 | + github_response = requests.get(url=GITHUB_SOURCE, headers=GITHUB_HEADER) |
| 66 | + github_json = github_response.json() |
70 | 67 | except ConnectionError as e:
|
71 | 68 | print("Connection Error:", e)
|
72 | 69 | print("Retrying in 10 seconds")
|
| 70 | + print(" | ✅ Github JSON!") |
| 71 | + |
| 72 | + github_joined = github_json["created_at"] |
| 73 | + print(" | | Join Date: ", github_joined) |
| 74 | + |
| 75 | + github_id = github_json["id"] |
| 76 | + print(" | | UserID: ", github_id) |
| 77 | + |
| 78 | + github_location = github_json["location"] |
| 79 | + print(" | | Location: ", github_location) |
73 | 80 |
|
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) |
| 81 | + github_name = github_json["name"] |
| 82 | + print(" | | Username: ", github_name) |
79 | 83 |
|
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) |
| 84 | + github_repos = github_json["public_repos"] |
| 85 | + print(" | | Respositores: ", github_repos) |
85 | 86 |
|
86 |
| - github_username = github_response["name"] |
87 |
| - print("Username: ", github_username) |
| 87 | + github_followers = github_json["followers"] |
| 88 | + print(" | | Followers: ", github_followers) |
| 89 | + github_bio = github_json["bio"] |
| 90 | + print(" | | Bio: ", github_bio) |
88 | 91 |
|
89 |
| - github_followers = github_response["followers"] |
90 |
| - print("Followers: ", github_followers) |
| 92 | + if DEBUG: |
| 93 | + print("Full API GET URL: ", GITHUB_SOURCE) |
| 94 | + print(github_json) |
91 | 95 |
|
92 |
| - print("Monotonic: ", time.monotonic()) |
| 96 | + github_response.close() |
| 97 | + print("✂️ Disconnected from Github API") |
93 | 98 |
|
94 | 99 | print("\nFinished!")
|
95 |
| - print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) |
| 100 | + print(f"Board Uptime: {time_calc(time.monotonic())}") |
| 101 | + print(f"Next Update: {time_calc(SLEEP_TIME)}") |
96 | 102 | print("===============================")
|
97 |
| - gc.collect() |
98 | 103 |
|
99 | 104 | except (ValueError, RuntimeError) as e:
|
100 |
| - print("Failed to get data, retrying\n", e) |
| 105 | + print(f"Failed to get data, retrying\n {e}") |
101 | 106 | time.sleep(60)
|
102 |
| - continue |
103 |
| - time.sleep(sleep_time) |
| 107 | + break |
| 108 | + time.sleep(SLEEP_TIME) |
0 commit comments