Files
SteamBase/tools/check_ownership.py
T
strsv 2ed00b7912
CI / test (push) Successful in 40s
feat: distinguish owned and shared games
Add game_ownership (schema v10) and tools/check_ownership.py, compare the local library with GetOwnedGames, expose owned/shared counts and filters, and show a shared-game badge on cards.
2026-11-07 01:40:47 +03:00

78 lines
2.4 KiB
Python

"""Mark local games that are owned by the Steam account.
python tools/check_ownership.py
python tools/check_ownership.py --dry-run
Uses the Steam Web API (GetOwnedGames). Games present locally but missing from
the API response are marked owned=0 — usually family-shared or account-bound
items. Requires STEAMBASE_STEAM_API_KEY or data/steam_api_key.txt.
"""
import argparse
import os
import sys
from datetime import datetime, timezone
for stream in (sys.stdout, sys.stderr):
try:
stream.reconfigure(encoding="utf-8")
except Exception:
pass
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import db
import steam_api
def main():
ap = argparse.ArgumentParser(description="Mark games owned by the Steam account")
ap.add_argument("--db", default=db.DEFAULT_DB)
ap.add_argument("--dry-run", action="store_true")
args = ap.parse_args()
if not steam_api.has_key():
print("Steam API key is not configured", file=sys.stderr)
return 1
con = db.connect(args.db)
db.init(con)
try:
steamid = steam_api.steam_id(con)
if not steamid:
print("SteamID is not known; run import_steam.py", file=sys.stderr)
return 1
try:
games = steam_api.owned_games(steamid)
except steam_api.SteamApiError as exc:
print(f"Steam API: {exc}", file=sys.stderr)
return 1
owned_ids = {int(game["appid"]) for game in games if game.get("appid")}
local = [row["appid"] for row in con.execute(
"SELECT appid FROM games WHERE LOWER(type) = 'game' ORDER BY appid")]
now = datetime.now(timezone.utc).isoformat(timespec="seconds")
rows = [(appid, 1 if appid in owned_ids else 0, now) for appid in local]
owned = sum(row[1] for row in rows)
shared = len(rows) - owned
if not args.dry_run:
con.executemany(
"""INSERT INTO game_ownership (appid, owned, checked_at)
VALUES (?, ?, ?)
ON CONFLICT(appid) DO UPDATE SET
owned = excluded.owned, checked_at = excluded.checked_at""",
rows)
con.commit()
print(f"Local games: {len(local)} | owned: {owned} | not owned: {shared}")
if args.dry_run:
print("dry run: database was not changed")
return 0
finally:
con.close()
if __name__ == "__main__":
raise SystemExit(main())