0
This commit is contained in:
@@ -18,17 +18,17 @@ log_level = logging.getLevelName(logger.level)
|
||||
class Cache:
|
||||
"""Cache Search Module"""
|
||||
def __init__(self):
|
||||
self.cache_db = os.path.join("/", "var",
|
||||
self.cache_db: str = os.path.join("/", "var",
|
||||
"lib", "singerdbs",
|
||||
"cached_lyrics.db")
|
||||
|
||||
self.cache_pre_query = "pragma journal_mode = WAL; pragma synchronous = normal; pragma temp_store = memory; pragma mmap_size = 30000000000;"
|
||||
self.sqlite_exts = ['/usr/local/lib/python3.11/dist-packages/spellfix1.cpython-311-x86_64-linux-gnu.so']
|
||||
self.label = "Cache"
|
||||
self.cache_pre_query: str = "pragma journal_mode = WAL; pragma synchronous = normal; pragma temp_store = memory; pragma mmap_size = 30000000000;"
|
||||
self.sqlite_exts: list[str] = ['/usr/local/lib/python3.11/dist-packages/spellfix1.cpython-311-x86_64-linux-gnu.so']
|
||||
self.label: str = "Cache"
|
||||
|
||||
def get_matched(self, sqlite_rows, matched_candidate, confidence) -> Optional[LyricsResult]:
|
||||
def get_matched(self, sqlite_rows: list[sqlite3.Row], matched_candidate: tuple, confidence: float) -> Optional[LyricsResult]:
|
||||
"""Get Matched Result"""
|
||||
matched_id = matched_candidate[0]
|
||||
matched_id: int = matched_candidate[0]
|
||||
for row in sqlite_rows:
|
||||
if row[0] == matched_id:
|
||||
(_id, artist, song, lyrics, original_src, _confidence) = row
|
||||
@@ -40,7 +40,7 @@ class Cache:
|
||||
confidence=confidence)
|
||||
return None
|
||||
|
||||
async def search(self, artist: str, song: str):
|
||||
async def search(self, artist: str, song: str) -> Optional[LyricsResult]:
|
||||
"""
|
||||
@artist: the artist to search
|
||||
@song: the song to search
|
||||
@@ -48,8 +48,8 @@ class Cache:
|
||||
- LyricsResult corresponding to nearest match found (if found), **None** otherwise
|
||||
"""
|
||||
try:
|
||||
artist = artist.strip().lower()
|
||||
song = song.strip().lower()
|
||||
artist: str = artist.strip().lower()
|
||||
song: str = song.strip().lower()
|
||||
logging.info("Searching %s - %s on %s",
|
||||
artist, song, self.label)
|
||||
async with sqlite3.connect(self.cache_db, timeout=2) as db_conn:
|
||||
@@ -57,20 +57,20 @@ class Cache:
|
||||
for ext in self.sqlite_exts:
|
||||
await db_conn.load_extension(ext)
|
||||
async with await db_conn.executescript(self.cache_pre_query) as _db_cursor:
|
||||
search_query = 'SELECT id, artist, song, lyrics, src, confidence FROM lyrics\
|
||||
search_query: str = 'SELECT id, artist, song, lyrics, src, confidence FROM lyrics\
|
||||
WHERE editdist3((artist || " " || song), (? || " " || ?))\
|
||||
<= 410 ORDER BY editdist3((artist || " " || song), ?) ASC LIMIT 10'
|
||||
search_params = (artist.strip(), song.strip(),
|
||||
search_params: tuple = (artist.strip(), song.strip(),
|
||||
f"{artist.strip()} {song.strip()}")
|
||||
async with await _db_cursor.execute(search_query, search_params) as db_cursor:
|
||||
results = await db_cursor.fetchall()
|
||||
result_tracks = []
|
||||
results: list = await db_cursor.fetchall()
|
||||
result_tracks: list = []
|
||||
for track in results:
|
||||
(_id, _artist, _song, _lyrics, _src, _confidence) = track
|
||||
result_tracks.append((_id, f"{_artist} - {_song}"))
|
||||
input_track = f"{artist} - {song}"
|
||||
input_track: str = f"{artist} - {song}"
|
||||
matcher = utils.TrackMatcher()
|
||||
best_match = matcher.find_best_match(input_track=input_track,
|
||||
best_match: tuple|None = matcher.find_best_match(input_track=input_track,
|
||||
candidate_tracks=result_tracks)
|
||||
if not best_match:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user