Skip to content

Commit

Permalink
lyrics: Add some missing None-checks to the Tekstowo source
Browse files Browse the repository at this point in the history
The previous code had the potential to crash if (when?) Tekstowo changes
their website structure sufficiently.

The new code is rather ugly due to the explicit checks after each and
every function call. Unfortunately, the alternative would be to catch a
bunch of very generic Exceptions (AttributeError, ...), since there's no
such thing as a `BeautifulSoupNotFoundError`.
  • Loading branch information
wisp3rwind committed Jul 4, 2021
1 parent 09be82b commit 0c28575
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions beetsplug/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,20 +461,27 @@ def parse_search_results(self, html):
if not soup:
return None

song_rows = soup.find("div", class_="content"). \
find("div", class_="card"). \
find_all("div", class_="box-przeboje")
content_div = soup.find("div", class_="content")
if not content_div:
return None

card_div = content_div.find("div", class_="card")
if not card_div:
return None

song_rows = card_div.find_all("div", class_="box-przeboje")
if not song_rows:
return None

song_row = song_rows[0]

if not song_row:
return None

href = song_row.find('a').get('href')
return self.BASE_URL + href
link = song_row.find('a')
if not link:
return None

return self.BASE_URL + link.get('href')

def extract_lyrics(self, html):
html = _scrape_strip_cruft(html)
Expand All @@ -484,10 +491,11 @@ def extract_lyrics(self, html):
if not soup:
return None

c = soup.find("div", class_="song-text")
if c:
return c.get_text()
return None
lyrics_div = soup.find("div", class_="song-text")
if not lyrics_div:
return None

return lyrics_div.get_text()


def remove_credits(text):
Expand Down

0 comments on commit 0c28575

Please sign in to comment.