Take into account va_likely param and remove redundant checks

- Instead of checking for empty `artist` query, use `va_likely`
  parameter to determine whether we should query for Various Artists or
  not.
- `album` / `title` is always a truthy string - no need to handle empty
  criteria case
- `tracks` list always has at least one track - no need to check for
  `len(items)`
This commit is contained in:
Šarūnas Nejus 2025-05-18 12:15:45 +01:00
parent 6487893315
commit 0102f3ce7d
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
2 changed files with 11 additions and 28 deletions

View file

@ -768,15 +768,14 @@ class MusicBrainzPlugin(BeetsPlugin):
def get_album_criteria(
self, items: list[Item], artist: str, album: str, va_likely: bool
) -> dict[str, str]:
# Build search criteria.
criteria = {"release": album.lower().strip()}
if artist is not None:
criteria["artist"] = artist.lower().strip()
else:
# Various Artists search.
criteria["arid"] = VARIOUS_ARTISTS_ID
if track_count := len(items):
criteria["tracks"] = str(track_count)
criteria = {
"release": album.lower().strip(),
"tracks": str(len(items)),
} | (
{"arid": VARIOUS_ARTISTS_ID}
if va_likely
else {"artist": artist.lower().strip()}
)
for tag, mb_field in self.extra_mb_field_by_tag.items():
most_common, _ = util.plurality(i.get(tag) for i in items)
@ -804,9 +803,6 @@ class MusicBrainzPlugin(BeetsPlugin):
optionally, a number of tracks on the album and any other extra tags.
"""
criteria = self.get_album_criteria(items, artist, album, va_likely)
# Abort if we have no search terms.
if not any(criteria.values()):
return
try:
self._log.debug(
@ -837,9 +833,6 @@ class MusicBrainzPlugin(BeetsPlugin):
"recording": title.lower().strip(),
}
if not any(criteria.values()):
return
try:
res = musicbrainzngs.search_recordings(
limit=self.config["searchlimit"].get(int), **criteria

View file

@ -823,23 +823,13 @@ class MBLibraryTest(MusicBrainzTestCase):
ai = list(self.mb.candidates([], "hello", "there", False))[0]
sp.assert_called_with(artist="hello", release="there", limit=5)
sp.assert_called_with(
artist="hello", release="there", tracks="0", limit=5
)
gp.assert_called_with(mbid, mock.ANY)
assert ai.tracks[0].title == "foo"
assert ai.album == "hi"
def test_match_track_empty(self):
with mock.patch("musicbrainzngs.search_recordings") as p:
til = list(self.mb.item_candidates(None, " ", " "))
assert not p.called
assert til == []
def test_candidates_empty(self):
with mock.patch("musicbrainzngs.search_releases") as p:
ail = list(self.mb.candidates([], " ", " ", False))
assert not p.called
assert ail == []
def test_follow_pseudo_releases(self):
side_effect = [
{