From 7e9f78ce882bc9fc2f40f632a021261cf10c671e Mon Sep 17 00:00:00 2001 From: NikkeDoy Date: Thu, 4 Jun 2026 16:22:29 +0300 Subject: [PATCH] :bug: | Fix prefetch bug (str vs int), move json import to module level, add missing type hints --- config.py | 4 ++-- music_db.py | 2 +- player.py | 8 +++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/config.py b/config.py index 14fe762..9c2d400 100644 --- a/config.py +++ b/config.py @@ -84,13 +84,13 @@ def save_volume(volume: int) -> None: save_config(dict(SETTINGS)) -def save_setting(key: str, value) -> None: +def save_setting(key: str, value: object) -> None: """Persist a single setting and update the in-memory SETTINGS dict.""" SETTINGS[key] = value save_config(dict(SETTINGS)) -def get_setting(key: str): +def get_setting(key: str) -> object: """Return a setting value from the in-memory dict.""" return SETTINGS.get(key, DEFAULT_CONFIG.get(key)) diff --git a/music_db.py b/music_db.py index 0725339..cfe6476 100644 --- a/music_db.py +++ b/music_db.py @@ -1,5 +1,6 @@ """SQLite-backed database for song history, favourites, and play stats.""" +import json import sqlite3 import threading from datetime import datetime, timezone @@ -87,7 +88,6 @@ class MusicDB: duration: int = 0, thumbnail: Optional[str] = None) -> None: """Record that a song was played (upsert + history row).""" - import json artists_json = json.dumps(artists, ensure_ascii=False) album_json = json.dumps(album) if album else None with self._lock: diff --git a/player.py b/player.py index adf35c4..a32678f 100644 --- a/player.py +++ b/player.py @@ -414,6 +414,8 @@ class AudioPlayer(QObject): self._active_task_id: Optional[int] = None # The task ID of the in-progress prefetch (if any). self._prefetch_task_id: Optional[int] = None + # The video_id being prefetched (used to avoid duplicate prefetches). + self._prefetch_video_id: Optional[str] = None # Cache of prefetched results: video_id → resolved dict with _local_path. self._prefetch_cache: dict[str, dict] = {} @@ -616,6 +618,7 @@ class AudioPlayer(QObject): self._worker.cancel_requested.emit() self._active_task_id = None self._prefetch_task_id = None + self._prefetch_video_id = None @Slot(int, dict) def _on_download_succeeded(self, task_id: int, song: dict) -> None: @@ -647,6 +650,7 @@ class AudioPlayer(QObject): # ── Stale check for prefetch download ──────────────────────── if task_id == self._prefetch_task_id: self._prefetch_task_id = None + self._prefetch_video_id = None vid = song.get("videoId") or song.get("video_id", "") # Only cache if the song is still in the active queue. @@ -680,6 +684,7 @@ class AudioPlayer(QObject): self._try_next_song() elif task_id == self._prefetch_task_id: self._prefetch_task_id = None + self._prefetch_video_id = None log.debug("Prefetch failed for task %d (harmless)", task_id) else: log.debug("Ignoring stale download failure for task %d", task_id) @@ -818,7 +823,7 @@ class AudioPlayer(QObject): # Already cached or already downloading. if vid in self._prefetch_cache: return - if vid == self._prefetch_task_id: + if vid == self._prefetch_video_id: return # Don't prefetch if it's already the active download target. if self._active_task_id is not None: @@ -831,6 +836,7 @@ class AudioPlayer(QObject): task_id = self._next_task_id self._next_task_id += 1 self._prefetch_task_id = task_id + self._prefetch_video_id = vid self._worker.download_requested.emit(task_id, next_song)