Skip to content

Commit 84ab63a

Browse files
committed
Steam API example
Example displays the sum total amount of time in hours and days playing video games on steam.
1 parent 0c12ada commit 84ab63a

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

examples/requests_api_steam.py

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

0 commit comments

Comments
 (0)