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

1
utils/__init__.py Normal file
View File

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