163 lines
6.9 KiB
Python
Raw Normal View History

import sys
2025-01-15 20:17:49 -05:00
import time
sys.path.insert(1, "..")
import traceback
2025-01-14 11:10:13 -05:00
import logging
2025-02-15 21:18:20 -05:00
from typing import Optional, Union
from aiohttp import ClientTimeout, ClientSession
from lyric_search import utils
from lyric_search.constructors import LyricsResult
2025-01-22 06:38:40 -05:00
from . import common, cache, redis_cache
2025-02-18 15:17:35 -05:00
from lyric_search.constructors import InvalidLRCLibResponseException
2025-01-14 11:10:13 -05:00
logger = logging.getLogger()
log_level = logging.getLevelName(logger.level)
class LRCLib:
"""LRCLib Search Module"""
2025-01-19 07:09:05 -05:00
def __init__(self) -> None:
2025-01-14 14:17:18 -05:00
self.label: str = "LRCLib"
2025-01-14 18:37:49 -05:00
self.lrclib_url: str = "https://lrclib.net/api/search"
2025-01-14 14:17:18 -05:00
self.headers: dict = common.SCRAPE_HEADERS
self.timeout = ClientTimeout(connect=2, sock_read=4)
self.datautils = utils.DataUtils()
self.matcher = utils.TrackMatcher()
2025-01-15 20:17:49 -05:00
self.cache = cache.Cache()
2025-01-22 06:38:40 -05:00
self.redis_cache = redis_cache.RedisCache()
async def search(
self, artist: str, song: str, plain: Optional[bool] = True
) -> Optional[LyricsResult]:
"""
2025-01-19 07:01:07 -05:00
LRCLib Search
Args:
artist (str): the artist to search
song (str): the song to search
Returns:
2025-02-15 21:09:33 -05:00
Optional[LyricsResult]: The result, if found - None otherwise.
"""
try:
2025-01-14 14:17:18 -05:00
artist: str = artist.strip().lower()
song: str = song.strip().lower()
2025-01-15 20:17:49 -05:00
time_start: float = time.time()
2025-01-17 07:48:29 -05:00
lrc_obj: Optional[list[dict]] = None
logging.info("Searching %s - %s on %s", artist, song, self.label)
input_track: str = f"{artist} - {song}"
returned_lyrics: str = ""
async with ClientSession() as client:
async with await client.get(
self.lrclib_url,
params={
"artist_name": artist,
"track_name": song,
},
timeout=self.timeout,
headers=self.headers,
) as request:
request.raise_for_status()
2025-02-15 21:09:33 -05:00
text: Optional[str] = await request.text()
if not text:
raise InvalidLRCLibResponseException("No search response.")
if len(text) < 100:
raise InvalidLRCLibResponseException(
"Search response text was invalid (len < 100 chars.)"
)
2025-02-15 21:18:20 -05:00
search_data: Optional[Union[list, dict]] = await request.json()
if not isinstance(search_data, list | dict):
2025-02-18 14:56:24 -05:00
raise InvalidLRCLibResponseException("No JSON search data.")
2025-01-14 18:37:49 -05:00
# logging.info("Search Data:\n%s", search_data)
2025-01-14 18:37:49 -05:00
if not isinstance(search_data, list):
2025-02-18 14:56:24 -05:00
raise InvalidLRCLibResponseException("Invalid JSON.")
2025-01-17 07:48:29 -05:00
if plain:
possible_matches = [
(
x,
f"{result.get('artistName')} - {result.get('trackName')}",
)
for x, result in enumerate(search_data)
]
2025-01-17 07:48:29 -05:00
else:
logging.info(
"Limiting possible matches to only those with non-null syncedLyrics"
)
possible_matches = [
(
x,
f"{result.get('artistName')} - {result.get('trackName')}",
)
for x, result in enumerate(search_data)
if isinstance(result["syncedLyrics"], str)
]
2025-01-17 07:48:29 -05:00
best_match = self.matcher.find_best_match(
input_track, possible_matches
)[0]
2025-01-14 18:37:49 -05:00
if not best_match:
return
best_match_id = best_match[0]
if not isinstance(search_data[best_match_id]["artistName"], str):
raise InvalidLRCLibResponseException(
f"Invalid JSON: Cannot find artistName key.\n{search_data}"
)
if not isinstance(search_data[best_match_id]["trackName"], str):
raise InvalidLRCLibResponseException(
f"Invalid JSON: Cannot find trackName key.\n{search_data}"
)
returned_artist: str = search_data[best_match_id]["artistName"]
returned_song: str = search_data[best_match_id]["trackName"]
2025-01-16 07:14:36 -05:00
if plain:
if not isinstance(
search_data[best_match_id]["plainLyrics"], str
):
raise InvalidLRCLibResponseException(
f"Invalid JSON: Cannot find plainLyrics key.\n{search_data}"
)
returned_lyrics: str = search_data[best_match_id]["plainLyrics"]
2025-01-16 07:14:36 -05:00
returned_lyrics = self.datautils.scrub_lyrics(returned_lyrics)
else:
if not isinstance(
search_data[best_match_id]["syncedLyrics"], str
):
raise InvalidLRCLibResponseException(
f"Invalid JSON: Cannot find syncedLyrics key.\n{search_data}"
)
returned_lyrics: str = search_data[best_match_id][
"syncedLyrics"
]
2025-01-17 07:48:29 -05:00
lrc_obj = self.datautils.create_lrc_object(returned_lyrics)
2025-01-14 18:37:49 -05:00
returned_track: str = f"{returned_artist} - {returned_song}"
(_matched, confidence) = self.matcher.find_best_match(
input_track=input_track, candidate_tracks=[(0, returned_track)]
)
if not confidence:
return # No suitable match found
2025-01-14 11:10:13 -05:00
logging.info("Result found on %s", self.label)
2025-01-15 20:17:49 -05:00
time_end: float = time.time()
time_diff: float = time_end - time_start
matched = LyricsResult(
artist=returned_artist,
song=returned_song,
src=self.label,
lyrics=returned_lyrics if plain else lrc_obj,
confidence=confidence,
time=time_diff,
)
2025-01-22 06:38:40 -05:00
await self.redis_cache.increment_found_count(self.label)
2025-01-15 20:17:49 -05:00
await self.cache.store(matched)
return matched
except:
traceback.print_exc()