Skip to content

Commit 9ffbff4

Browse files
committed
Fix token file permissions, 4xx error queueing, and heartbeat silent failure
1 parent 5f95a34 commit 9ffbff4

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

script.punchplay/api.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def _get_or_create_device_id(self) -> str:
5454
if device_id:
5555
return device_id
5656
device_id = str(uuid.uuid4())
57-
with open(self._device_id_file, "w") as f:
57+
fd = os.open(self._device_id_file, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
58+
with os.fdopen(fd, "w") as f:
5859
f.write(device_id)
5960
return device_id
6061

@@ -69,7 +70,8 @@ def _load_tokens(self) -> dict[str, str]:
6970

7071
def _save_tokens(self, tokens: dict[str, str]) -> None:
7172
self._tokens = tokens
72-
with open(self._token_file, "w") as f:
73+
fd = os.open(self._token_file, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
74+
with os.fdopen(fd, "w") as f:
7375
json.dump(tokens, f, indent=2)
7476

7577
def _headers(self) -> dict[str, str]:
@@ -168,11 +170,19 @@ def post(self, path: str, payload: dict[str, Any]) -> dict[str, Any] | None:
168170
self._cache.enqueue_scrobble(path, payload)
169171
return None
170172
except urllib.error.HTTPError as exc:
171-
xbmc.log(
172-
f"[PunchPlay] HTTP {exc.code} on {path} — queuing", xbmc.LOGWARNING
173-
)
174-
if self._cache is not None:
175-
self._cache.enqueue_scrobble(path, payload)
173+
if 500 <= exc.code < 600:
174+
# Transient server error — queue for retry.
175+
xbmc.log(
176+
f"[PunchPlay] HTTP {exc.code} on {path} — queuing", xbmc.LOGWARNING
177+
)
178+
if self._cache is not None:
179+
self._cache.enqueue_scrobble(path, payload)
180+
else:
181+
# Permanent client error (4xx) — drop, retrying won't help.
182+
xbmc.log(
183+
f"[PunchPlay] HTTP {exc.code} on {path} — dropping (permanent error)",
184+
xbmc.LOGWARNING,
185+
)
176186
return None
177187

178188
# ------------------------------------------------------------------

script.punchplay/player.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ def _heartbeat_loop(self) -> None:
168168
self._api.post("/api/scrobble/progress", payload)
169169

170170
except Exception as exc:
171-
xbmc.log(f"[PunchPlay] Heartbeat error: {exc}", xbmc.LOGDEBUG)
171+
xbmc.log(f"[PunchPlay] Heartbeat error: {exc}", xbmc.LOGWARNING)
172+
# If the player is no longer valid, stop the heartbeat loop
173+
# rather than spinning silently.
174+
if not self.isPlayingVideo():
175+
xbmc.log("[PunchPlay] Heartbeat stopping — player no longer active", xbmc.LOGINFO)
176+
return
172177

173178
# ------------------------------------------------------------------
174179
# Playback events

0 commit comments

Comments
 (0)