mirror of
https://github.com/beetbox/beets.git
synced 2026-01-04 23:12:51 +01:00
musicbrainz: search directly
This commit is contained in:
parent
2ef1722976
commit
51810bfb45
2 changed files with 26 additions and 22 deletions
|
|
@ -771,17 +771,20 @@ class MusicBrainzPlugin(MetadataSourcePlugin):
|
|||
def get_album_criteria(
|
||||
self, items: Sequence[Item], artist: str, album: str, va_likely: bool
|
||||
) -> dict[str, str]:
|
||||
criteria = {
|
||||
"release": album,
|
||||
"alias": album,
|
||||
"tracks": str(len(items)),
|
||||
} | ({"arid": VARIOUS_ARTISTS_ID} if va_likely else {"artist": artist})
|
||||
criteria = {"release": album} | (
|
||||
{"arid": VARIOUS_ARTISTS_ID} if va_likely else {"artist": artist}
|
||||
)
|
||||
|
||||
for tag, mb_field in self.extra_mb_field_by_tag.items():
|
||||
most_common, _ = util.plurality(i.get(tag) for i in items)
|
||||
value = str(most_common)
|
||||
if tag == "catalognum":
|
||||
value = value.replace(" ", "")
|
||||
if tag == "tracks":
|
||||
value = str(len(items))
|
||||
elif tag == "alias":
|
||||
value = album
|
||||
else:
|
||||
most_common, _ = util.plurality(i.get(tag) for i in items)
|
||||
value = str(most_common)
|
||||
if tag == "catalognum":
|
||||
value = value.replace(" ", "")
|
||||
|
||||
criteria[mb_field] = value
|
||||
|
||||
|
|
@ -798,20 +801,23 @@ class MusicBrainzPlugin(MetadataSourcePlugin):
|
|||
using the provided criteria. Handles API errors by converting them into
|
||||
MusicBrainzAPIError exceptions with contextual information.
|
||||
"""
|
||||
filters = {
|
||||
k: _v for k, v in filters.items() if (_v := v.lower().strip())
|
||||
}
|
||||
query = " AND ".join(
|
||||
f'{k}:"{_v}"'
|
||||
for k, v in filters.items()
|
||||
if (_v := v.lower().strip())
|
||||
)
|
||||
self._log.debug(
|
||||
"Searching for MusicBrainz {}s with: {!r}", query_type, filters
|
||||
"Searching for MusicBrainz {}s with: {!r}", query_type, query
|
||||
)
|
||||
try:
|
||||
method = getattr(musicbrainzngs, f"search_{query_type}s")
|
||||
res = method(limit=self.config["search_limit"].get(), **filters)
|
||||
res = self.api._get(
|
||||
query_type, query=query, limit=self.config["search_limit"].get()
|
||||
)
|
||||
except musicbrainzngs.MusicBrainzError as exc:
|
||||
raise MusicBrainzAPIError(
|
||||
exc, f"{query_type} search", filters, traceback.format_exc()
|
||||
)
|
||||
return res[f"{query_type}-list"]
|
||||
return res[f"{query_type}s"]
|
||||
|
||||
def candidates(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -1026,15 +1026,13 @@ class TestMusicBrainzPlugin(PluginMixin):
|
|||
|
||||
assert mb.get_album_criteria(items, "Artist ", " Album", va_likely) == {
|
||||
"release": " Album",
|
||||
"alias": " Album",
|
||||
"tracks": str(len(items)),
|
||||
**expected_additional_criteria,
|
||||
}
|
||||
|
||||
def test_item_candidates(self, monkeypatch, mb):
|
||||
monkeypatch.setattr(
|
||||
"musicbrainzngs.search_recordings",
|
||||
lambda *_, **__: {"recording-list": [self.RECORDING]},
|
||||
"beetsplug.musicbrainz.MusicBrainzAPI._get",
|
||||
lambda *_, **__: {"recordings": [self.RECORDING]},
|
||||
)
|
||||
|
||||
candidates = list(mb.item_candidates(Item(), "hello", "there"))
|
||||
|
|
@ -1044,8 +1042,8 @@ class TestMusicBrainzPlugin(PluginMixin):
|
|||
|
||||
def test_candidates(self, monkeypatch, mb):
|
||||
monkeypatch.setattr(
|
||||
"musicbrainzngs.search_releases",
|
||||
lambda *_, **__: {"release-list": [{"id": self.mbid}]},
|
||||
"beetsplug.musicbrainz.MusicBrainzAPI._get",
|
||||
lambda *_, **__: {"releases": [{"id": self.mbid}]},
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"beetsplug.musicbrainz.MusicBrainzAPI.get_release",
|
||||
|
|
|
|||
Loading…
Reference in a new issue