diff --git a/endpoints/radio.py b/endpoints/radio.py index c0d0c92..ecec084 100644 --- a/endpoints/radio.py +++ b/endpoints/radio.py @@ -423,6 +423,10 @@ class Radio(FastAPI): self.radio_util.now_playing[data.station] = next next["start"] = time_started next["end"] = time_ends + + # Clear the LRC cache for the station + self.lrc_cache.pop(data.station, None) + try: background_tasks.add_task(self.radio_util.webhook_song_change, next, data.station) # Broadcast track change to WebSocket clients @@ -629,14 +633,10 @@ class Radio(FastAPI): logging.info(f"Sending LRC to client for station {station}") logging.info(f"Track data: {track_data}") try: - artist: Optional[str] = track_data.get("artist") - title: Optional[str] = track_data.get("song") # Changed from "title" to "song" - duration: Optional[int] = track_data.get("duration") - - # Check if LRC is already cached + # Always check if LRC is already cached cached_lrc = self.lrc_cache.get(station) if cached_lrc: - logging.info("LRC found in cache, sending immediately.") + logging.info("Using cached LRC for client") lrc_data: dict = { "type": "lrc", "data": cached_lrc, @@ -645,6 +645,11 @@ class Radio(FastAPI): await websocket.send_text(json.dumps(lrc_data)) return + # Fetch LRC if not cached + artist: Optional[str] = track_data.get("artist") + title: Optional[str] = track_data.get("song") + duration: Optional[int] = track_data.get("duration") + if artist and title: logging.info(f"Fetching LRC for {artist} - {title} (duration: {duration})") lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song( @@ -658,9 +663,8 @@ class Radio(FastAPI): lrc = lrclib_result.lyrics source = "LRCLib" logging.info("LRC found via LRCLib fallback") - self.lrc_cache[station] = lrc # Cache the LRC for future use - logging.info(f"LRC fetched: {lrc is not None}") if lrc: + self.lrc_cache[station] = lrc # Cache the LRC regardless of source lrc_data: dict = { "type": "lrc", "data": lrc, @@ -668,6 +672,8 @@ class Radio(FastAPI): } await websocket.send_text(json.dumps(lrc_data)) logging.info("LRC sent to client") + else: + logging.info("No LRC found from any source.") except Exception as e: logging.error(f"Failed to send LRC to client: {e}") @@ -677,32 +683,14 @@ class Radio(FastAPI): return try: + # Clear the LRC cache for the station on track change + self.lrc_cache.pop(station, None) + + # Fetch LRC if not cached artist: Optional[str] = track_data.get("artist") title: Optional[str] = track_data.get("song") # Changed from "title" to "song" duration: Optional[int] = track_data.get("duration") - # Check if LRC is already cached - cached_lrc = self.lrc_cache.get(station) - if cached_lrc: - logging.info("LRC found in cache, broadcasting immediately.") - lrc_data: dict = { - "type": "lrc", - "data": cached_lrc, - "source": "Cache" - } - disconnected_clients = set() - for websocket in self.active_connections[station]: - try: - await websocket.send_text(json.dumps(lrc_data)) - except Exception as e: - logging.warning(f"Failed to send LRC to client: {e}") - disconnected_clients.add(websocket) - - # Remove failed connections - for websocket in disconnected_clients: - self.active_connections[station].discard(websocket) - return - if artist and title: logging.info(f"Broadcasting LRC fetch for {artist} - {title} (duration: {duration})") lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song( @@ -716,7 +704,7 @@ class Radio(FastAPI): lrc = lrclib_result.lyrics source = "LRCLib" logging.info("LRC found via LRCLib fallback") - self.lrc_cache[station] = lrc # Cache the LRC for future use + self.lrc_cache[station] = lrc # Cache the LRC logging.info(f"LRC fetched for broadcast: {lrc is not None}") if lrc: lrc_data: dict = { @@ -733,8 +721,6 @@ class Radio(FastAPI): except Exception as e: logging.warning(f"Failed to send LRC to client: {e}") disconnected_clients.add(websocket) - - # Remove failed connections for websocket in disconnected_clients: self.active_connections[station].discard(websocket) logging.info("LRC broadcasted to clients") diff --git a/lyric_search/utils.py b/lyric_search/utils.py index a46e2bb..2f874fa 100644 --- a/lyric_search/utils.py +++ b/lyric_search/utils.py @@ -166,12 +166,12 @@ class DataUtils: if not reg_helper: continue reg_helper = reg_helper[0] - logging.debug( - "Reg helper: %s for line: %s; len: %s", - reg_helper, - line, - len(reg_helper), - ) + # logging.debug( + # "Reg helper: %s for line: %s; len: %s", + # reg_helper, + # line, + # len(reg_helper), + # ) _timetag = reg_helper[0] if not reg_helper[1].strip(): continue