64 lines
2.3 KiB
Python
Raw Normal View History

2025-01-13 20:47:39 -05:00
#!/usr/bin/env python3.12
2025-01-14 11:10:13 -05:00
# pylint: disable=wrong-import-order, wrong-import-position
2025-01-13 20:47:39 -05:00
from typing import Optional
from lyric_search_new.constructors import LyricsResult
import sys
2025-01-14 10:52:53 -05:00
import logging
2025-01-13 20:47:39 -05:00
sys.path.insert(1,'..')
from . import cache
from . import genius
from . import lrclib
2025-01-14 07:45:34 -05:00
2025-01-14 10:52:53 -05:00
logger = logging.getLogger()
logger.setLevel(logging.INFO)
2025-01-13 20:47:39 -05:00
class Aggregate:
2025-01-19 07:01:07 -05:00
"""
Aggregate all source methods
"""
2025-01-13 20:47:39 -05:00
2025-01-19 07:09:05 -05:00
def __init__(self, exclude_methods=None) -> None:
2025-01-13 20:47:39 -05:00
if not exclude_methods:
2025-01-14 14:17:18 -05:00
exclude_methods: list = []
2025-01-13 20:47:39 -05:00
self.exclude_methods = exclude_methods
2025-01-16 07:14:36 -05:00
async def search(self, artist: str, song: str, plain: bool = True) -> Optional[LyricsResult]:
2025-01-19 07:01:07 -05:00
"""
Aggregate Search
Args:
artist (str): Artist to search
song (str): Song to search
plain (bool): Search for plain lyrics (lrc otherwise)
Returns:
LyricsResult|None: The result, if found - None otherwise.
"""
2025-01-16 07:14:36 -05:00
if not plain:
logging.info("LRCs requested, limiting search to LRCLib")
self.exclude_methods = ["genius", "cache"]
2025-01-14 11:13:39 -05:00
logging.info("Performing aggregate search")
2025-01-13 20:47:39 -05:00
cache_search = cache.Cache()
genius_search = genius.Genius()
lrclib_search = lrclib.LRCLib()
2025-01-14 14:17:18 -05:00
sources: list = [cache_search,
2025-01-14 10:52:53 -05:00
lrclib_search,
genius_search]
2025-01-20 05:47:09 -05:00
if not plain:
sources = [lrclib_search] # Only LRCLib supported for synced lyrics
2025-01-14 14:17:18 -05:00
search_result: Optional[LyricsResult] = None
2025-01-14 10:52:53 -05:00
for source in sources:
if source.label.lower() in self.exclude_methods:
2025-01-19 14:07:19 -05:00
if source.label.lower() == "cache" or not plain:
logging.info("Exclude conditions rejected - source requested to exclude: %s, plain: %s",
source.label, plain)
else:
if plain:
logging.info("Skipping source: %s, excluded.", source.label)
continue
2025-01-20 05:47:09 -05:00
2025-01-16 07:14:36 -05:00
search_result = await source.search(artist=artist, song=song,
plain=plain)
2025-01-14 10:52:53 -05:00
if search_result:
2025-01-15 20:17:49 -05:00
break
2025-01-14 10:57:25 -05:00
logging.info("%s: NOT FOUND!", source.label)
2025-01-16 09:21:50 -05:00
return search_result