From 1c7a64bc5881385b632b7beb4ce241462eb6148d Mon Sep 17 00:00:00 2001 From: codey Date: Mon, 20 Jan 2025 05:47:09 -0500 Subject: [PATCH] stoof --- endpoints/lyric_search.py | 12 +++++--- lyric_search_new/sources/aggregate.py | 3 ++ lyric_search_new/sources/redis_cache.py | 38 +++++++++++++++++++++---- lyric_search_new/utils.py | 2 +- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/endpoints/lyric_search.py b/endpoints/lyric_search.py index 1dd6dcb..75399b9 100644 --- a/endpoints/lyric_search.py +++ b/endpoints/lyric_search.py @@ -112,7 +112,8 @@ class LyricSearch(FastAPI): "IRC-KALI", "DISC-ACES", "DISC-HAVOC", - "IRC-SHARED" + "IRC-SHARED", + "LIMNORIA-SHARED", ] self.lrc_regex = regex.compile(r'\[([0-9]{2}:[0-9]{2})\.[0-9]{1,3}\](\s(.*)){0,}') @@ -143,8 +144,8 @@ class LyricSearch(FastAPI): async def song_typeahead_handler(self, data: ValidTypeAheadRequest): """Song Type Ahead Handler""" - if not isinstance(data.pre_query, str) or len(data.pre_query) < 2\ - or not isinstance(data.query, str) or len(data.query) < 2: + if not isinstance(data.pre_query, str)\ + or not isinstance(data.query, str|None): return { 'err': True, 'errorText': 'Invalid request', @@ -183,7 +184,10 @@ class LyricSearch(FastAPI): raise HTTPException(detail="Invalid request", status_code=500) if data.src.upper() not in self.acceptable_request_sources: - raise HTTPException(detail="Invalid request", status_code=500) + return { + 'err': True, + 'errorText': f'Unknown request source: {data.src.upper()}', + } search_artist: Optional[str] = data.a search_song: Optional[str] = data.s diff --git a/lyric_search_new/sources/aggregate.py b/lyric_search_new/sources/aggregate.py index 57b9799..c70e878 100644 --- a/lyric_search_new/sources/aggregate.py +++ b/lyric_search_new/sources/aggregate.py @@ -43,6 +43,8 @@ class Aggregate: sources: list = [cache_search, lrclib_search, genius_search] + if not plain: + sources = [lrclib_search] # Only LRCLib supported for synced lyrics search_result: Optional[LyricsResult] = None for source in sources: if source.label.lower() in self.exclude_methods: @@ -53,6 +55,7 @@ class Aggregate: if plain: logging.info("Skipping source: %s, excluded.", source.label) continue + search_result = await source.search(artist=artist, song=song, plain=plain) if search_result: diff --git a/lyric_search_new/sources/redis_cache.py b/lyric_search_new/sources/redis_cache.py index 2e0a3bc..449662a 100644 --- a/lyric_search_new/sources/redis_cache.py +++ b/lyric_search_new/sources/redis_cache.py @@ -8,6 +8,7 @@ import traceback import json import time import sys +import regex sys.path.insert(1,'..') from lyric_search_new import notifier from lyric_search_new.constructors import LyricsResult @@ -21,6 +22,7 @@ from . import private + logger = logging.getLogger() log_level = logging.getLevelName(logger.level) @@ -38,6 +40,10 @@ class RedisCache: self.redis_client = redis.Redis(password=private.REDIS_PW) self.notifier = notifier.DiscordNotifier() self.notify_warnings = True + self.regexes = [ + regex.compile(r'\-'), + regex.compile(r'[^a-zA-Z0-9\s]'), + ] async def create_index(self) -> None: """Create Index""" @@ -54,6 +60,24 @@ class RedisCache: raise RedisException(f"Redis: Failed to create index: {result}") except Exception as e: await self.notifier.send(f"ERROR @ {__file__}", f"Failed to create idx: {str(e)}") + + def sanitize_input(self, artist: str, song: str) -> tuple[str, str]: + """ + Sanitize artist/song input (convert to redis matchable fuzzy query) + Args: + artist: Input artist + song: Input song + Returns: + tuple[str, str]: Tuple containing the 2 output strings (artist, song) + """ + artist = self.regexes[0].sub(" ", artist) + artist = self.regexes[1].sub("", artist).strip() + song = self.regexes[0].sub(" ", song) + song = self.regexes[1].sub("", song).strip() + artist = " ".join([f"(%{artist_word}%)" for artist_word in artist.split(" ")]) + song = "".join([f"(%{song_word}%)" for song_word in song.split(" ")]) + return (artist, song) + async def search(self, **kwargs) -> list[tuple]: """ @@ -77,11 +101,15 @@ class RedisCache: raise RedisException("Lyric search not yet implemented") if not is_random_search: - artist = artist.replace("-", " ") - song = song.replace("-", " ") - search_res = await self.redis_client.ft().search( - Query(f"@artist:{artist} @song:{song}" - )) + (artist, song) = self.sanitize_input(artist=artist, + song=song) + search_res = await self.redis_client.ft().search(Query( + f"@artist:{artist} @song:{song}" + )) + + + + search_res_out = [(result['id'].split(":", maxsplit=1)[1], dict(json.loads(result['json']))) for result in search_res.docs] diff --git a/lyric_search_new/utils.py b/lyric_search_new/utils.py index 37279d3..ec7e1f3 100644 --- a/lyric_search_new/utils.py +++ b/lyric_search_new/utils.py @@ -140,7 +140,7 @@ class DataUtils: if not reg_helper[1].strip(): _words = "♪" else: - _words = reg_helper[1] + _words = reg_helper[1].strip() lrc_out.append({ "timeTag": _timetag, "words": _words,