cleanup
This commit is contained in:
@ -9,16 +9,12 @@ from bs4 import BeautifulSoup, ResultSet # type: ignore
|
||||
import html as htm
|
||||
from . import private, common, cache, redis_cache
|
||||
from lyric_search import utils
|
||||
from lyric_search.constructors import LyricsResult
|
||||
from lyric_search.constructors import (
|
||||
LyricsResult, InvalidGeniusResponseException)
|
||||
|
||||
logger = logging.getLogger()
|
||||
log_level = logging.getLevelName(logger.level)
|
||||
|
||||
class InvalidResponseException(Exception):
|
||||
"""
|
||||
InvalidResponseException
|
||||
"""
|
||||
|
||||
class Genius:
|
||||
"""
|
||||
Genius Search Module
|
||||
@ -60,23 +56,23 @@ class Genius:
|
||||
text: Optional[str] = await request.text()
|
||||
|
||||
if not text:
|
||||
raise InvalidResponseException("No search response.")
|
||||
raise InvalidGeniusResponseException("No search response.")
|
||||
|
||||
if len(text) < 100:
|
||||
raise InvalidResponseException("Search response text was invalid (len < 100 chars.)")
|
||||
raise InvalidGeniusResponseException("Search response text was invalid (len < 100 chars.)")
|
||||
search_data = await request.json()
|
||||
|
||||
if not isinstance(search_data, dict):
|
||||
raise InvalidResponseException("Invalid JSON.")
|
||||
raise InvalidGeniusResponseException("Invalid JSON.")
|
||||
|
||||
if not isinstance(search_data['response'], dict):
|
||||
raise InvalidResponseException(f"Invalid JSON: Cannot find response key.\n{search_data}")
|
||||
raise InvalidGeniusResponseException(f"Invalid JSON: Cannot find response key.\n{search_data}")
|
||||
|
||||
if not isinstance(search_data['response']['sections'], list):
|
||||
raise InvalidResponseException(f"Invalid JSON: Cannot find response->sections key.\n{search_data}")
|
||||
raise InvalidGeniusResponseException(f"Invalid JSON: Cannot find response->sections key.\n{search_data}")
|
||||
|
||||
if not isinstance(search_data['response']['sections'][0]['hits'], list):
|
||||
raise InvalidResponseException("Invalid JSON: Cannot find response->sections[0]->hits key.")
|
||||
raise InvalidGeniusResponseException("Invalid JSON: Cannot find response->sections[0]->hits key.")
|
||||
|
||||
possible_matches: list = search_data['response']['sections'][0]['hits']
|
||||
to_scrape: list[tuple] = [
|
||||
@ -98,10 +94,10 @@ class Genius:
|
||||
scrape_text: Optional[str] = await scrape_request.text()
|
||||
|
||||
if not scrape_text:
|
||||
raise InvalidResponseException("No scrape response.")
|
||||
raise InvalidGeniusResponseException("No scrape response.")
|
||||
|
||||
if len(scrape_text) < 100:
|
||||
raise InvalidResponseException("Scrape response was invalid (len < 100 chars.)")
|
||||
raise InvalidGeniusResponseException("Scrape response was invalid (len < 100 chars.)")
|
||||
|
||||
|
||||
html = BeautifulSoup(htm.unescape(scrape_text).replace('<br/>', '\n'), "html.parser")
|
||||
|
Reference in New Issue
Block a user