Add duration parameter to LRCLib search method and create init files for endpoints and utils packages

This commit is contained in:
2025-09-26 12:10:17 -04:00
parent 66d13ad6e7
commit 8f59326c82
3 changed files with 20 additions and 11 deletions

1
endpoints/__init__.py Normal file
View File

@@ -0,0 +1 @@
# Radio endpoints package

View File

@@ -1,7 +1,5 @@
import sys
import time import time
sys.path.insert(1, "..")
import traceback import traceback
import logging import logging
from typing import Optional, Union from typing import Optional, Union
@@ -31,7 +29,7 @@ class LRCLib:
@retry(stop=stop_after_attempt(2), wait=wait_fixed(0.5)) @retry(stop=stop_after_attempt(2), wait=wait_fixed(0.5))
async def search( async def search(
self, artist: str, song: str, plain: Optional[bool] = True self, artist: str, song: str, plain: Optional[bool] = True, duration: Optional[int] = None
) -> Optional[LyricsResult]: ) -> Optional[LyricsResult]:
""" """
LRCLib Search LRCLib Search
@@ -42,8 +40,8 @@ class LRCLib:
Optional[LyricsResult]: The result, if found - None otherwise. Optional[LyricsResult]: The result, if found - None otherwise.
""" """
try: try:
artist: str = artist.strip().lower() artist = artist.strip().lower()
song: str = song.strip().lower() song = song.strip().lower()
time_start: float = time.time() time_start: float = time.time()
lrc_obj: Optional[list[dict]] = None lrc_obj: Optional[list[dict]] = None
@@ -57,6 +55,7 @@ class LRCLib:
params={ params={
"artist_name": artist, "artist_name": artist,
"track_name": song, "track_name": song,
**({"duration": duration} if duration else {}),
}, },
timeout=self.timeout, timeout=self.timeout,
headers=self.headers, headers=self.headers,
@@ -80,6 +79,10 @@ class LRCLib:
if not isinstance(search_data, list): if not isinstance(search_data, list):
raise InvalidLRCLibResponseException("Invalid JSON.") raise InvalidLRCLibResponseException("Invalid JSON.")
# Filter by duration if provided
if duration:
search_data = [r for r in search_data if abs(r.get("duration", 0) - duration) <= 5]
if plain: if plain:
possible_matches = [ possible_matches = [
( (
@@ -101,10 +104,13 @@ class LRCLib:
if isinstance(result["syncedLyrics"], str) if isinstance(result["syncedLyrics"], str)
] ]
best_match = None
try: try:
best_match = self.matcher.find_best_match( match_result = self.matcher.find_best_match(
input_track, possible_matches input_track, possible_matches # type: ignore
)[0] )
if match_result:
best_match = match_result[0]
except: # noqa except: # noqa
pass pass
@@ -145,11 +151,12 @@ class LRCLib:
] ]
lrc_obj = self.datautils.create_lrc_object(returned_lyrics) lrc_obj = self.datautils.create_lrc_object(returned_lyrics)
returned_track: str = f"{returned_artist} - {returned_song}" returned_track: str = f"{returned_artist} - {returned_song}"
(_matched, confidence) = self.matcher.find_best_match( match_result = self.matcher.find_best_match(
input_track=input_track, candidate_tracks=[(0, returned_track)] input_track=input_track, candidate_tracks=[(0, returned_track)]
) )
if not confidence: if not match_result:
return # No suitable match found return # No suitable match found
_matched, confidence = match_result
logging.info("Result found on %s", self.label) logging.info("Result found on %s", self.label)
time_end: float = time.time() time_end: float = time.time()
time_diff: float = time_end - time_start time_diff: float = time_end - time_start
@@ -157,7 +164,7 @@ class LRCLib:
artist=returned_artist, artist=returned_artist,
song=returned_song, song=returned_song,
src=self.label, src=self.label,
lyrics=returned_lyrics if plain else lrc_obj, lyrics=returned_lyrics if plain else lrc_obj, # type: ignore
confidence=confidence, confidence=confidence,
time=time_diff, time=time_diff,
) )

1
utils/__init__.py Normal file
View File

@@ -0,0 +1 @@
# Utils package