feat: distinguish owned and shared games
CI / test (push) Successful in 40s

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.
This commit is contained in:
strsv committed 2026-11-07 01:40:47 +03:00
1 parent 528fcb7474
commit 2ed00b7912
18 files changed
+255 -10

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
"""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())
+15 -1
View File
@@ -8,7 +8,7 @@ from datetime import datetime, timezone
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEFAULT_DB = os.path.join(BASE_DIR, "steam.db")
SCHEMA_PATH = os.path.join(BASE_DIR, "tools", "schema.sql")
SCHEMA_VERSION = 9
SCHEMA_VERSION = 10
MOOD_PRESETS = {
"adventure-rpg-party": {
@@ -587,6 +587,19 @@ def _migration_9(con):
con.execute("CREATE INDEX IF NOT EXISTS idx_games_parent ON games(parent_appid)")
def _migration_10(con):
"""Track which local games are owned by the Steam account."""
con.execute("""
CREATE TABLE IF NOT EXISTS game_ownership (
appid INTEGER PRIMARY KEY REFERENCES games(appid) ON DELETE CASCADE,
owned INTEGER NOT NULL,
checked_at TEXT NOT NULL
)
""")
con.execute("CREATE INDEX IF NOT EXISTS idx_game_ownership_owned "
"ON game_ownership(owned)")
MIGRATIONS = {
1: _migration_1,
2: _migration_2,
@@ -597,6 +610,7 @@ MIGRATIONS = {
7: _migration_7,
8: _migration_8,
9: _migration_9,
10: _migration_10,
}
+7
View File
@@ -25,6 +25,13 @@ CREATE TABLE IF NOT EXISTS games (
updated_at TEXT
);
CREATE TABLE IF NOT EXISTS game_ownership (
appid INTEGER PRIMARY KEY REFERENCES games(appid) ON DELETE CASCADE,
owned INTEGER NOT NULL,
checked_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_game_ownership_owned ON game_ownership(owned);
CREATE TABLE IF NOT EXISTS game_genres (
appid INTEGER NOT NULL REFERENCES games(appid) ON DELETE CASCADE,
genre TEXT NOT NULL,