Skip to content
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

add basic Fitbit API example #140

Merged
merged 6 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions examples/requests_adafruit_discord_active_online.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries
# SPDX-FileCopyrightText: 2023 DJDevon3
# SPDX-License-Identifier: MIT
# Coded for Circuit Python 8.0
"""DJDevon3 Adafruit Feather ESP32-S2 Adafruit_Discord_Active_Users_Example"""
"""
Coded for Circuit Python 8.2.3
requests_adafruit_discord_active_online
"""
import gc
import os
import time
import ssl
import json
import wifi
import socketpool
import adafruit_requests

# No user or token required, 100% JSON web scrape from SHIELDS.IO
# Public API. No user or token required
# 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)

# Time between API refreshes
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
# Time in seconds between updates (polling)
# 600 = 10 mins, 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
# this example uses settings.toml for credentials
ssid = os.getenv("WIFI_SSID")
appw = os.getenv("WIFI_PASSWORD")


# Converts seconds to minutes/hours/days
def time_calc(input_time):
if input_time < 60:
sleep_int = input_time
time_output = f"{sleep_int:.0f} seconds"
elif 60 <= input_time < 3600:
sleep_int = input_time / 60
time_output = f"{sleep_int:.0f} minutes"
elif 3600 <= input_time < 86400:
sleep_int = input_time / 60 / 60
time_output = f"{sleep_int:.0f} hours"
else:
sleep_int = input_time / 60 / 60 / 24
time_output = f"{sleep_int:.1f} days"
return time_output

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"

# Originally attempted to use SVG. Found JSON exists with same filename.
# https://img.shields.io/discord/327254708534116352.svg
Expand All @@ -49,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(ssid, appw)
except ConnectionError as e:
print("Connection Error:", e)
print("Retrying in 10 seconds")
Expand Down Expand Up @@ -93,7 +100,7 @@
print("Monotonic: ", time.monotonic())

print("\nFinished!")
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion))
print("Next Update: ", time_calc(sleep_time))
print("===============================")
gc.collect()

Expand Down
Loading