diff --git a/lyric_search_new/sources/cache.py b/lyric_search_new/sources/cache.py index f63fa36..9dc9386 100644 --- a/lyric_search_new/sources/cache.py +++ b/lyric_search_new/sources/cache.py @@ -80,9 +80,16 @@ class Cache: """ logging.debug("Checking whether %s is already stored", artistsong.replace("\n", " - ")) - check_query = "SELECT id FROM lyrics WHERE artistsong LIKE ? LIMIT 1" - params = (f"%{artistsong}%",) + check_query: str = 'SELECT id, artist, song FROM lyrics WHERE editdist3((lower(artist) || " " || lower(song)), (? || " " || ?))\ + <= 410 ORDER BY editdist3((lower(artist) || " " || lower(song)), ?) ASC LIMIT 1' + artistsong_split = artistsong.split("\n", maxsplit=1) + artist = artistsong_split[0].lower() + song = artistsong_split[1].lower() + params = (artist, song, artistsong.lower()) 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: async with await db_conn.execute(check_query, params) as db_cursor: result = await db_cursor.fetchone()