diff options
Diffstat (limited to 'deckit')
| -rw-r--r-- | deckit/modules/xbox.py | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/deckit/modules/xbox.py b/deckit/modules/xbox.py index 5f9545a..8254e9c 100644 --- a/deckit/modules/xbox.py +++ b/deckit/modules/xbox.py @@ -28,6 +28,7 @@ The endpoints, headers and command bodies follow xbox-webapi-python; Microsoft documents none of them for third parties. """ +import functools import http.client import json import os @@ -119,6 +120,7 @@ def _http(url, data=None, headers=None, timeout=10): return json.loads(raw) if raw else {} +@functools.cache def user_dir(name: str) -> Path: """An XDG user directory (PICTURES, VIDEOS), else ~/Pictures or ~/Videos.""" try: @@ -128,6 +130,14 @@ def user_dir(name: str) -> Path: return Path(p) if p and Path(p) != Path.home() else Path.home() / name.title() +def save_path(c: dict) -> Path: + """Where save puts a capture: <title> <capture date>.png|mp4.""" + clip = c["kind"] == "clip" + name = " ".join(filter(None, [c.get("title"), (c.get("date") or c["id"])[:19].replace(":", "-")])) + name = "".join(ch if ch.isalnum() or ch in " -_.T" else "_" for ch in name).strip(" .") + return user_dir("VIDEOS" if clip else "PICTURES") / "Xbox" / f"{name}.{'mp4' if clip else 'png'}" + + def foreground(presence: dict) -> dict | None: """The title filling a console's screen, if any.""" for dev in presence.get("devices") or []: @@ -409,8 +419,9 @@ class Xbox: return {"signed_in": False, "login": self._login_view()} if "presence" not in self.data: return {"signed_in": True, "offline": self.offline, "reason": self.reason, "loading": not self.offline} + caps = [{**c, "saved": save_path(c).exists()} for c in self.data.get("captures") or []] return {"signed_in": True, "stale": self.offline, "reason": self.reason, **self.data, - "playing": self.playing, "session": self.session} + "captures": caps, "playing": self.playing, "session": self.session} # --- reads ---------------------------------------------------------- def read_fast(self): @@ -551,16 +562,17 @@ class Xbox: self.wake.set() # --- saving a capture on the PC ------------------------------------- - def save(self, body: dict) -> dict: - """Download a listed capture into the PC's Pictures/Xbox or - Videos/Xbox; the answer names the file.""" + def _capture(self, body: dict) -> dict: c = next((c for c in self.data.get("captures") or [] if c["id"] == (body or {}).get("id")), None) if c is None: raise ModuleError("unknown capture") - clip = c["kind"] == "clip" - name = " ".join(filter(None, [c.get("title"), (c.get("date") or c["id"])[:19].replace(":", "-")])) - name = "".join(ch if ch.isalnum() or ch in " -_.T" else "_" for ch in name).strip(" .") - path = user_dir("VIDEOS" if clip else "PICTURES") / "Xbox" / f"{name}.{'mp4' if clip else 'png'}" + return c + + def save(self, body: dict) -> dict: + """Download a listed capture into the PC's Pictures/Xbox or + Videos/Xbox; the answer names the file.""" + c = self._capture(body) + path = save_path(c) tmp = path.with_name("." + path.name + ".part") try: path.parent.mkdir(parents=True, exist_ok=True) |
