14
14
15
15
"""Adds pseudo-releases from MusicBrainz as candidates during import."""
16
16
17
+ import itertools
17
18
from typing import Iterable , Sequence
18
19
19
20
from typing_extensions import override
32
33
class MusicBrainzPseudoReleasePlugin (MetadataSourcePlugin ):
33
34
def __init__ (self , * args , ** kwargs ) -> None :
34
35
super ().__init__ (* args , ** kwargs )
35
- self .config .add ({"scripts" : []})
36
+ self .config .add ({"scripts" : [], "include_official_releases" : False })
36
37
self ._scripts = self .config ["scripts" ].as_str_seq ()
37
38
self ._mb = mbplugin .MusicBrainzPlugin ()
38
39
self ._pseudo_release_ids : dict [str , list [str ]] = {}
@@ -132,23 +133,9 @@ def candidates(
132
133
pseudo_release_ids = []
133
134
134
135
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
+ )
152
139
del self ._pseudo_release_ids [official_release_id ]
153
140
del self ._intercepted_candidates [official_release_id ]
154
141
return pseudo_releases
@@ -166,6 +153,50 @@ def candidates(
166
153
self ._mb .candidates (items , artist , album , va_likely )
167
154
)
168
155
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 :
169
200
recursion = False
170
201
for official_candidate in official_candidates :
171
202
if official_candidate .album_id in self ._pseudo_release_ids :
@@ -178,12 +209,7 @@ def candidates(
178
209
items , intercepted .tracks
179
210
)
180
211
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
187
213
188
214
@override
189
215
def album_distance (
0 commit comments