Skip to content

Commit 9e67a56

Browse files
committed
Do not attempt to fetch lyrics with empty data
Modified `search_pairs` function in `lyrics.py` to: * Firstly strip each of `artist`, `artist_sort` and `title` fields * Only generate alternatives if both `artist` and `title` are not empty * Ensure that `artist_sort` is not empty and not equal to artist (ignoring case) before appending it to the artists Extended tests to cover the changes.
1 parent 03915fe commit 9e67a56

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

beetsplug/lyrics.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ def generate_alternatives(string, patterns):
163163
alternatives.append(match.group(1))
164164
return alternatives
165165

166-
title, artist, artist_sort = item.title, item.artist, item.artist_sort
166+
title, artist, artist_sort = (
167+
item.title.strip(),
168+
item.artist.strip(),
169+
item.artist_sort.strip(),
170+
)
171+
if not title or not artist:
172+
return ()
167173

168174
patterns = [
169175
# Remove any featuring artists from the artists name
@@ -172,7 +178,7 @@ def generate_alternatives(string, patterns):
172178
artists = generate_alternatives(artist, patterns)
173179
# Use the artist_sort as fallback only if it differs from artist to avoid
174180
# repeated remote requests with the same search terms
175-
if artist != artist_sort:
181+
if artist_sort and artist.lower() != artist_sort.lower():
176182
artists.append(artist_sort)
177183

178184
patterns = [

docs/changelog.rst

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ Bug fixes:
4848
configuration for each test case. This fixes the issue where some tests
4949
failed because they read developer's local lyrics configuration.
5050
:bug:`5133`
51+
* :doc:`plugins/lyrics`: Do not attempt to search for lyrics if either the
52+
artist or title is missing and ignore ``artist_sort`` value if it is empty.
53+
:bug:`2635`
5154

5255
For packagers:
5356

test/plugins/test_lyrics.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,36 @@ def xfail_on_ci(msg: str) -> pytest.MarkDecorator:
4141

4242

4343
class TestLyricsUtils:
44-
unexpected_empty_artist = pytest.mark.xfail(
45-
reason="Empty artist '' should not be present"
44+
@pytest.mark.parametrize(
45+
"artist, title",
46+
[
47+
("Artist", ""),
48+
("", "Title"),
49+
(" ", ""),
50+
("", " "),
51+
("", ""),
52+
],
4653
)
54+
def test_search_empty(self, artist, title):
55+
actual_pairs = lyrics.search_pairs(Item(artist=artist, title=title))
56+
57+
assert not list(actual_pairs)
4758

4859
@pytest.mark.parametrize(
4960
"artist, artist_sort, expected_extra_artists",
5061
[
51-
_p("Alice ft. Bob", "", ["Alice"], marks=unexpected_empty_artist),
52-
_p("Alice feat Bob", "", ["Alice"], marks=unexpected_empty_artist),
53-
_p("Alice feat. Bob", "", ["Alice"], marks=unexpected_empty_artist),
54-
_p("Alice feats Bob", "", [], marks=unexpected_empty_artist),
55-
_p("Alice featuring Bob", "", ["Alice"], marks=unexpected_empty_artist),
56-
_p("Alice & Bob", "", ["Alice"], marks=unexpected_empty_artist),
57-
_p("Alice and Bob", "", ["Alice"], marks=unexpected_empty_artist),
58-
_p("Alice", "", [], marks=unexpected_empty_artist),
62+
("Alice ft. Bob", "", ["Alice"]),
63+
("Alice feat Bob", "", ["Alice"]),
64+
("Alice feat. Bob", "", ["Alice"]),
65+
("Alice feats Bob", "", []),
66+
("Alice featuring Bob", "", ["Alice"]),
67+
("Alice & Bob", "", ["Alice"]),
68+
("Alice and Bob", "", ["Alice"]),
69+
("Alice", "", []),
70+
("Alice", "Alice", []),
71+
("Alice", "alice", []),
72+
("Alice", "alice ", []),
73+
("Alice", "Alice A", ["Alice A"]),
5974
("CHVRCHΞS", "CHVRCHES", ["CHVRCHES"]),
6075
("横山克", "Masaru Yokoyama", ["Masaru Yokoyama"]),
6176
],

0 commit comments

Comments
 (0)