Skip to content

Commit a211b4a

Browse files
committed
Try simple MB search in case artist is an alias
1 parent 5e6bc35 commit a211b4a

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

beetsplug/_utils/musicbrainz.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import operator
1414
from dataclasses import dataclass, field
1515
from functools import cached_property, singledispatchmethod, wraps
16-
from itertools import groupby
16+
from itertools import chain, groupby
1717
from typing import TYPE_CHECKING, Any, Literal, ParamSpec, TypedDict, TypeVar
1818

1919
from requests_ratelimiter import LimiterMixin
@@ -185,6 +185,8 @@ def search(
185185
self,
186186
entity: Entity,
187187
filters: dict[str, str],
188+
advanced: bool = True,
189+
*args: str,
188190
**kwargs: Unpack[SearchKwargs],
189191
) -> list[JSONDict]:
190192
"""Search for MusicBrainz entities matching the given filters.
@@ -195,11 +197,23 @@ def search(
195197
- 'value' is empty, in which case the filter is ignored
196198
* Values are lowercased and stripped of whitespace.
197199
"""
198-
query = " AND ".join(
199-
":".join(filter(None, (k, f'"{_v}"')))
200-
for k, v in filters.items()
201-
if (_v := v.lower().strip())
202-
)
200+
if advanced:
201+
query = " AND ".join(
202+
f'{k}:"{_v}"'
203+
for k, v in filters.items()
204+
if (_v := v.lower().strip())
205+
)
206+
else:
207+
params = chain(
208+
(str(arg) for arg in args),
209+
(
210+
f'{k}:"{_v}"'
211+
for k, v in filters.items()
212+
if (_v := v.lower().strip())
213+
),
214+
)
215+
query = " ".join(params)
216+
203217
log.debug("Searching for MusicBrainz {}s with: {!r}", entity, query)
204218
kwargs["query"] = query
205219
return self._get_resource(entity, **kwargs)[f"{entity}s"]

beetsplug/musicbrainz.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -825,15 +825,20 @@ def _search_api(
825825
self,
826826
query_type: Literal["recording", "release"],
827827
filters: dict[str, str],
828+
advanced: bool = True,
829+
*args: str,
828830
) -> list[JSONDict]:
829831
"""Perform MusicBrainz API search and return results.
830832
831833
Execute a search against the MusicBrainz API for recordings or releases
832-
using the provided criteria. Handles API errors by converting them into
833-
MusicBrainzAPIError exceptions with contextual information.
834+
using the provided criteria.
834835
"""
835836
return self.mb_api.search(
836-
query_type, filters, limit=self.config["search_limit"].get()
837+
query_type,
838+
filters,
839+
advanced,
840+
*args,
841+
limit=self.config["search_limit"].get(),
837842
)
838843

839844
def candidates(
@@ -844,7 +849,15 @@ def candidates(
844849
va_likely: bool,
845850
) -> Iterable[AlbumInfo]:
846851
criteria = self.get_album_criteria(items, artist, album, va_likely)
847-
release_ids = (r["id"] for r in self._search_api("release", criteria))
852+
release_ids = [r["id"] for r in self._search_api("release", criteria)]
853+
854+
if len(release_ids) == 0 and "artist" in criteria:
855+
# try a less advanced search if va_likely is False
856+
del criteria["artist"]
857+
release_ids = [
858+
r["id"]
859+
for r in self._search_api("release", criteria, False, artist)
860+
]
848861

849862
for id_ in release_ids:
850863
with suppress(HTTPNotFoundError):

0 commit comments

Comments
 (0)