2025-01-13 20:47:39 -05:00
|
|
|
from typing import Optional
|
2025-01-24 09:10:54 -05:00
|
|
|
from lyric_search.constructors import LyricsResult
|
|
|
|
from lyric_search 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-04-17 07:28:05 -04: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-04-17 07:28:05 -04:00
|
|
|
|
2025-01-13 20:47:39 -05:00
|
|
|
class Aggregate:
|
2025-01-19 07:01:07 -05:00
|
|
|
"""
|
|
|
|
Aggregate all source methods
|
|
|
|
"""
|
2025-04-17 07:28:05 -04: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-04-17 07:28:05 -04:00
|
|
|
|
|
|
|
async def search(
|
|
|
|
self, artist: str, song: str, plain: Optional[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:
|
2025-02-15 21:09:33 -05:00
|
|
|
Optional[LyricsResult]: The result, if found - None otherwise.
|
2025-01-19 07:01:07 -05:00
|
|
|
"""
|
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()
|
2025-01-14 10:04:05 -05:00
|
|
|
lrclib_search = lrclib.LRCLib()
|
2025-01-22 09:00:51 -05:00
|
|
|
sources: list = [
|
|
|
|
cache_search,
|
|
|
|
lrclib_search,
|
|
|
|
genius_search,
|
2025-04-17 07:28:05 -04:00
|
|
|
]
|
2025-01-20 05:47:09 -05:00
|
|
|
if not plain:
|
2025-04-17 07:28:05 -04:00
|
|
|
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-04-08 11:27:56 -04:00
|
|
|
if not plain:
|
2025-04-17 07:28:05 -04:00
|
|
|
logging.info(
|
|
|
|
"Exclude conditions rejected - source requested to exclude: %s, plain: %s",
|
|
|
|
source.label,
|
|
|
|
plain,
|
|
|
|
)
|
2025-01-19 14:07:19 -05:00
|
|
|
else:
|
|
|
|
if plain:
|
|
|
|
logging.info("Skipping source: %s, excluded.", source.label)
|
|
|
|
continue
|
2025-04-17 07:28:05 -04: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:
|
2025-04-17 07:28:05 -04:00
|
|
|
logging.info("%s - %s: all sources exhausted, not found.", artist, song)
|
|
|
|
if plain: # do not record LRC fails
|
|
|
|
try:
|
2025-01-24 06:31:12 -05:00
|
|
|
await self.redis_cache.increment_found_count("failed")
|
2025-04-17 07:28:05 -04:00
|
|
|
self.notifier.send(
|
|
|
|
"WARNING",
|
|
|
|
f"Could not find {artist} - {song} via queried sources.",
|
|
|
|
)
|
2025-01-24 06:31:12 -05:00
|
|
|
except Exception as e:
|
|
|
|
traceback.print_exc()
|
2025-04-17 07:28:05 -04:00
|
|
|
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)}",
|
|
|
|
)
|
|
|
|
return search_result
|