|
1 |
| -# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries |
| 1 | +# SPDX-FileCopyrightText: 2023 DJDevon3 |
2 | 2 | # SPDX-License-Identifier: MIT
|
3 |
| -# Coded for Circuit Python 8.0 |
4 |
| -"""DJDevon3 Adafruit Feather ESP32-S2 Adafruit_Discord_Active_Users_Example""" |
| 3 | +""" |
| 4 | +Coded for Circuit Python 8.2.3 |
| 5 | +requests_adafruit_discord_active_online |
| 6 | +""" |
5 | 7 | import gc
|
| 8 | +import os |
6 | 9 | import time
|
7 | 10 | import ssl
|
8 | 11 | import json
|
9 | 12 | import wifi
|
10 | 13 | import socketpool
|
11 | 14 | import adafruit_requests
|
12 | 15 |
|
13 |
| -# No user or token required, 100% JSON web scrape from SHIELDS.IO |
| 16 | +# Public API. No user or token required |
| 17 | +# JSON web scrape from SHIELDS.IO |
14 | 18 | # Adafruit uses Shields.IO to see online users
|
15 | 19 |
|
16 | 20 | # Initialize WiFi Pool (There can be only 1 pool & top of script)
|
17 | 21 | pool = socketpool.SocketPool(wifi.radio)
|
18 | 22 |
|
19 |
| -# Time between API refreshes |
20 |
| -# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
| 23 | +# Time in seconds between updates (polling) |
| 24 | +# 600 = 10 mins, 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
21 | 25 | sleep_time = 900
|
22 | 26 |
|
23 |
| -try: |
24 |
| - from secrets import secrets |
25 |
| -except ImportError: |
26 |
| - print("Secrets File Import Error") |
27 |
| - raise |
| 27 | +# this example uses settings.toml for credentials |
| 28 | +ssid = os.getenv("WIFI_SSID") |
| 29 | +appw = os.getenv("WIFI_PASSWORD") |
| 30 | + |
| 31 | + |
| 32 | +# Converts seconds to minutes/hours/days |
| 33 | +def time_calc(input_time): |
| 34 | + if input_time < 60: |
| 35 | + sleep_int = input_time |
| 36 | + time_output = f"{sleep_int:.0f} seconds" |
| 37 | + elif 60 <= input_time < 3600: |
| 38 | + sleep_int = input_time / 60 |
| 39 | + time_output = f"{sleep_int:.0f} minutes" |
| 40 | + elif 3600 <= input_time < 86400: |
| 41 | + sleep_int = input_time / 60 / 60 |
| 42 | + time_output = f"{sleep_int:.0f} hours" |
| 43 | + else: |
| 44 | + sleep_int = input_time / 60 / 60 / 24 |
| 45 | + time_output = f"{sleep_int:.1f} days" |
| 46 | + return time_output |
28 | 47 |
|
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 | 48 |
|
42 | 49 | # Originally attempted to use SVG. Found JSON exists with same filename.
|
43 | 50 | # https://img.shields.io/discord/327254708534116352.svg
|
|
49 | 56 | requests = adafruit_requests.Session(pool, ssl.create_default_context())
|
50 | 57 | while not wifi.radio.ipv4_address:
|
51 | 58 | try:
|
52 |
| - wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 59 | + wifi.radio.connect(ssid, appw) |
53 | 60 | except ConnectionError as e:
|
54 | 61 | print("Connection Error:", e)
|
55 | 62 | print("Retrying in 10 seconds")
|
|
93 | 100 | print("Monotonic: ", time.monotonic())
|
94 | 101 |
|
95 | 102 | print("\nFinished!")
|
96 |
| - print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) |
| 103 | + print("Next Update: ", time_calc(sleep_time)) |
97 | 104 | print("===============================")
|
98 | 105 | gc.collect()
|
99 | 106 |
|
|
0 commit comments