diff --git a/endpoints/__init__.py b/endpoints/__init__.py new file mode 100644 index 0000000..d09dd4d --- /dev/null +++ b/endpoints/__init__.py @@ -0,0 +1 @@ +# Radio endpoints package \ No newline at end of file diff --git a/lyric_search/sources/lrclib.py b/lyric_search/sources/lrclib.py index e9a5448..581d432 100644 --- a/lyric_search/sources/lrclib.py +++ b/lyric_search/sources/lrclib.py @@ -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, ) diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..67b9db6 --- /dev/null +++ b/utils/__init__.py @@ -0,0 +1 @@ +# Utils package \ No newline at end of file