2025-01-14 10:04:05 -05:00
|
|
|
#!/usr/bin/env python3.12
|
|
|
|
# pylint: disable=bare-except, broad-exception-caught, wrong-import-position
|
|
|
|
|
|
|
|
import sys
|
|
|
|
sys.path.insert(1,'..')
|
|
|
|
import traceback
|
2025-01-14 11:10:13 -05:00
|
|
|
import logging
|
2025-01-14 10:04:05 -05:00
|
|
|
from aiohttp import ClientTimeout, ClientSession
|
|
|
|
from lyric_search_new import utils
|
|
|
|
from lyric_search_new.constructors import LyricsResult
|
|
|
|
from . import common
|
|
|
|
|
2025-01-14 11:10:13 -05:00
|
|
|
logger = logging.getLogger()
|
|
|
|
log_level = logging.getLevelName(logger.level)
|
|
|
|
|
2025-01-14 10:04:05 -05:00
|
|
|
class InvalidResponseException(Exception):
|
|
|
|
"""
|
|
|
|
Invalid Response Exception
|
|
|
|
"""
|
|
|
|
|
|
|
|
class LRCLib:
|
|
|
|
"""LRCLib Search Module"""
|
|
|
|
def __init__(self):
|
|
|
|
self.label = "LRCLib"
|
|
|
|
self.lrclib_url = "https://lrclib.net/api/get"
|
|
|
|
self.headers = common.SCRAPE_HEADERS
|
|
|
|
self.timeout = ClientTimeout(connect=2, sock_read=4)
|
|
|
|
self.datautils = utils.DataUtils()
|
|
|
|
self.matcher = utils.TrackMatcher()
|
|
|
|
|
|
|
|
async def search(self, artist: str, song: str):
|
|
|
|
"""
|
|
|
|
@artist: the artist to search
|
|
|
|
@song: the song to search
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
artist = artist.strip().lower()
|
|
|
|
song = song.strip().lower()
|
|
|
|
returned_lyrics = ''
|
|
|
|
async with ClientSession() as client:
|
|
|
|
async with client.get(self.lrclib_url,
|
|
|
|
params = {
|
|
|
|
'artist_name': artist,
|
|
|
|
'track_name': song,
|
|
|
|
},
|
|
|
|
timeout=self.timeout,
|
|
|
|
headers=self.headers) as request:
|
|
|
|
request.raise_for_status()
|
|
|
|
text = await request.text()
|
|
|
|
|
|
|
|
if len(text) < 100:
|
|
|
|
raise InvalidResponseException("Search response text was invalid (len < 100 chars.)")
|
|
|
|
|
|
|
|
search_data = await request.json()
|
|
|
|
|
|
|
|
if not isinstance(search_data, dict):
|
|
|
|
raise InvalidResponseException("Invalid JSON.")
|
|
|
|
|
|
|
|
if not isinstance(search_data['artistName'], str):
|
|
|
|
raise InvalidResponseException(f"Invalid JSON: Cannot find artistName key.\n{search_data}")
|
|
|
|
|
|
|
|
if not isinstance(search_data['trackName'], str):
|
|
|
|
raise InvalidResponseException(f"Invalid JSON: Cannot find trackName key.\n{search_data}")
|
|
|
|
|
|
|
|
returned_artist = search_data['artistName']
|
|
|
|
returned_song = search_data['trackName']
|
|
|
|
returned_lyrics = search_data['plainLyrics']
|
|
|
|
returned_lyrics = self.datautils.scrub_lyrics(returned_lyrics)
|
|
|
|
input_track = f"{artist} - {song}"
|
|
|
|
returned_track = f"{artist} - {song}"
|
2025-01-14 10:57:25 -05:00
|
|
|
(_matched, confidence) = self.matcher.find_best_match(input_track=input_track,
|
2025-01-14 10:04:05 -05:00
|
|
|
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-14 10:04:05 -05:00
|
|
|
return LyricsResult(artist=returned_artist,
|
|
|
|
song=returned_song,
|
|
|
|
src=self.label,
|
|
|
|
lyrics=returned_lyrics,
|
|
|
|
confidence=confidence)
|
|
|
|
except:
|
2025-01-14 11:10:13 -05:00
|
|
|
if log_level == "DEBUG":
|
|
|
|
traceback.print_exc()
|
2025-01-14 10:04:05 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
|