revisions

This commit is contained in:
codey 2025-01-14 11:10:13 -05:00
parent 3c23bc93f6
commit f3336b09ac
5 changed files with 58 additions and 37 deletions

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3.12 #!/usr/bin/env python3.12
# pylint: disable=wrong-import-order # pylint: disable=wrong-import-order, wrong-import-position
from typing import Optional from typing import Optional
from lyric_search_new.constructors import LyricsResult from lyric_search_new.constructors import LyricsResult
@ -22,6 +22,7 @@ class Aggregate:
self.exclude_methods = exclude_methods self.exclude_methods = exclude_methods
async def search(self, artist: str, song: str) -> Optional[LyricsResult]: async def search(self, artist: str, song: str) -> Optional[LyricsResult]:
"""Aggregate Search"""
cache_search = cache.Cache() cache_search = cache.Cache()
genius_search = genius.Genius() genius_search = genius.Genius()
lrclib_search = lrclib.LRCLib() lrclib_search = lrclib.LRCLib()

View File

@ -1,8 +1,10 @@
#!/usr/bin/env python3.12 #!/usr/bin/env python3.12
# pylint: disable=wrong-import-position # pylint: disable=wrong-import-order, wrong-import-position bare-except, broad-exception-caught
import os import os
import logging
import sys import sys
import traceback
sys.path.insert(1,'..') sys.path.insert(1,'..')
sys.path.insert(1,'.') sys.path.insert(1,'.')
from typing import Optional from typing import Optional
@ -10,6 +12,9 @@ import aiosqlite as sqlite3
from lyric_search_new import utils from lyric_search_new import utils
from lyric_search_new.constructors import LyricsResult from lyric_search_new.constructors import LyricsResult
logger = logging.getLogger()
log_level = logging.getLevelName(logger.level)
class Cache: class Cache:
"""Cache Search Module""" """Cache Search Module"""
def __init__(self): def __init__(self):
@ -44,32 +49,38 @@ class Cache:
""" """
artist = artist.strip().lower() artist = artist.strip().lower()
song = song.strip().lower() song = song.strip().lower()
async with sqlite3.connect(self.cache_db, timeout=2) as db_conn: try:
await db_conn.enable_load_extension(True) async with sqlite3.connect(self.cache_db, timeout=2) as db_conn:
for ext in self.sqlite_exts: await db_conn.enable_load_extension(True)
await db_conn.load_extension(ext) for ext in self.sqlite_exts:
async with await db_conn.executescript(self.cache_pre_query) as _db_cursor: await db_conn.load_extension(ext)
search_query = 'SELECT id, artist, song, lyrics, src, confidence FROM lyrics\ async with await db_conn.executescript(self.cache_pre_query) as _db_cursor:
WHERE editdist3((artist || " " || song), (? || " " || ?))\ search_query = 'SELECT id, artist, song, lyrics, src, confidence FROM lyrics\
<= 410 ORDER BY editdist3((artist || " " || song), ?) ASC LIMIT 10' WHERE editdist3((artist || " " || song), (? || " " || ?))\
search_params = (artist.strip(), song.strip(), <= 410 ORDER BY editdist3((artist || " " || song), ?) ASC LIMIT 10'
f"{artist.strip()} {song.strip()}") search_params = (artist.strip(), song.strip(),
async with await _db_cursor.execute(search_query, search_params) as db_cursor: f"{artist.strip()} {song.strip()}")
results = await db_cursor.fetchall() async with await _db_cursor.execute(search_query, search_params) as db_cursor:
result_tracks = [] results = await db_cursor.fetchall()
for track in results: result_tracks = []
(_id, _artist, _song, _lyrics, _src, _confidence) = track for track in results:
result_tracks.append((_id, f"{_artist} - {_song}")) (_id, _artist, _song, _lyrics, _src, _confidence) = track
input_track = f"{artist} - {song}" result_tracks.append((_id, f"{_artist} - {_song}"))
matcher = utils.TrackMatcher() input_track = f"{artist} - {song}"
best_match = matcher.find_best_match(input_track=input_track, matcher = utils.TrackMatcher()
candidate_tracks=result_tracks) best_match = matcher.find_best_match(input_track=input_track,
if not best_match: candidate_tracks=result_tracks)
return None if not best_match:
(candidate, confidence) = best_match return None
return self.get_matched(sqlite_rows=results, (candidate, confidence) = best_match
matched_candidate=candidate, logging.info("Result found on %s", self.label)
confidence=confidence) return self.get_matched(sqlite_rows=results,
matched_candidate=candidate,
confidence=confidence)
except:
if log_level == "DEBUG":
traceback.print_exc()
return

View File

@ -1,9 +1,10 @@
#!/usr/bin/env python3.12 #!/usr/bin/env python3.12
# pylint: disable=bare-except, broad-exception-caught, wrong-import-position # pylint: disable=bare-except, broad-exception-caught, wrong-import-order, wrong-import-position
import sys import sys
sys.path.insert(1,'..') sys.path.insert(1,'..')
import traceback import traceback
import logging
from aiohttp import ClientTimeout, ClientSession from aiohttp import ClientTimeout, ClientSession
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import html as htm import html as htm
@ -12,8 +13,12 @@ from . import common
from lyric_search_new import utils from lyric_search_new import utils
from lyric_search_new.constructors import LyricsResult from lyric_search_new.constructors import LyricsResult
logger = logging.getLogger()
log_level = logging.getLevelName(logger.level)
class InvalidResponseException(Exception): class InvalidResponseException(Exception):
""" """
InvalidResponseException
""" """
class Genius: class Genius:
@ -94,6 +99,7 @@ class Genius:
returned_lyrics = self.datautils.scrub_lyrics(returned_lyrics) returned_lyrics = self.datautils.scrub_lyrics(returned_lyrics)
artist = track.split(" - ", maxsplit=1)[0] artist = track.split(" - ", maxsplit=1)[0]
song = track.split(" - ", maxsplit=1)[1] song = track.split(" - ", maxsplit=1)[1]
logging.info("Result found on %s", self.label)
return LyricsResult(artist=artist, return LyricsResult(artist=artist,
song=song, song=song,
src=self.label, src=self.label,
@ -101,7 +107,8 @@ class Genius:
confidence=confidence) confidence=confidence)
except: except:
traceback.print_exc() if log_level == "DEBUG":
traceback.print_exc()
return return

View File

@ -4,11 +4,15 @@
import sys import sys
sys.path.insert(1,'..') sys.path.insert(1,'..')
import traceback import traceback
import logging
from aiohttp import ClientTimeout, ClientSession from aiohttp import ClientTimeout, ClientSession
from lyric_search_new import utils from lyric_search_new import utils
from lyric_search_new.constructors import LyricsResult from lyric_search_new.constructors import LyricsResult
from . import common from . import common
logger = logging.getLogger()
log_level = logging.getLevelName(logger.level)
class InvalidResponseException(Exception): class InvalidResponseException(Exception):
""" """
Invalid Response Exception Invalid Response Exception
@ -68,14 +72,15 @@ class LRCLib:
candidate_tracks=[(0, returned_track)]) candidate_tracks=[(0, returned_track)])
if not confidence: if not confidence:
return # No suitable match found return # No suitable match found
print("Returning!") logging.info("Result found on %s", self.label)
return LyricsResult(artist=returned_artist, return LyricsResult(artist=returned_artist,
song=returned_song, song=returned_song,
src=self.label, src=self.label,
lyrics=returned_lyrics, lyrics=returned_lyrics,
confidence=confidence) confidence=confidence)
except: except:
traceback.print_exc() if log_level == "DEBUG":
traceback.print_exc()
return return

View File

@ -34,8 +34,6 @@ class TrackMatcher:
# Normalize input track # Normalize input track
input_track = self._normalize_string(input_track) input_track = self._normalize_string(input_track)
print(f"input_track: {input_track}")
best_match = None best_match = None
best_score = 0 best_score = 0
@ -64,7 +62,6 @@ class TrackMatcher:
""" """
# Remove special characters and convert to lowercase # Remove special characters and convert to lowercase
text = regex.sub(r'[^\w\s-]', '', text).lower() text = regex.sub(r'[^\w\s-]', '', text).lower()
print(f"Text: {text}")
# Normalize spaces # Normalize spaces
text = ' '.join(text.split()) text = ' '.join(text.split())
return text return text
@ -89,7 +86,7 @@ class DataUtils:
Data Utils Data Utils
""" """
def scrub_lyrics(self, lyrics: str) -> str: def scrub_lyrics(self, lyrics: str) -> str:
# Regex chain """Regex Chain"""
lyrics = regex.sub(r'(\[.*?\])(\s){0,}(\:){0,1}', '', lyrics) lyrics = regex.sub(r'(\[.*?\])(\s){0,}(\:){0,1}', '', lyrics)
lyrics = regex.sub(r'(\d?)(Embed\b)', '', lyrics, flags=regex.IGNORECASE) lyrics = regex.sub(r'(\d?)(Embed\b)', '', lyrics, flags=regex.IGNORECASE)
lyrics = regex.sub(r'\n{2}', '\n', lyrics) # Gaps between verses lyrics = regex.sub(r'\n{2}', '\n', lyrics) # Gaps between verses