166 lines
7.4 KiB
Python
Raw Normal View History

2025-01-12 20:19:48 -05:00
#!/usr/bin/env python3.12
2025-01-14 11:10:13 -05:00
# pylint: disable=wrong-import-order, wrong-import-position bare-except, broad-exception-caught
2025-01-12 20:19:48 -05:00
2025-01-13 20:47:39 -05:00
import os
2025-01-15 20:17:49 -05:00
import time
import regex
2025-01-14 11:10:13 -05:00
import logging
2025-01-13 20:47:39 -05:00
import sys
2025-01-14 11:10:13 -05:00
import traceback
2025-01-13 20:47:39 -05:00
sys.path.insert(1,'..')
2025-01-14 07:45:34 -05:00
sys.path.insert(1,'.')
2025-01-13 20:47:39 -05:00
from typing import Optional
2025-01-14 07:45:34 -05:00
import aiosqlite as sqlite3
2025-01-13 20:47:39 -05:00
from lyric_search_new import utils
from lyric_search_new.constructors import LyricsResult
2025-01-14 11:10:13 -05:00
logger = logging.getLogger()
log_level = logging.getLevelName(logger.level)
2025-01-12 20:19:48 -05:00
class Cache:
"""Cache Search Module"""
def __init__(self):
2025-01-14 14:17:18 -05:00
self.cache_db: str = os.path.join("/", "var",
2025-01-13 20:47:39 -05:00
"lib", "singerdbs",
"cached_lyrics.db")
2025-01-14 14:17:18 -05:00
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"
2025-01-13 20:47:39 -05:00
2025-01-14 14:17:18 -05:00
def get_matched(self, sqlite_rows: list[sqlite3.Row], matched_candidate: tuple, confidence: float) -> Optional[LyricsResult]:
2025-01-14 07:45:34 -05:00
"""Get Matched Result"""
2025-01-14 14:17:18 -05:00
matched_id: int = matched_candidate[0]
2025-01-13 20:47:39 -05:00
for row in sqlite_rows:
if row[0] == matched_id:
(_id, artist, song, lyrics, original_src, _confidence) = row
return LyricsResult(
artist=artist,
song=song,
lyrics=lyrics,
src=f"{original_src} (cached, id: {_id})",
confidence=confidence)
return None
2025-01-15 20:17:49 -05:00
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"
2025-01-15 20:19:45 -05:00
params = (f"%{artistsong}%",)
2025-01-15 20:17:49 -05:00
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)
2025-01-15 20:21:19 -05:00
insert_query = "INSERT INTO lyrics (src, date_retrieved, artist, song, artistsong, confidence, lyrics)\
VALUES(?, ?, ?, ?, ?, ?, ?)"
2025-01-15 20:17:49 -05:00
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()
2025-01-13 20:47:39 -05:00
2025-01-16 07:14:36 -05:00
async def search(self, artist: str, song: str, **kwargs) -> Optional[LyricsResult]:
2025-01-13 20:47:39 -05:00
"""
2025-01-15 20:17:49 -05:00
search
2025-01-13 20:47:39 -05:00
@artist: the artist to search
@song: the song to search
Returns:
- LyricsResult corresponding to nearest match found (if found), **None** otherwise
"""
2025-01-14 11:10:13 -05:00
try:
2025-01-14 14:17:18 -05:00
artist: str = artist.strip().lower()
song: str = song.strip().lower()
2025-01-14 18:37:49 -05:00
search_params: Optional[tuple] = None
random_search: bool = False
2025-01-15 20:17:49 -05:00
time_start: float = time.time()
2025-01-14 11:13:39 -05:00
logging.info("Searching %s - %s on %s",
artist, song, self.label)
2025-01-14 11:10:13 -05:00
async with sqlite3.connect(self.cache_db, timeout=2) as db_conn:
await db_conn.enable_load_extension(True)
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:
2025-01-14 14:17:18 -05:00
search_query: str = 'SELECT id, artist, song, lyrics, src, confidence FROM lyrics\
2025-01-15 20:17:49 -05:00
WHERE editdist3((lower(artist) || " " || lower(song)), (? || " " || ?))\
<= 410 ORDER BY editdist3((lower(artist) || " " || lower(song)), ?) ASC LIMIT 10'
2025-01-14 14:17:18 -05:00
search_params: tuple = (artist.strip(), song.strip(),
2025-01-14 18:37:49 -05:00
f"{artist.strip()} {song.strip()}")
if artist == "!" and song == "!":
random_search: bool = True
2025-01-15 20:21:19 -05:00
search_query: str = 'SELECT id, artist, song, lyrics, src, confidence\
FROM lyrics ORDER BY RANDOM() LIMIT 1'
2025-01-14 18:37:49 -05:00
search_params = None
2025-01-14 11:10:13 -05:00
async with await _db_cursor.execute(search_query, search_params) as db_cursor:
2025-01-14 14:17:18 -05:00
results: list = await db_cursor.fetchall()
result_tracks: list = []
2025-01-14 11:10:13 -05:00
for track in results:
(_id, _artist, _song, _lyrics, _src, _confidence) = track
result_tracks.append((_id, f"{_artist} - {_song}"))
2025-01-14 14:17:18 -05:00
input_track: str = f"{artist} - {song}"
2025-01-14 11:10:13 -05:00
matcher = utils.TrackMatcher()
2025-01-14 18:37:49 -05:00
if not random_search:
best_match: tuple|None = matcher.find_best_match(input_track=input_track,
2025-01-14 11:10:13 -05:00
candidate_tracks=result_tracks)
2025-01-14 18:37:49 -05:00
else:
best_match = (result_tracks[0], float(1))
2025-01-14 11:10:13 -05:00
if not best_match:
return None
(candidate, confidence) = best_match
logging.info("Result found on %s", self.label)
2025-01-15 20:17:49 -05:00
matched = self.get_matched(sqlite_rows=results,
2025-01-14 11:10:13 -05:00
matched_candidate=candidate,
confidence=confidence)
2025-01-15 20:17:49 -05:00
time_end: float = time.time()
time_diff: float = time_end - time_start
matched.time = time_diff
return matched
2025-01-14 11:10:13 -05:00
except:
if log_level == "DEBUG":
traceback.print_exc()
return
2025-01-13 20:47:39 -05:00