Skip to content

Commit d0c88c9

Browse files
committed
Optionally emit official candidates
1 parent fcb526e commit d0c88c9

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

beetsplug/mbpseudo.py

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Adds pseudo-releases from MusicBrainz as candidates during import."""
1616

17+
import itertools
1718
from typing import Iterable, Sequence
1819

1920
from typing_extensions import override
@@ -32,7 +33,7 @@
3233
class MusicBrainzPseudoReleasePlugin(MetadataSourcePlugin):
3334
def __init__(self, *args, **kwargs) -> None:
3435
super().__init__(*args, **kwargs)
35-
self.config.add({"scripts": []})
36+
self.config.add({"scripts": [], "include_official_releases": False})
3637
self._scripts = self.config["scripts"].as_str_seq()
3738
self._mb = mbplugin.MusicBrainzPlugin()
3839
self._pseudo_release_ids: dict[str, list[str]] = {}
@@ -132,23 +133,9 @@ def candidates(
132133
pseudo_release_ids = []
133134

134135
if official_release_id is not None:
135-
pseudo_releases: list[AlbumInfo] = []
136-
for pri in pseudo_release_ids:
137-
if match := self._mb.album_for_id(pri):
138-
pseudo_album_info = PseudoAlbumInfo(
139-
pseudo_release=match,
140-
official_release=self._intercepted_candidates[
141-
official_release_id
142-
],
143-
data_source=self.data_source,
144-
)
145-
self._log.debug(
146-
"Using {0} release for distance calculations for album {1}",
147-
pseudo_album_info.determine_best_ref(items),
148-
pri,
149-
)
150-
pseudo_releases.append(pseudo_album_info)
151-
136+
pseudo_releases = self._get_pseudo_releases(
137+
items, official_release_id, pseudo_release_ids
138+
)
152139
del self._pseudo_release_ids[official_release_id]
153140
del self._intercepted_candidates[official_release_id]
154141
return pseudo_releases
@@ -166,6 +153,50 @@ def candidates(
166153
self._mb.candidates(items, artist, album, va_likely)
167154
)
168155

156+
recursion = self._mb_plugin_simulation_matched(
157+
items, official_candidates
158+
)
159+
160+
if not self.config.get().get("include_official_releases"):
161+
official_candidates = []
162+
163+
if recursion:
164+
return itertools.chain(
165+
self.candidates(items, artist, album, va_likely),
166+
iter(official_candidates),
167+
)
168+
else:
169+
return iter(official_candidates)
170+
171+
def _get_pseudo_releases(
172+
self,
173+
items: Sequence[Item],
174+
official_release_id: str,
175+
pseudo_release_ids: list[str],
176+
) -> list[AlbumInfo]:
177+
pseudo_releases: list[AlbumInfo] = []
178+
for pri in pseudo_release_ids:
179+
if match := self._mb.album_for_id(pri):
180+
pseudo_album_info = PseudoAlbumInfo(
181+
pseudo_release=match,
182+
official_release=self._intercepted_candidates[
183+
official_release_id
184+
],
185+
data_source=self.data_source,
186+
)
187+
self._log.debug(
188+
"Using {0} release for distance calculations for album {1}",
189+
pseudo_album_info.determine_best_ref(items),
190+
pri,
191+
)
192+
pseudo_releases.append(pseudo_album_info)
193+
return pseudo_releases
194+
195+
def _mb_plugin_simulation_matched(
196+
self,
197+
items: Sequence[Item],
198+
official_candidates: list[AlbumInfo],
199+
) -> bool:
169200
recursion = False
170201
for official_candidate in official_candidates:
171202
if official_candidate.album_id in self._pseudo_release_ids:
@@ -178,12 +209,7 @@ def candidates(
178209
items, intercepted.tracks
179210
)
180211
recursion = True
181-
182-
# TODO include official candidates?
183-
if recursion:
184-
return self.candidates(items, artist, album, va_likely)
185-
else:
186-
return []
212+
return recursion
187213

188214
@override
189215
def album_distance(

0 commit comments

Comments
 (0)