Enhance LRC fetching logic by adding source tracking and improving type hints for artist, title, and duration
This commit is contained in:
@@ -625,27 +625,30 @@ 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 = track_data.get("artist")
|
artist: Optional[str] = track_data.get("artist")
|
||||||
title = track_data.get("song") # Changed from "title" to "song"
|
title: Optional[str] = track_data.get("song") # Changed from "title" to "song"
|
||||||
duration = track_data.get("duration")
|
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 = await self.sr_util.get_lrc_by_artist_song(
|
lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song(
|
||||||
artist, title, duration=duration
|
artist, title, duration=duration
|
||||||
)
|
)
|
||||||
|
source: str = "SR"
|
||||||
if not lrc:
|
if not lrc:
|
||||||
logging.info(f"No LRC from SR, trying LRCLib for {artist} - {title}")
|
logging.info(f"No LRC from SR, trying LRCLib for {artist} - {title}")
|
||||||
lrclib_result = await self.lrclib.search(artist, title, plain=False)
|
lrclib_result = await self.lrclib.search(artist, title, plain=False)
|
||||||
if lrclib_result and lrclib_result.lyrics and isinstance(lrclib_result.lyrics, str):
|
if lrclib_result and lrclib_result.lyrics and isinstance(lrclib_result.lyrics, str):
|
||||||
lrc = lrclib_result.lyrics
|
lrc = lrclib_result.lyrics
|
||||||
|
source = "LRCLib"
|
||||||
logging.info("LRC found via LRCLib fallback")
|
logging.info("LRC found via LRCLib fallback")
|
||||||
self.lrc_cache[station] = lrc
|
self.lrc_cache[station] = lrc
|
||||||
logging.info(f"LRC fetched: {lrc is not None}")
|
logging.info(f"LRC fetched: {lrc is not None}")
|
||||||
if lrc:
|
if lrc:
|
||||||
lrc_data = {
|
lrc_data: dict = {
|
||||||
"type": "lrc",
|
"type": "lrc",
|
||||||
"data": lrc
|
"data": lrc,
|
||||||
|
"source": source
|
||||||
}
|
}
|
||||||
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")
|
||||||
@@ -658,27 +661,30 @@ class Radio(FastAPI):
|
|||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
artist = track_data.get("artist")
|
artist: Optional[str] = track_data.get("artist")
|
||||||
title = track_data.get("song") # Changed from "title" to "song"
|
title: Optional[str] = track_data.get("song") # Changed from "title" to "song"
|
||||||
duration = track_data.get("duration")
|
duration: Optional[int] = track_data.get("duration")
|
||||||
|
|
||||||
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 = await self.sr_util.get_lrc_by_artist_song(
|
lrc: Optional[str] = await self.sr_util.get_lrc_by_artist_song(
|
||||||
artist, title, duration=duration
|
artist, title, duration=duration
|
||||||
)
|
)
|
||||||
|
source: str = "SR"
|
||||||
if not lrc:
|
if not lrc:
|
||||||
logging.info(f"No LRC from SR, trying LRCLib for {artist} - {title}")
|
logging.info(f"No LRC from SR, trying LRCLib for {artist} - {title}")
|
||||||
lrclib_result = await self.lrclib.search(artist, title, plain=False)
|
lrclib_result = await self.lrclib.search(artist, title, plain=False)
|
||||||
if lrclib_result and lrclib_result.lyrics and isinstance(lrclib_result.lyrics, str):
|
if lrclib_result and lrclib_result.lyrics and isinstance(lrclib_result.lyrics, str):
|
||||||
lrc = lrclib_result.lyrics
|
lrc = lrclib_result.lyrics
|
||||||
|
source = "LRCLib"
|
||||||
logging.info("LRC found via LRCLib fallback")
|
logging.info("LRC found via LRCLib fallback")
|
||||||
self.lrc_cache[station] = lrc
|
self.lrc_cache[station] = 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 = {
|
lrc_data: dict = {
|
||||||
"type": "lrc",
|
"type": "lrc",
|
||||||
"data": lrc
|
"data": lrc,
|
||||||
|
"source": source
|
||||||
}
|
}
|
||||||
|
|
||||||
# Send to all connected clients
|
# Send to all connected clients
|
||||||
|
Reference in New Issue
Block a user