Files
api/lyric_search_new/sources/aggregate.py

42 lines
1.4 KiB
Python

#!/usr/bin/env python3.12
# pylint: disable=wrong-import-order
from typing import Optional
from lyric_search_new.constructors import LyricsResult
import sys
sys.path.insert(1,'..')
sys.path.insert(1,'..')
from . import cache
from . import genius
from . import lrclib
class Aggregate:
"""Aggregate all source methods"""
def __init__(self, exclude_methods=None):
if not exclude_methods:
exclude_methods = []
self.exclude_methods = exclude_methods
async def search(self, artist: str, song: str) -> Optional[LyricsResult]:
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)
return search