diff --git a/endpoints/lyric_search.py b/endpoints/lyric_search.py index cf7f644..c5b04a3 100644 --- a/endpoints/lyric_search.py +++ b/endpoints/lyric_search.py @@ -121,7 +121,7 @@ class LyricSearch(FastAPI): if not data.a or not data.s: raise HTTPException(detail="Invalid request", status_code=500) - aggregate_search = aggregate.Aggregate() + aggregate_search = aggregate.Aggregate(exclude_methods=None) result = await aggregate_search.search(data.a, data.s) if not result: return { diff --git a/lyric_search_new/sources/aggregate.py b/lyric_search_new/sources/aggregate.py index 76d0937..569883c 100644 --- a/lyric_search_new/sources/aggregate.py +++ b/lyric_search_new/sources/aggregate.py @@ -4,12 +4,15 @@ from typing import Optional from lyric_search_new.constructors import LyricsResult import sys -sys.path.insert(1,'..') +import logging sys.path.insert(1,'..') from . import cache from . import genius from . import lrclib +logger = logging.getLogger() +logger.setLevel(logging.INFO) + class Aggregate: """Aggregate all source methods""" @@ -22,20 +25,17 @@ class Aggregate: cache_search = cache.Cache() genius_search = genius.Genius() lrclib_search = lrclib.LRCLib() - search = None - if "cache" not in self.exclude_methods: - # First, try cache - search = await cache_search.search(artist, song) - if not search: - print("Cache: NOT FOUND!") - # Then try LRCLib - if "lrclib" not in self.exclude_methods: - search = await lrclib_search.search(artist, song) - if not search: - print("LRCLib: Not found!") - # Then try Genius - if "genius" in self.exclude_methods: - return # Skipped last possible source, return None - search = await genius_search.search(artist, song) + sources = [cache_search, + lrclib_search, + genius_search] + search_result = None + for source in sources: + if source.label.lower() in self.exclude_methods: + logging.debug("Skipping source: %s, excluded.") + continue + search_result = await source.search(artist, song) + if search_result: + return search_result + logging.debug("%s: NOT FOUND!", str(source)) - return search + return search_result diff --git a/lyric_search_new/sources/cache.py b/lyric_search_new/sources/cache.py index 36c0ac8..35ab52f 100644 --- a/lyric_search_new/sources/cache.py +++ b/lyric_search_new/sources/cache.py @@ -19,6 +19,7 @@ class Cache: 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" def get_matched(self, sqlite_rows, matched_candidate, confidence) -> Optional[LyricsResult]: """Get Matched Result""" diff --git a/lyric_search_new/tests.py b/lyric_search_new/tests.py index 6219d5d..e869403 100644 --- a/lyric_search_new/tests.py +++ b/lyric_search_new/tests.py @@ -7,7 +7,7 @@ sys.path.insert(1, '.') import sources.cache, sources.genius, sources.aggregate, sources.lrclib test_artist = "hopsin" -test_song = "ill mind of hopsin 5" +test_song = "ill mind of fssfgdfhopsin 5" async def test_cache(artist, song): cache = sources.cache.Cache() @@ -48,6 +48,6 @@ async def test_aggregate(artist=None, song=None): loop = asyncio.new_event_loop() # loop.run_until_complete(test_genius()) -loop.run_until_complete(test_lrclib()) +# loop.run_until_complete(test_lrclib()) # loop.run_until_complete(test_cache(artist=test_artist, song=test_song)) -# loop.run_until_complete(test_aggregate()) +loop.run_until_complete(test_aggregate())