Decouple fetching metadata

This commit is contained in:
Ritiek Malhotra
2020-03-22 21:44:04 +05:30
parent dae76a0abb
commit 7413c541d3
12 changed files with 373 additions and 485 deletions

View File

@@ -14,6 +14,11 @@ class Genius(LyricBase):
self.base_url = BASE_URL
def _guess_lyric_url(self):
"""
Returns the possible lyric URL for the track available
on Genius. This may not always be a valid URL, but this
is apparently the best we can do at the moment?
"""
query = "/{} {} lyrics".format(self.artist, self.track)
query = query.replace(" ", "-")
encoded_query = urllib.request.quote(query)
@@ -21,6 +26,10 @@ class Genius(LyricBase):
return lyric_url
def _fetch_page(self, url, timeout=None):
"""
Makes a GET request to the given URL and returns the
HTML content in the case of a valid response.
"""
request = urllib.request.Request(url)
request.add_header("User-Agent", "urllib")
try:
@@ -35,14 +44,23 @@ class Genius(LyricBase):
return response.read()
def _get_lyrics_text(self, html):
"""
Extracts and returns the lyric content from the
provided HTML.
"""
soup = BeautifulSoup(html, "html.parser")
lyrics_paragraph = soup.find("p")
if lyrics_paragraph:
return lyrics_paragraph.get_text()
else:
raise LyricsNotFoundError("The lyrics for this track are yet to be released.")
raise LyricsNotFoundError(
"The lyrics for this track are yet to be released."
)
def get_lyrics(self, linesep="\n", timeout=None):
"""
Returns the lyric string for the given artist and track.
"""
url = self._guess_lyric_url()
html_page = self._fetch_page(url, timeout=timeout)
lyrics = self._get_lyrics_text(html_page)