Skip to content

Commit 6dd6dee

Browse files
authored
Merge pull request #129 from DJDevon3/main
Steam API example
2 parents 558fff7 + 32c45a0 commit 6dd6dee

6 files changed

+119
-3
lines changed

examples/requests_api_discord.py

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
# Print keys to Serial
9090
discord_debug_keys = True # Set to True to print Serial data
9191
if discord_debug_keys:
92-
9392
ada_discord_all_members = ada_res["approximate_member_count"]
9493
print("Members: ", ada_discord_all_members)
9594

examples/requests_api_github.py

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
# Print Keys to Serial
8282
gh_debug_keys = True # Set True to print Serial data
8383
if gh_debug_keys:
84-
8584
github_id = github_response["id"]
8685
print("UserID: ", github_id)
8786

examples/requests_api_mastodon.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
print("Secrets File Import Error")
3434
raise
3535

36+
3637
# Converts seconds in minutes/hours/days
3738
def time_calc(input_time):
3839
if input_time < 60:

examples/requests_api_steam.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# SPDX-FileCopyrightText: 2022 DJDevon3 (Neradoc & Deshipu helped) for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
# Coded for Circuit Python 8.0
4+
"""DJDevon3 Adafruit Feather ESP32-S2 api_steam Example"""
5+
import os
6+
import gc
7+
import time
8+
import ssl
9+
import json
10+
import wifi
11+
import socketpool
12+
import adafruit_requests
13+
14+
# Steam API Docs: https://steamcommunity.com/dev
15+
# Steam API Key: https://steamcommunity.com/dev/apikey
16+
# Steam Usernumber: Visit https://steamcommunity.com
17+
# click on your profile icon, your usernumber will be in the browser url.
18+
19+
# Ensure these are setup in settings.toml
20+
# Requires Steam Developer API key
21+
ssid = os.getenv("AP_SSID")
22+
appw = os.getenv("AP_PASSWORD")
23+
steam_usernumber = os.getenv("steam_id")
24+
steam_apikey = os.getenv("steam_api_key")
25+
26+
# Initialize WiFi Pool (There can be only 1 pool & top of script)
27+
pool = socketpool.SocketPool(wifi.radio)
28+
29+
# Time between API refreshes
30+
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
31+
sleep_time = 900
32+
33+
# Deconstruct URL (pylint hates long lines)
34+
# http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/
35+
# ?key=XXXXXXXXXXXXXXXXXXXXX&include_played_free_games=1&steamid=XXXXXXXXXXXXXXXX&format=json
36+
Steam_OwnedGames_URL = (
37+
"http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?"
38+
+ "key="
39+
+ steam_apikey
40+
+ "&include_played_free_games=1"
41+
+ "&steamid="
42+
+ steam_usernumber
43+
+ "&format=json"
44+
)
45+
46+
if sleep_time < 60:
47+
sleep_time_conversion = "seconds"
48+
sleep_int = sleep_time
49+
elif 60 <= sleep_time < 3600:
50+
sleep_int = sleep_time / 60
51+
sleep_time_conversion = "minutes"
52+
elif 3600 <= sleep_time < 86400:
53+
sleep_int = sleep_time / 60 / 60
54+
sleep_time_conversion = "hours"
55+
else:
56+
sleep_int = sleep_time / 60 / 60 / 24
57+
sleep_time_conversion = "days"
58+
59+
# Connect to Wi-Fi
60+
print("\n===============================")
61+
print("Connecting to WiFi...")
62+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
63+
while not wifi.radio.ipv4_address:
64+
try:
65+
wifi.radio.connect(ssid, appw)
66+
except ConnectionError as e:
67+
print("Connection Error:", e)
68+
print("Retrying in 10 seconds")
69+
time.sleep(10)
70+
gc.collect()
71+
print("Connected!\n")
72+
73+
while True:
74+
try:
75+
print("\nAttempting to GET STEAM Stats!") # --------------------------------
76+
# Print Request to Serial
77+
debug_request = False # Set true to see full request
78+
if debug_request:
79+
print("Full API GET URL: ", Steam_OwnedGames_URL)
80+
print("===============================")
81+
try:
82+
steam_response = requests.get(url=Steam_OwnedGames_URL).json()
83+
except ConnectionError as e:
84+
print("Connection Error:", e)
85+
print("Retrying in 10 seconds")
86+
87+
# Print Response to Serial
88+
debug_response = False # Set true to see full response
89+
if debug_response:
90+
dump_object = json.dumps(steam_response)
91+
print("JSON Dump: ", dump_object)
92+
93+
# Print Keys to Serial
94+
steam_debug_keys = True # Set True to print Serial data
95+
if steam_debug_keys:
96+
game_count = steam_response["response"]["game_count"]
97+
print("Total Games: ", game_count)
98+
total_minutes = 0
99+
100+
for game in steam_response["response"]["games"]:
101+
total_minutes += game["playtime_forever"]
102+
total_hours = total_minutes / 60
103+
total_days = total_minutes / 60 / 24
104+
print(f"Total Hours: {total_hours}")
105+
print(f"Total Days: {total_days}")
106+
107+
print("Monotonic: ", time.monotonic())
108+
print("\nFinished!")
109+
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion))
110+
print("===============================")
111+
gc.collect()
112+
113+
except (ValueError, RuntimeError) as e:
114+
print("Failed to get data, retrying\n", e)
115+
time.sleep(60)
116+
continue
117+
time.sleep(sleep_time)

examples/requests_api_twitch.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
print("Secrets File Import Error")
3434
raise
3535

36+
3637
# Converts seconds in minutes/hours/days
3738
def time_calc(input_time):
3839
if input_time < 60:

examples/requests_api_twitter.py

-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
# Print to Serial
8989
tw_debug_keys = True # Set true to print Serial data
9090
if tw_debug_keys:
91-
9291
tw_userid = tw_json["data"]["id"]
9392
print("User ID: ", tw_userid)
9493

0 commit comments

Comments
 (0)