This commit is contained in:
codey 2025-01-20 05:47:09 -05:00
parent 31c8f0ac04
commit 1c7a64bc58
4 changed files with 45 additions and 10 deletions

View File

@ -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

View File

@ -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:

View File

@ -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"""
@ -55,6 +61,24 @@ class RedisCache:
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]:
"""
Search Redis Cache
@ -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]

View File

@ -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,