This commit is contained in:
2025-01-15 20:17:49 -05:00
parent b2bb724826
commit c09f72803e
7 changed files with 128 additions and 25 deletions

View File

@@ -2,6 +2,8 @@
# pylint: disable=wrong-import-order, wrong-import-position bare-except, broad-exception-caught
import os
import time
import regex
import logging
import sys
import traceback
@@ -39,9 +41,67 @@ class Cache:
src=f"{original_src} (cached, id: {_id})",
confidence=confidence)
return None
async def check_existence(self, artistsong: str) -> Optional[bool]:
"""
Check whether lyrics are already stored for track
@artistsong: artist and song in artist\nsong format
"""
logging.debug("Checking whether %s is already stored",
artistsong.replace("\n", " - "))
check_query = "SELECT id FROM lyrics WHERE artistsong LIKE ? LIMIT 1"
params = (artistsong,)
async with sqlite3.connect(self.cache_db, timeout=2) as db_conn:
async with db_conn.execute(check_query, params) as db_cursor:
result = await db_cursor.fetchone()
if result:
logging.debug("%s is already stored.",
artistsong.replace("\n", " - "))
return True
logging.debug("%s cleared to be stored.",
artistsong)
return False
async def store(self, lyr_result: LyricsResult) -> None:
"""
store
@lyr_result: the returned lyrics to cache
"""
logging.info("Storing %s",
f"{lyr_result.artist} - {lyr_result.song}")
if lyr_result.src.lower() == "cache":
logging.info("Skipping cache storage - returned LyricsResult originated from cache")
return
artistsong = f"{lyr_result.artist}\n{lyr_result.song}"
if await self.check_existence(artistsong):
logging.info("Skipping cache storage - %s is already stored.",
artistsong.replace("\n", " - "))
return
try:
lyrics = regex.sub(r'(<br>|\n|\r\n)', ' / ', lyr_result.lyrics.strip())
lyrics = regex.sub(r'\s{2,}', ' ', lyrics)
insert_query = "INSERT INTO lyrics (src, date_retrieved, artist, song, artistsong, confidence, lyrics) VALUES(?, ?, ?, ?, ?, ?, ?)"
params = (lyr_result.src, time.time(), lyr_result.artist,
lyr_result.song, artistsong, lyr_result.confidence, lyrics)
async with sqlite3.connect(self.cache_db, timeout=2) as db_conn:
async with db_conn.execute(insert_query, params) as _cursor:
await db_conn.commit()
logging.info("Stored %s!", artistsong.replace("\n", " - "))
except:
logging.critical("Cache storage error!")
traceback.print_exc()
async def search(self, artist: str, song: str) -> Optional[LyricsResult]:
"""
search
@artist: the artist to search
@song: the song to search
Returns:
@@ -52,6 +112,8 @@ class Cache:
song: str = song.strip().lower()
search_params: Optional[tuple] = None
random_search: bool = False
time_start: float = time.time()
logging.info("Searching %s - %s on %s",
artist, song, self.label)
async with sqlite3.connect(self.cache_db, timeout=2) as db_conn:
@@ -60,8 +122,8 @@ class Cache:
await db_conn.load_extension(ext)
async with await db_conn.executescript(self.cache_pre_query) as _db_cursor:
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'
WHERE editdist3((lower(artist) || " " || lower(song)), (? || " " || ?))\
<= 410 ORDER BY editdist3((lower(artist) || " " || lower(song)), ?) ASC LIMIT 10'
search_params: tuple = (artist.strip(), song.strip(),
f"{artist.strip()} {song.strip()}")
if artist == "!" and song == "!":
@@ -85,9 +147,13 @@ class Cache:
return None
(candidate, confidence) = best_match
logging.info("Result found on %s", self.label)
return self.get_matched(sqlite_rows=results,
matched = self.get_matched(sqlite_rows=results,
matched_candidate=candidate,
confidence=confidence)
time_end: float = time.time()
time_diff: float = time_end - time_start
matched.time = time_diff
return matched
except:
if log_level == "DEBUG":
traceback.print_exc()