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:
1 parent
5b1b23d392
commit
376a2ee7a0
15 files changed
+456
-15
No files matched your search
+101
@@ -1008,6 +1008,14 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self._compare(qs)
|
||||
elif path == "/api/activity":
|
||||
self._activity(qs)
|
||||
elif path == "/api/steam/friends":
|
||||
self._steam_friends()
|
||||
elif path.startswith("/api/steam/friends/"):
|
||||
tail = path.rsplit("/", 1)[1]
|
||||
if not (tail.isdigit() and len(tail) == 17):
|
||||
self._error(400, "invalid steamid")
|
||||
else:
|
||||
self._steam_friend(tail)
|
||||
elif path.startswith("/api/steam/achievements/"):
|
||||
tail = path.rsplit("/", 1)[1]
|
||||
if not tail.isdigit():
|
||||
@@ -1338,6 +1346,99 @@ class Handler(BaseHTTPRequestHandler):
|
||||
finally:
|
||||
con.close()
|
||||
|
||||
def _steam_friends(self):
|
||||
con = connect()
|
||||
try:
|
||||
if not steam_api.has_key():
|
||||
self._error(400, "Steam API key is not configured")
|
||||
return
|
||||
steamid = steam_api.steam_id(con)
|
||||
if not steamid:
|
||||
self._error(400, "SteamID is not known; run import_steam.py")
|
||||
return
|
||||
try:
|
||||
friends = steam_api.friends(steamid)
|
||||
ids = [item.get("steamid") for item in friends if item.get("steamid")]
|
||||
summaries = steam_api.player_summaries(ids) if ids else []
|
||||
except steam_api.SteamApiError as exc:
|
||||
if exc.status in (401, 403):
|
||||
self._json({"friends": [], "total": 0, "private": True})
|
||||
return
|
||||
self._error(502, str(exc))
|
||||
return
|
||||
by_id = {str(player.get("steamid")): player for player in summaries}
|
||||
items = []
|
||||
for item in friends:
|
||||
sid = str(item.get("steamid") or "")
|
||||
if not sid:
|
||||
continue
|
||||
player = by_id.get(sid) or {}
|
||||
items.append({
|
||||
"steamid": sid,
|
||||
"name": player.get("personaname") or sid,
|
||||
"avatar": player.get("avatarmedium") or player.get("avatar") or "",
|
||||
"profile_url": player.get("profileurl")
|
||||
or f"https://steamcommunity.com/profiles/{sid}",
|
||||
"state": player.get("personastate"),
|
||||
"friend_since": item.get("friend_since"),
|
||||
})
|
||||
items.sort(key=lambda row: row["name"].lower())
|
||||
self._json({"friends": items, "total": len(items)})
|
||||
finally:
|
||||
con.close()
|
||||
|
||||
def _steam_friend(self, friend_id):
|
||||
con = connect()
|
||||
try:
|
||||
if not steam_api.has_key():
|
||||
self._error(400, "Steam API key is not configured")
|
||||
return
|
||||
try:
|
||||
games = steam_api.owned_games(friend_id)
|
||||
summaries = steam_api.player_summaries([friend_id])
|
||||
except steam_api.SteamApiError as exc:
|
||||
self._error(502, str(exc))
|
||||
return
|
||||
local = {
|
||||
row["appid"]: row for row in con.execute(
|
||||
"""SELECT g.appid, g.name, COALESCE(pe.status, 'backlog') AS status
|
||||
FROM games g LEFT JOIN personal pe ON pe.appid = g.appid
|
||||
WHERE LOWER(g.type) = 'game'""")
|
||||
}
|
||||
common, friend_only = [], []
|
||||
for game in games:
|
||||
appid = game.get("appid")
|
||||
if not appid:
|
||||
continue
|
||||
hours = round((game.get("playtime_forever") or 0) / 60, 1)
|
||||
if appid in local:
|
||||
common.append({
|
||||
"appid": appid, "name": local[appid]["name"],
|
||||
"status": local[appid]["status"], "friend_hours": hours,
|
||||
})
|
||||
else:
|
||||
friend_only.append({
|
||||
"appid": appid, "name": game.get("name") or str(appid),
|
||||
"friend_hours": hours,
|
||||
})
|
||||
common.sort(key=lambda row: row["friend_hours"], reverse=True)
|
||||
friend_only.sort(key=lambda row: row["friend_hours"], reverse=True)
|
||||
player = summaries[0] if summaries else {}
|
||||
self._json({
|
||||
"steamid": friend_id,
|
||||
"name": player.get("personaname") or friend_id,
|
||||
"avatar": player.get("avatarmedium") or player.get("avatar") or "",
|
||||
"profile_url": player.get("profileurl")
|
||||
or f"https://steamcommunity.com/profiles/{friend_id}",
|
||||
"total_games": len(games),
|
||||
"common_count": len(common),
|
||||
"friend_only_count": len(friend_only),
|
||||
"common": common[:500],
|
||||
"friend_only": friend_only[:500],
|
||||
})
|
||||
finally:
|
||||
con.close()
|
||||
|
||||
def _steam_achievements(self, appid):
|
||||
con = connect()
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user