cleanup
This commit is contained in:
@ -8,15 +8,11 @@ from aiohttp import ClientTimeout, ClientSession
|
||||
from lyric_search import utils
|
||||
from lyric_search.constructors import LyricsResult
|
||||
from . import common, cache, redis_cache
|
||||
from constructors import InvalidLRCLibResponseException
|
||||
|
||||
logger = logging.getLogger()
|
||||
log_level = logging.getLevelName(logger.level)
|
||||
|
||||
class InvalidResponseException(Exception):
|
||||
"""
|
||||
Invalid Response Exception
|
||||
"""
|
||||
|
||||
class LRCLib:
|
||||
"""LRCLib Search Module"""
|
||||
def __init__(self) -> None:
|
||||
@ -62,18 +58,18 @@ class LRCLib:
|
||||
|
||||
text: Optional[str] = await request.text()
|
||||
if not text:
|
||||
raise InvalidResponseException("No search response.")
|
||||
raise InvalidLRCLibResponseException("No search response.")
|
||||
if len(text) < 100:
|
||||
raise InvalidResponseException("Search response text was invalid (len < 100 chars.)")
|
||||
raise InvalidLRCLibResponseException("Search response text was invalid (len < 100 chars.)")
|
||||
|
||||
search_data: Optional[Union[list, dict]] = await request.json()
|
||||
if not isinstance(search_data, list|dict):
|
||||
raise InvalidResponseException("No JSON search data.")
|
||||
raise InvalidLRCLibResponseException("No JSON search data.")
|
||||
|
||||
# logging.info("Search Data:\n%s", search_data)
|
||||
|
||||
if not isinstance(search_data, list):
|
||||
raise InvalidResponseException("Invalid JSON.")
|
||||
raise InvalidLRCLibResponseException("Invalid JSON.")
|
||||
|
||||
if plain:
|
||||
possible_matches = [(x, f"{result.get('artistName')} - {result.get('trackName')}")
|
||||
@ -92,21 +88,21 @@ class LRCLib:
|
||||
best_match_id = best_match[0]
|
||||
|
||||
if not isinstance(search_data[best_match_id]['artistName'], str):
|
||||
raise InvalidResponseException(f"Invalid JSON: Cannot find artistName key.\n{search_data}")
|
||||
raise InvalidLRCLibResponseException(f"Invalid JSON: Cannot find artistName key.\n{search_data}")
|
||||
|
||||
if not isinstance(search_data[best_match_id]['trackName'], str):
|
||||
raise InvalidResponseException(f"Invalid JSON: Cannot find trackName key.\n{search_data}")
|
||||
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']
|
||||
if plain:
|
||||
if not isinstance(search_data[best_match_id]['plainLyrics'], str):
|
||||
raise InvalidResponseException(f"Invalid JSON: Cannot find plainLyrics key.\n{search_data}")
|
||||
raise InvalidLRCLibResponseException(f"Invalid JSON: Cannot find plainLyrics key.\n{search_data}")
|
||||
returned_lyrics: str = search_data[best_match_id]['plainLyrics']
|
||||
returned_lyrics = self.datautils.scrub_lyrics(returned_lyrics)
|
||||
else:
|
||||
if not isinstance(search_data[best_match_id]['syncedLyrics'], str):
|
||||
raise InvalidResponseException(f"Invalid JSON: Cannot find syncedLyrics key.\n{search_data}")
|
||||
raise InvalidLRCLibResponseException(f"Invalid JSON: Cannot find syncedLyrics key.\n{search_data}")
|
||||
returned_lyrics: str = search_data[best_match_id]['syncedLyrics']
|
||||
lrc_obj = self.datautils.create_lrc_object(returned_lyrics)
|
||||
returned_track: str = f"{returned_artist} - {returned_song}"
|
||||
|
Reference in New Issue
Block a user