Clear LRC cache on track change and improve LRC fetching logic

This commit is contained in:
2025-09-26 13:45:39 -04:00
parent 47089cc7cb
commit 00614326a4
2 changed files with 25 additions and 39 deletions

View File

@@ -423,6 +423,10 @@ class Radio(FastAPI):
self.radio_util.now_playing[data.station] = next self.radio_util.now_playing[data.station] = next
next["start"] = time_started next["start"] = time_started
next["end"] = time_ends next["end"] = time_ends
# Clear the LRC cache for the station
self.lrc_cache.pop(data.station, None)
try: try:
background_tasks.add_task(self.radio_util.webhook_song_change, next, data.station) background_tasks.add_task(self.radio_util.webhook_song_change, next, data.station)
# Broadcast track change to WebSocket clients # 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"Sending LRC to client for station {station}")
logging.info(f"Track data: {track_data}") logging.info(f"Track data: {track_data}")
try: try:
artist: Optional[str] = track_data.get("artist") # Always check if LRC is already cached
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) cached_lrc = self.lrc_cache.get(station)
if cached_lrc: if cached_lrc:
logging.info("LRC found in cache, sending immediately.") logging.info("Using cached LRC for client")
lrc_data: dict = { lrc_data: dict = {
"type": "lrc", "type": "lrc",
"data": cached_lrc, "data": cached_lrc,
@@ -645,6 +645,11 @@ class Radio(FastAPI):
await websocket.send_text(json.dumps(lrc_data)) await websocket.send_text(json.dumps(lrc_data))
return 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: if artist and title:
logging.info(f"Fetching LRC for {artist} - {title} (duration: {duration})") logging.info(f"Fetching LRC for {artist} - {title} (duration: {duration})")
lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song( lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song(
@@ -658,9 +663,8 @@ class Radio(FastAPI):
lrc = lrclib_result.lyrics lrc = lrclib_result.lyrics
source = "LRCLib" source = "LRCLib"
logging.info("LRC found via LRCLib fallback") 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: if lrc:
self.lrc_cache[station] = lrc # Cache the LRC regardless of source
lrc_data: dict = { lrc_data: dict = {
"type": "lrc", "type": "lrc",
"data": lrc, "data": lrc,
@@ -668,6 +672,8 @@ class Radio(FastAPI):
} }
await websocket.send_text(json.dumps(lrc_data)) await websocket.send_text(json.dumps(lrc_data))
logging.info("LRC sent to client") logging.info("LRC sent to client")
else:
logging.info("No LRC found from any source.")
except Exception as e: except Exception as e:
logging.error(f"Failed to send LRC to client: {e}") logging.error(f"Failed to send LRC to client: {e}")
@@ -677,32 +683,14 @@ class Radio(FastAPI):
return return
try: 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") artist: Optional[str] = track_data.get("artist")
title: Optional[str] = track_data.get("song") # Changed from "title" to "song" title: Optional[str] = track_data.get("song") # Changed from "title" to "song"
duration: Optional[int] = track_data.get("duration") 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: if artist and title:
logging.info(f"Broadcasting LRC fetch for {artist} - {title} (duration: {duration})") logging.info(f"Broadcasting LRC fetch for {artist} - {title} (duration: {duration})")
lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song( lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song(
@@ -716,7 +704,7 @@ class Radio(FastAPI):
lrc = lrclib_result.lyrics lrc = lrclib_result.lyrics
source = "LRCLib" source = "LRCLib"
logging.info("LRC found via LRCLib fallback") 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}") logging.info(f"LRC fetched for broadcast: {lrc is not None}")
if lrc: if lrc:
lrc_data: dict = { lrc_data: dict = {
@@ -733,8 +721,6 @@ class Radio(FastAPI):
except Exception as e: except Exception as e:
logging.warning(f"Failed to send LRC to client: {e}") logging.warning(f"Failed to send LRC to client: {e}")
disconnected_clients.add(websocket) disconnected_clients.add(websocket)
# Remove failed connections
for websocket in disconnected_clients: for websocket in disconnected_clients:
self.active_connections[station].discard(websocket) self.active_connections[station].discard(websocket)
logging.info("LRC broadcasted to clients") logging.info("LRC broadcasted to clients")

View File

@@ -166,12 +166,12 @@ class DataUtils:
if not reg_helper: if not reg_helper:
continue continue
reg_helper = reg_helper[0] reg_helper = reg_helper[0]
logging.debug( # logging.debug(
"Reg helper: %s for line: %s; len: %s", # "Reg helper: %s for line: %s; len: %s",
reg_helper, # reg_helper,
line, # line,
len(reg_helper), # len(reg_helper),
) # )
_timetag = reg_helper[0] _timetag = reg_helper[0]
if not reg_helper[1].strip(): if not reg_helper[1].strip():
continue continue