Skip to content

Commit b4d78da

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 f36016d commit b4d78da

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
@@ -152,7 +152,13 @@ def generate_alternatives(string, patterns):
152152
alternatives.append(match.group(1))
153153
return alternatives
154154

155-
title, artist, artist_sort = item.title, item.artist, item.artist_sort
155+
title, artist, artist_sort = (
156+
item.title.strip(),
157+
item.artist.strip(),
158+
item.artist_sort.strip(),
159+
)
160+
if not title or not artist:
161+
return ()
156162

157163
patterns = [
158164
# Remove any featuring artists from the artists name
@@ -161,7 +167,7 @@ def generate_alternatives(string, patterns):
161167
artists = generate_alternatives(artist, patterns)
162168
# Use the artist_sort as fallback only if it differs from artist to avoid
163169
# repeated remote requests with the same search terms
164-
if artist != artist_sort:
170+
if artist_sort and artist.lower() != artist_sort.lower():
165171
artists.append(artist_sort)
166172

167173
patterns = [

docs/changelog.rst

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

5558
For packagers:
5659

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)