From d58f2f9413d459500b088a4d87524112fe571c08 Mon Sep 17 00:00:00 2001 From: codey Date: Thu, 16 Jan 2025 07:14:36 -0500 Subject: [PATCH] tweaks --- endpoints/lyric_search.py | 3 ++- lyric_search_new/sources/aggregate.py | 8 ++++++-- lyric_search_new/sources/cache.py | 2 +- lyric_search_new/sources/genius.py | 4 ++-- lyric_search_new/sources/lrclib.py | 16 ++++++++++------ 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/endpoints/lyric_search.py b/endpoints/lyric_search.py index e146114..bab81a3 100644 --- a/endpoints/lyric_search.py +++ b/endpoints/lyric_search.py @@ -185,7 +185,8 @@ class LyricSearch(FastAPI): excluded_sources = data.excluded_sources aggregate_search = aggregate.Aggregate(exclude_methods=excluded_sources) - result = await aggregate_search.search(data.a, data.s) + plain_lyrics = not data.lrc + result = await aggregate_search.search(data.a, data.s, plain_lyrics) if not result: return { 'err': True, diff --git a/lyric_search_new/sources/aggregate.py b/lyric_search_new/sources/aggregate.py index c59b14c..c0a74b0 100644 --- a/lyric_search_new/sources/aggregate.py +++ b/lyric_search_new/sources/aggregate.py @@ -21,8 +21,11 @@ class Aggregate: exclude_methods: list = [] self.exclude_methods = exclude_methods - async def search(self, artist: str, song: str) -> Optional[LyricsResult]: + async def search(self, artist: str, song: str, plain: bool = True) -> Optional[LyricsResult]: """Aggregate Search""" + if not plain: + logging.info("LRCs requested, limiting search to LRCLib") + self.exclude_methods = ["genius", "cache"] logging.info("Performing aggregate search") cache_search = cache.Cache() genius_search = genius.Genius() @@ -35,7 +38,8 @@ class Aggregate: if source.label.lower() in self.exclude_methods: logging.info("Skipping source: %s, excluded.", source.label) continue - search_result = await source.search(artist, song) + search_result = await source.search(artist=artist, song=song, + plain=plain) if search_result: break logging.info("%s: NOT FOUND!", source.label) diff --git a/lyric_search_new/sources/cache.py b/lyric_search_new/sources/cache.py index e207a5f..90512af 100644 --- a/lyric_search_new/sources/cache.py +++ b/lyric_search_new/sources/cache.py @@ -100,7 +100,7 @@ class Cache: - async def search(self, artist: str, song: str) -> Optional[LyricsResult]: + async def search(self, artist: str, song: str, **kwargs) -> Optional[LyricsResult]: """ search @artist: the artist to search diff --git a/lyric_search_new/sources/genius.py b/lyric_search_new/sources/genius.py index b899661..ad1c89e 100644 --- a/lyric_search_new/sources/genius.py +++ b/lyric_search_new/sources/genius.py @@ -32,12 +32,12 @@ class Genius: self.genius_url: str = private.GENIUS_URL self.genius_search_url: str = f'{self.genius_url}api/search/song?q=' self.headers: dict = common.SCRAPE_HEADERS - self.timeout = ClientTimeout(connect=2, sock_read=4) + self.timeout = ClientTimeout(connect=3, sock_read=5) self.datautils = utils.DataUtils() self.matcher = utils.TrackMatcher() self.cache = cache.Cache() - async def search(self, artist: str, song: str) -> Optional[LyricsResult]: + async def search(self, artist: str, song: str, **kwargs) -> Optional[LyricsResult]: """ @artist: the artist to search @song: the song to search diff --git a/lyric_search_new/sources/lrclib.py b/lyric_search_new/sources/lrclib.py index a28c483..60a9aea 100644 --- a/lyric_search_new/sources/lrclib.py +++ b/lyric_search_new/sources/lrclib.py @@ -32,7 +32,7 @@ class LRCLib: self.matcher = utils.TrackMatcher() self.cache = cache.Cache() - async def search(self, artist: str, song: str) -> Optional[LyricsResult]: + async def search(self, artist: str, song: str, plain: bool = True) -> Optional[LyricsResult]: """ @artist: the artist to search @song: the song to search @@ -81,14 +81,18 @@ class LRCLib: if not isinstance(search_data[best_match_id]['trackName'], str): raise InvalidResponseException(f"Invalid JSON: Cannot find trackName key.\n{search_data}") - - if not isinstance(search_data[best_match_id]['plainLyrics'], str): - raise InvalidResponseException(f"Invalid JSON: Cannot find plainLyrics key.\n{search_data}") returned_artist: str = search_data[best_match_id]['artistName'] returned_song: str = search_data[best_match_id]['trackName'] - returned_lyrics: str = search_data[best_match_id]['plainLyrics'] - returned_lyrics = self.datautils.scrub_lyrics(returned_lyrics) + if plain: + if not isinstance(search_data[best_match_id]['plainLyrics'], str): + raise InvalidResponseException(f"Invalid JSON: Cannot find plainLyrics key.\n{search_data}") + returned_lyrics: str = search_data[best_match_id]['plainLyrics'] + returned_lyrics = self.datautils.scrub_lyrics(returned_lyrics) + else: + if not isinstance(search_data[best_match_id]['syncedLyrics'], str): + raise InvalidResponseException(f"Invalid JSON: Cannot find syncedLyrics key.\n{search_data}") + returned_lyrics: str = search_data[best_match_id]['syncedLyrics'] returned_track: str = f"{returned_artist} - {returned_song}" (_matched, confidence) = self.matcher.find_best_match(input_track=input_track, candidate_tracks=[(0, returned_track)])