|
| 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) |
0 commit comments