Skip to content

Commit 626bf91

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 63977de commit 626bf91

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
@@ -33,21 +33,36 @@
3333

3434

3535
class TestLyricsUtils:
36-
unexpected_empty_artist = pytest.mark.xfail(
37-
reason="Empty artist '' should not be present"
36+
@pytest.mark.parametrize(
37+
"artist, title",
38+
[
39+
("Artist", ""),
40+
("", "Title"),
41+
(" ", ""),
42+
("", " "),
43+
("", ""),
44+
],
3845
)
46+
def test_search_empty(self, artist, title):
47+
actual_pairs = lyrics.search_pairs(Item(artist=artist, title=title))
48+
49+
assert not list(actual_pairs)
3950

4051
@pytest.mark.parametrize(
4152
"artist, artist_sort, expected_extra_artists",
4253
[
43-
_p("Alice ft. Bob", "", ["Alice"], marks=unexpected_empty_artist),
44-
_p("Alice feat Bob", "", ["Alice"], marks=unexpected_empty_artist),
45-
_p("Alice feat. Bob", "", ["Alice"], marks=unexpected_empty_artist),
46-
_p("Alice feats Bob", "", [], marks=unexpected_empty_artist),
47-
_p("Alice featuring Bob", "", ["Alice"], marks=unexpected_empty_artist),
48-
_p("Alice & Bob", "", ["Alice"], marks=unexpected_empty_artist),
49-
_p("Alice and Bob", "", ["Alice"], marks=unexpected_empty_artist),
50-
_p("Alice", "", [], marks=unexpected_empty_artist),
54+
("Alice ft. Bob", "", ["Alice"]),
55+
("Alice feat Bob", "", ["Alice"]),
56+
("Alice feat. Bob", "", ["Alice"]),
57+
("Alice feats Bob", "", []),
58+
("Alice featuring Bob", "", ["Alice"]),
59+
("Alice & Bob", "", ["Alice"]),
60+
("Alice and Bob", "", ["Alice"]),
61+
("Alice", "", []),
62+
("Alice", "Alice", []),
63+
("Alice", "alice", []),
64+
("Alice", "alice ", []),
65+
("Alice", "Alice A", ["Alice A"]),
5166
("CHVRCHΞS", "CHVRCHES", ["CHVRCHES"]),
5267
("横山克", "Masaru Yokoyama", ["Masaru Yokoyama"]),
5368
],

0 commit comments

Comments
 (0)