Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Genius: handle lyrics divs with no text #5585

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions beetsplug/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,12 @@ def _scrape_lyrics_from_html(self, html):
for lyrics_div in lyrics_divs:
self.replace_br(lyrics_div)
lyrics += lyrics_div.get_text() + "\n\n"
while lyrics[-1] == "\n":
lyrics = lyrics[:-1]
return lyrics
# We can end up with an empty string here if there was no text in the
# lyrics divs: for classical pieces, for example, there is sometimes
# just a series of images representing pages of the score.
# In that case, safe to say there are no lyrics (or if there are,
# we can't retrieve them).
return lyrics.rstrip() or None

def _try_extracting_lyrics_from_non_data_lyrics_container(self, soup):
"""Extract lyrics from a div without attribute data-lyrics-container
Expand Down
14 changes: 11 additions & 3 deletions test/plugins/test_lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,23 @@ def setUp(self):
self.plugin = lyrics.LyricsPlugin()

def test_no_lyrics_div(self):
"""Ensure we don't crash when the scraping the html for a genius page
doesn't contain <div class="lyrics"></div>
"""Ensure we return None when the html for a genius page
doesn't contain a div with lyrics
"""
# https://github.com/beetbox/beets/issues/3535
# expected return value None
url = "https://genius.com/sample"
mock = MockFetchUrl()
assert genius._scrape_lyrics_from_html(mock(url)) is None

def test_empty_lyrics_div(self):
"""Ensure we return None when the html for a genius page
contains a lyrics div with no text in it
"""
# https://github.com/beetbox/beets/issues/5583
url = "https://genius.com/beethovengraphicalscore"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there's a need to use an actual html text file. I think it would be more clear to simply define a string in this test case, where it's clear that it doesn't contain the expected tags

mock = MockFetchUrl()
assert genius._scrape_lyrics_from_html(mock(url)) is None

def test_good_lyrics(self):
"""Ensure we are able to scrape a page with lyrics"""
url = "https://genius.com/Ttng-chinchilla-lyrics"
Expand Down
1,581 changes: 1,581 additions & 0 deletions test/rsrc/lyrics/geniuscom/beethovengraphicalscore.txt

Large diffs are not rendered by default.

Loading