feat: add Steam friends and library comparison
CI / test (push) Successful in 37s

Add GET /api/steam/friends and /api/steam/friends/{steamid}, a Friends tab with manual SteamID entry for private profiles, and one-hour caching for friend lists and owned games.
This commit is contained in:
strsv committed 2026-11-07 01:31:44 +03:00
1 parent 5b1b23d392
commit 376a2ee7a0
15 files changed
+456 -15

No files matched your search

+55 -1
View File
@@ -24,6 +24,7 @@ API_ROOT = "https://api.steampowered.com"
USER_AGENT = "SteamBase/1.0"
DEFAULT_TIMEOUT = 15
CACHE_TTL = 900
FRIEND_CACHE_TTL = 3600
_cache = {}
@@ -31,6 +32,10 @@ _cache = {}
class SteamApiError(RuntimeError):
"""Raised for Steam API failures without exposing the key."""
def __init__(self, message, status=None):
super().__init__(message)
self.status = status
def load_key():
key = (os.environ.get(KEY_ENV) or "").strip()
@@ -74,7 +79,13 @@ def _get_json(path, params):
with urllib.request.urlopen(request, timeout=DEFAULT_TIMEOUT) as response:
return json.load(response)
except urllib.error.HTTPError as exc:
raise SteamApiError(f"Steam API HTTP {exc.code}") from None
if exc.code in (401, 403):
message = "Steam profile is private or access is denied"
elif exc.code == 429:
message = "Steam API rate limit exceeded"
else:
message = f"Steam API HTTP {exc.code}"
raise SteamApiError(message, status=exc.code) from None
except (urllib.error.URLError, TimeoutError, ValueError) as exc:
raise SteamApiError(f"Steam API request failed: {type(exc).__name__}") from None
@@ -136,5 +147,48 @@ def achievements(appid, steamid, ttl=CACHE_TTL):
return result
def _cached(key, ttl, loader):
now = time.time()
cached = _cache.get(key)
if cached and now - cached[0] < ttl:
return cached[1]
value = loader()
_cache[key] = (now, value)
return value
def friends(steamid, ttl=FRIEND_CACHE_TTL):
"""Friend list, or [] when the profile is private/unavailable."""
def load():
data = _get_json(
"ISteamUser/GetFriendList/v0001",
{"steamid": steamid, "relationship": "friend"})
return (data.get("friendslist") or {}).get("friends") or []
return _cached(("friends", str(steamid)), ttl, load)
def player_summaries(steamids, ttl=FRIEND_CACHE_TTL):
ids = [str(value) for value in steamids if str(value).isdigit()][:100]
if not ids:
return []
def load():
data = _get_json(
"ISteamUser/GetPlayerSummaries/v0002",
{"steamids": ",".join(ids)})
return (data.get("response") or {}).get("players") or []
return _cached(("summaries", ",".join(ids)), ttl, load)
def owned_games(steamid, ttl=FRIEND_CACHE_TTL):
def load():
data = _get_json(
"IPlayerService/GetOwnedGames/v0001",
{"steamid": steamid, "include_appinfo": 1,
"include_played_free_games": 1})
return (data.get("response") or {}).get("games") or []
return _cached(("owned", str(steamid)), ttl, load)
def clear_cache():
_cache.clear()