80 lines
3.1 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
2025-01-23 13:02:03 -05:00
from lyric_search_new import notifier
2025-01-13 20:47:39 -05:00
import sys
2025-01-14 10:52:53 -05:00
import logging
2025-01-23 13:02:03 -05:00
import traceback
2025-01-13 20:47:39 -05:00
sys.path.insert(1,'..')
2025-01-23 13:02:03 -05:00
from . import cache, redis_cache, genius, 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-23 13:02:03 -05:00
self.redis_cache = redis_cache.RedisCache()
self.notifier = notifier.DiscordNotifier()
2025-01-13 20:47:39 -05:00
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-22 09:00:51 -05:00
sources: list = [
cache_search,
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-23 13:02:03 -05:00
if not search_result:
logging.info("%s - %s: all sources exhausted, not found.",
artist, song)
if plain: # do not record LRC fails
try:
await self.redis_cache.increment_found_count("failed")
except Exception as e:
traceback.print_exc()
logging.info("Could not increment redis failed counter: %s",
str(e))
self.notifier.send(f"ERROR @ {__file__.rsplit("/", maxsplit=1)[-1]}",
f"Could not increment redis failed counter: {str(e)}")
2025-01-16 09:21:50 -05:00
return search_result