61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
#!/usr/bin/env python3.12
|
|
# pylint: disable=wrong-import-order, wrong-import-position
|
|
|
|
from typing import Optional
|
|
from lyric_search_new.constructors import LyricsResult
|
|
import sys
|
|
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
|
|
"""
|
|
|
|
def __init__(self, exclude_methods=None) -> None:
|
|
if not exclude_methods:
|
|
exclude_methods: list = []
|
|
self.exclude_methods = exclude_methods
|
|
|
|
async def search(self, artist: str, song: str, plain: bool = True) -> Optional[LyricsResult]:
|
|
"""
|
|
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.
|
|
"""
|
|
if not plain:
|
|
logging.info("LRCs requested, limiting search to LRCLib")
|
|
self.exclude_methods = ["genius", "cache"]
|
|
logging.info("Performing aggregate search")
|
|
cache_search = cache.Cache()
|
|
genius_search = genius.Genius()
|
|
lrclib_search = lrclib.LRCLib()
|
|
sources: list = [cache_search,
|
|
lrclib_search,
|
|
genius_search]
|
|
search_result: Optional[LyricsResult] = None
|
|
for source in sources:
|
|
if source.label.lower() in self.exclude_methods:
|
|
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
|
|
search_result = await source.search(artist=artist, song=song,
|
|
plain=plain)
|
|
if search_result:
|
|
break
|
|
logging.info("%s: NOT FOUND!", source.label)
|
|
return search_result |