From a81ad9738e9013557b2bd3cbdd7014d981f032fe Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Mon, 16 Nov 2026 17:44:47 +0100 Subject: feat(xbox): one games list with installed badges --- AGENTS.md | 4 +++- deckit/modules/xbox.py | 58 ++++++++++++++++++++++++++++++++++++++------------ static/deck.css | 8 +++---- static/modules/xbox.js | 36 +++++++++++-------------------- tests/test_xbox.py | 37 +++++++++++++++++++++++++------- 5 files changed, 92 insertions(+), 51 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index dee3791..ed90af8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -100,7 +100,9 @@ Keep the server dependency-free apart from that one package. consoles (`xccs`) every 30s, profile, recent titles (titlehub, minutes from userstats), the last achievements, friends and each console's installed titles (`xccs` installedApps) every 10 min and on a title change; an - endpoint that refuses keeps its last data. Xbox Live has no system + endpoint that refuses keeps its last data. Installed titles join the + recent ones in one games list (titlehub batch by pfn for those not + recent), marked `installed`, most recently played first. Xbox Live has no system software version. `_http` sends `User-Agent: deck-it`: xccs's gateway answers 403 to urllib's default. A foreground title that titlehub calls a Game is `playing`: `$XDG_RUNTIME_DIR/status.xbox` diff --git a/deckit/modules/xbox.py b/deckit/modules/xbox.py index 7b3da3d..9254f99 100644 --- a/deckit/modules/xbox.py +++ b/deckit/modules/xbox.py @@ -149,11 +149,30 @@ def summarize_consoles(r: dict) -> list: def summarize_apps(r: dict) -> list: - """A console's installed titles, biggest first.""" - apps = [{"name": a.get("name"), "game": a.get("contentType") == "Game", "size": a.get("sizeInBytes") or 0, - "version": a.get("version"), "updated": a.get("updateTime") or a.get("installTime"), - "last": a.get("lastActiveTime")} for a in r.get("result") or []] - return sorted(apps, key=lambda a: -a["size"]) + """A console's installed games and apps (DLC is not a title of its own).""" + return [{"id": str(a["titleId"]) if a.get("titleId") else None, + "pfn": (a.get("aumid") or "").split("!")[0] or None, + "name": a.get("name"), "size": a.get("sizeInBytes") or 0, "version": a.get("version"), + "updated": a.get("updateTime") or a.get("installTime"), "last": a.get("lastActiveTime")} + for a in r.get("result") or [] if a.get("contentType") != "Dlc"] + + +def merge_installed(titles: list, apps: dict) -> list: + """The titles, each marked with what is installed of it on any console, + most recently played first.""" + by = {} + for console_apps in apps.values(): + for a in console_apps: + for k in (a["id"], a["pfn"]): + if k: + by.setdefault(k, a) + out = [] + for t in titles: + a = by.get(t["id"]) or by.get(t.get("pfn")) + inst = {"size": a["size"], "version": a["version"], "updated": a["updated"]} if a else None + last = max(filter(None, (t["last"], a and a["last"])), default=None) + out.append({**t, "installed": inst, "last": last}) + return sorted(out, key=lambda t: t["last"] or "", reverse=True) def minutes_played(r: dict) -> dict: @@ -171,7 +190,8 @@ def summarize_titles(r: dict, minutes: dict) -> list: for t in r.get("titles") or []: a = t.get("achievement") or {} tid = str(t["titleId"]) - out.append({"id": tid, "name": t.get("name"), "type": t.get("type"), "cover": t.get("displayImage"), + out.append({"id": tid, "pfn": t.get("pfn"), "name": t.get("name"), "type": t.get("type"), + "cover": t.get("displayImage"), "last": (t.get("titleHistory") or {}).get("lastTimePlayed"), "achievements": {"done": a.get("currentAchievements", 0), "total": a.get("totalAchievements", 0), "score": a.get("currentGamerscore", 0), "max": a.get("totalGamerscore", 0), @@ -379,28 +399,38 @@ class Xbox: xuid = self.auth["xuid"] def titles(): - history = self.api(TITLEHUB.format(xuid) + f"titlehistory/decoration/achievement,image?maxItems={TITLES}", - 2, extra=HUB) - ids = [str(t["titleId"]) for t in history.get("titles") or []] + """The recent titles plus every installed one, as one list.""" + found = list(self.api(TITLEHUB.format(xuid) + f"titlehistory/decoration/achievement,image?maxItems={TITLES}", + 2, extra=HUB).get("titles") or []) + known = {str(t["titleId"]) for t in found} | {t.get("pfn") for t in found} + apps = [a for v in (self.data.get("apps") or {}).values() for a in v] + pfns = sorted({a["pfn"] for a in apps if a["pfn"] and a["id"] not in known and a["pfn"] not in known}) + if pfns: + try: + found += self.api(TITLEHUB.format(xuid) + "batch/decoration/achievement,image", 2, + {"pfns": pfns, "windowsPhoneProductIds": []}, HUB).get("titles") or [] + except HttpError: + pass # installed titles without a cover; the recent ones still show + ids = [str(t["titleId"]) for t in found] try: minutes = minutes_played(self.api(USERSTATS, 2, { "arrangebyfield": "xuid", "xuids": [xuid], "stats": [{"name": "MinutesPlayed", "titleId": i} for i in ids]})) if ids else {} except HttpError: minutes = {} - out = summarize_titles(history, minutes) + out = merge_installed(summarize_titles({"titles": found}, minutes), self.data.get("apps") or {}) self.types.update({t["id"]: t["type"] for t in out}) return out parts = { "profile": lambda: summarize_profile(self.api(PROFILE, 3)), - "titles": titles, - "achievements": lambda: summarize_achievements(self.api(ACHIEVEMENTS.format(xuid), 2)), - "friends": lambda: summarize_friends(self.api(FRIENDS, 7)), - # console id -> installed titles + # console id -> installed titles; read before "titles", which merges them in "apps": lambda: {c["id"]: summarize_apps(self.api( XCCS + "/lists/installedApps?deviceId=" + urllib.parse.quote(c["id"]), 4, extra=SG)) for c in self.data.get("consoles") or []}, + "titles": titles, + "achievements": lambda: summarize_achievements(self.api(ACHIEVEMENTS.format(xuid), 2)), + "friends": lambda: summarize_friends(self.api(FRIENDS, 7)), } for key, read in parts.items(): try: diff --git a/static/deck.css b/static/deck.css index 6681c3e..778fe72 100644 --- a/static/deck.css +++ b/static/deck.css @@ -906,11 +906,9 @@ dialog.vm-sheet::backdrop { background: color-mix(in srgb, var(--udt-crust) 55%, .xbox .xb-flag { padding: 2px 10px; border-radius: 999px; font-size: 0.857rem; border: 1px solid var(--udt-surface1); color: var(--udt-subtext0); } .xbox .xb-flag.on { border-color: var(--udt-green); color: var(--udt-green); } -.xbox .xb-apps { display: flex; flex-direction: column; gap: 8px; } -.xbox .xb-apps > summary { cursor: pointer; min-height: 32px; } -.xbox .xb-app { display: flex; align-items: center; gap: 10px; padding: 4px 0; } -.xbox .xb-app.game .xb-rtitle { color: var(--udt-text); } -.xbox .xb-app:not(.game) .xb-rtitle { color: var(--udt-subtext1); } +.xbox .xb-gart { position: relative; } +.xbox .xb-badge { position: absolute; left: 6px; bottom: 6px; padding: 2px 8px; border-radius: 999px; + font-size: 0.75rem; font-weight: 600; background: var(--udt-green); color: var(--udt-crust); } .xbox .xb-pill { padding: 4px 12px; border-radius: 999px; background: var(--udt-surface1); font-weight: 600; } .xbox .xb-pill.good { background: var(--udt-green); color: var(--udt-crust); } .xbox .xb-pill.warn { background: var(--udt-yellow); color: var(--udt-crust); } diff --git a/static/modules/xbox.js b/static/modules/xbox.js index 858d4c1..b6b1c45 100644 --- a/static/modules/xbox.js +++ b/static/modules/xbox.js @@ -99,26 +99,6 @@ function presenceLine(st) { // "XboxSeriesX" -> "Xbox Series X" const model = (t) => (t ?? "").replace(/([a-z])([A-Z])/g, "$1 $2"); -// The installed list stays open across redraws once opened. -let appsOpen = false; - -function appsList(apps) { - const box = h("details", "xb-apps"); - box.open = appsOpen; - box.ontoggle = () => { appsOpen = box.open; }; - const total = apps.reduce((n, a) => n + a.size, 0); - box.append(h("summary", "xb-section", `Installed · ${apps.length} · ${gb(total)}`)); - for (const a of apps) { - const sub = [a.version != null ? `v${a.version}` : null, a.updated ? `updated ${ago(a.updated)}` : null, - a.last ? `played ${ago(a.last)}` : null].filter(Boolean).join(" · "); - const r = h("div", `xb-app${a.game ? " game" : ""}`); - const text = h("div", "xb-rtext"); - text.append(h("div", "xb-rtitle", a.name ?? ""), h("div", "xb-rsub", sub)); - r.append(text, h("span", "xb-rside num", gb(a.size))); - box.append(r); - } - return box; -} function consoleCard(c, apps) { const on = c.power === "On"; @@ -162,7 +142,9 @@ function consoleCard(c, apps) { if (on_ != null) flags.append(h("span", `xb-flag${on_ ? " on" : ""}`, `${text} ${on_ ? "on" : "off"}`)); } card.append(flags); - if (apps?.length) card.append(appsList(apps)); + if (apps?.length) { + card.append(h("div", "xb-cmodel", `${apps.length} installed · ${gb(apps.reduce((n, a) => n + a.size, 0))}`)); + } card.append(h("div", "xb-err")); return card; } @@ -189,7 +171,15 @@ function nowPlaying(st) { function gameTile(t) { const tile = h("div", "xb-game"); - tile.append(img(t.cover, "xb-gcover"), h("div", "xb-gname", t.name ?? "")); + const art = h("div", "xb-gart"); + art.append(img(t.cover, "xb-gcover")); + if (t.installed) { + const badge = h("span", "xb-badge", `Installed · ${gb(t.installed.size)}`); + badge.title = [t.installed.version != null ? `v${t.installed.version}` : null, + t.installed.updated ? `updated ${ago(t.installed.updated)}` : null].filter(Boolean).join(" · "); + art.append(badge); + } + tile.append(art, h("div", "xb-gname", t.name ?? "")); if (t.type === "Game") { const a = t.achievements; const bar = h("div", "gbar"); @@ -251,7 +241,7 @@ function render(box, st) { } box.append(top); - box.append(h("div", "xb-section", "Recent games")); + box.append(h("div", "xb-section", "Games")); const games = h("div", "xb-games"); for (const t of st.titles ?? []) games.append(gameTile(t)); box.append(games); diff --git a/tests/test_xbox.py b/tests/test_xbox.py index f70aa6f..8844e3b 100644 --- a/tests/test_xbox.py +++ b/tests/test_xbox.py @@ -37,9 +37,17 @@ PROFILE = {"profileUsers": [{"id": "1", "settings": [ {"id": "Gamertag", "value": "Player"}, {"id": "GameDisplayPicRaw", "value": "https://img.example.org/p.png"}, {"id": "Gamerscore", "value": "12345"}]}]} APPS = {"result": [ - {"name": "Small App", "contentType": "App", "sizeInBytes": 1e8, "version": 7, "installTime": "2026-10-01T00:00:00Z"}, - {"name": "Big Game", "contentType": "Game", "sizeInBytes": 9e10, "version": 12, "installTime": "2026-09-01T00:00:00Z", - "updateTime": "2026-11-10T00:00:00Z", "lastActiveTime": "2026-11-15T21:00:00Z"}]} + {"titleId": 99, "aumid": "Video.App_x!App", "name": "Video App", "contentType": "App", "sizeInBytes": 1e8, + "version": 7, "installTime": "2026-10-01T00:00:00Z"}, + {"titleId": 1234, "aumid": "Some.Game_x!Game", "name": "Some Game", "contentType": "Game", "sizeInBytes": 9e10, + "version": 12, "installTime": "2026-09-01T00:00:00Z", "updateTime": "2026-11-10T00:00:00Z", + "lastActiveTime": "2026-11-15T21:00:00Z"}, + {"titleId": 777, "aumid": "Old.Game_x!Game", "name": "Old Game", "contentType": "Game", "sizeInBytes": 5e10, + "version": 3, "installTime": "2026-01-01T00:00:00Z", "lastActiveTime": "2026-03-01T00:00:00Z"}, + {"name": "Some Game Expansion", "contentType": "Dlc", "sizeInBytes": 2e9}]} +BATCH = {"titles": [{"titleId": "777", "pfn": "Old.Game_x", "name": "Old Game", "type": "Game", + "displayImage": "https://img.example.org/o.png", + "achievement": {"currentGamerscore": 50, "totalGamerscore": 1000, "progressPercentage": 5.0}}]} HISTORY = {"titles": [ {"titleId": "1234", "name": "Some Game", "type": "Game", "displayImage": "https://img.example.org/c.png", "titleHistory": {"lastTimePlayed": "2026-11-16T10:00:00Z"}, @@ -81,6 +89,7 @@ class Fake: "peoplehub": FRIENDS, "/commands": {"status": {"errorCode": "OK"}, "opId": "op"}, "/lists/installedApps": APPS, + "/titles/batch/": BATCH, } self.answers.update(over) @@ -172,11 +181,20 @@ class Summaries(unittest.TestCase): self.assertEqual((c["id"], c["name"], c["power"]), ("CON1", "Living room", "On")) self.assertEqual(c["storage"], [{"name": "Internal", "free": 4e11, "total": 1e12}]) - def test_apps_biggest_first(self): + def test_apps_skip_dlc(self): a = xb.summarize_apps(APPS) - self.assertEqual([x["name"] for x in a], ["Big Game", "Small App"]) - self.assertEqual((a[0]["game"], a[0]["version"], a[0]["updated"]), (True, 12, "2026-11-10T00:00:00Z")) - self.assertEqual(a[1]["updated"], "2026-10-01T00:00:00Z") # never updated: the install time + self.assertEqual([x["name"] for x in a], ["Video App", "Some Game", "Old Game"]) + self.assertEqual((a[1]["id"], a[1]["pfn"], a[1]["updated"]), ("1234", "Some.Game_x", "2026-11-10T00:00:00Z")) + self.assertEqual(a[0]["updated"], "2026-10-01T00:00:00Z") # never updated: the install time + + def test_merge_marks_installed_and_sorts_by_last_played(self): + titles = xb.summarize_titles(HISTORY, {}) + xb.summarize_titles(BATCH, {}) + merged = xb.merge_installed(titles, {"CON1": xb.summarize_apps(APPS)}) + self.assertEqual([t["name"] for t in merged], ["Some Game", "Old Game", "Video App"]) + self.assertEqual(merged[0]["installed"]["size"], 9e10) + self.assertEqual(merged[0]["last"], "2026-11-16T10:00:00Z") # the newer of history and console + self.assertEqual(merged[1]["last"], "2026-03-01T00:00:00Z") # no history: the console's last use + self.assertEqual(merged[1]["installed"]["version"], 3) def test_minutes_and_titles(self): minutes = xb.minutes_played(STATS) @@ -251,8 +269,11 @@ class Tick(Base): self.assertEqual(st["titles"][0]["minutes"], 185) self.assertEqual(st["achievements"][0]["score"], 15) self.assertEqual(st["friends"][0]["gamertag"], "amy") - self.assertEqual(st["apps"]["CON1"][0]["name"], "Big Game") self.assertIn("deviceId=CON1", self.fake.urls("/lists/installedApps")[0][0]) + # the installed game missing from the recent ones comes from the batch, by pfn + self.assertEqual(self.fake.urls("/titles/batch/")[0][1]["pfns"], ["Old.Game_x"]) + old = next(t for t in st["titles"] if t["name"] == "Old Game") + self.assertEqual((old["cover"], old["installed"]["size"]), ("https://img.example.org/o.png", 5e10)) self.assertEqual(st["playing"]["name"], "Some Game") self.assertIsInstance(st["session"], int) self.assertEqual(self.status.read_text(), "1") -- cgit v1.2.3