Skip to content

Commit 497e97f

Browse files
committed
Search for multiple album/track ids
1 parent 2da99e8 commit 497e97f

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

beets/autotag/match.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def match_by_id(album_id: str | None, consensus: bool) -> Iterable[AlbumInfo]:
115115
log.debug("No album ID consensus.")
116116
else:
117117
log.debug("Searching for discovered album ID: {}", album_id)
118-
return metadata_plugins.albums_for_ids(album_id)
118+
return metadata_plugins.albums_for_ids([album_id])
119119

120120
return ()
121121

@@ -203,7 +203,7 @@ def _add_candidate(
203203
return
204204

205205
# Prevent duplicates.
206-
if info.album_id and info.identifier in results:
206+
if info.identifier in results:
207207
log.debug("Duplicate.")
208208
return
209209

@@ -270,10 +270,9 @@ def tag_album(
270270

271271
# Search by explicit ID.
272272
if search_ids:
273-
for search_id in search_ids:
274-
log.debug("Searching for album ID: {}", search_id)
275-
for _info in metadata_plugins.albums_for_ids(search_id):
276-
_add_candidate(items, candidates, _info)
273+
log.debug("Searching for album IDs: {}", search_ids)
274+
for _info in metadata_plugins.albums_for_ids(search_ids):
275+
_add_candidate(items, candidates, _info)
277276

278277
# Use existing metadata or text search.
279278
else:
@@ -345,17 +344,16 @@ def tag_item(
345344
# First, try matching by the external source ID.
346345
trackids = search_ids or [t for t in [item.mb_trackid] if t]
347346
if trackids:
348-
for trackid in trackids:
349-
log.debug("Searching for track ID: {}", trackid)
350-
for info in metadata_plugins.tracks_for_ids(trackid):
351-
dist = track_distance(item, info, incl_artist=True)
352-
candidates[info.identifier] = hooks.TrackMatch(dist, info)
353-
354-
# If this is a good match, then don't keep searching.
355-
rec = _recommendation(_sort_candidates(candidates.values()))
356-
if rec == Recommendation.strong and not config["import"]["timid"]:
357-
log.debug("Track ID match.")
358-
return Proposal(_sort_candidates(candidates.values()), rec)
347+
log.debug("Searching for track IDs: {}", trackids)
348+
for info in metadata_plugins.tracks_for_ids(trackids):
349+
dist = track_distance(item, info, incl_artist=True)
350+
candidates[info.identifier] = hooks.TrackMatch(dist, info)
351+
352+
# If this is a good match, then don't keep searching.
353+
rec = _recommendation(_sort_candidates(candidates.values()))
354+
if rec == Recommendation.strong and not config["import"]["timid"]:
355+
log.debug("Track ID match.")
356+
return Proposal(_sort_candidates(candidates.values()), rec)
359357

360358
# If we're searching by ID, don't proceed.
361359
if search_ids:

beets/metadata_plugins.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ def item_candidates(*args, **kwargs) -> Iterable[TrackInfo]:
4949

5050

5151
@notify_info_yielded("albuminfo_received")
52-
def albums_for_ids(_id: str) -> Iterable[AlbumInfo]:
52+
def albums_for_ids(ids: Sequence[str]) -> Iterable[AlbumInfo]:
5353
"""Return matching albums from all metadata sources for the given ID."""
5454
for plugin in find_metadata_source_plugins():
55-
yield from plugin.albums_for_ids([_id])
55+
yield from plugin.albums_for_ids(ids)
5656

5757

5858
@notify_info_yielded("trackinfo_received")
59-
def tracks_for_ids(_id: str) -> Iterable[TrackInfo]:
59+
def tracks_for_ids(ids: Sequence[str]) -> Iterable[TrackInfo]:
6060
"""Return matching tracks from all metadata sources for the given ID."""
6161
for plugin in find_metadata_source_plugins():
62-
yield from plugin.tracks_for_ids([_id])
62+
yield from plugin.tracks_for_ids(ids)
6363

6464

6565
@cache

test/autotag/test_match.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import ClassVar
2-
31
import pytest
42

53
from beets import metadata_plugins
@@ -108,10 +106,7 @@ def shared_album_id(self):
108106

109107
@pytest.fixture(autouse=True)
110108
def _setup_plugins(self, monkeypatch, shared_album_id, shared_track_id):
111-
class StubPlugin:
112-
data_source: ClassVar[str]
113-
data_source_mismatch_penalty = 0
114-
109+
class StubPlugin(metadata_plugins.MetadataSourcePlugin):
115110
@property
116111
def track(self):
117112
return TrackInfo(
@@ -131,11 +126,11 @@ def album(self):
131126
data_source=self.data_source,
132127
)
133128

134-
def albums_for_ids(self, *_):
135-
yield self.album
129+
def album_for_id(self, album_id):
130+
return self.album if album_id == shared_album_id else None
136131

137-
def tracks_for_ids(self, *_):
138-
yield self.track
132+
def track_for_id(self, track_id):
133+
return self.track if track_id == shared_track_id else None
139134

140135
def candidates(self, *_, **__):
141136
yield self.album

0 commit comments

Comments
 (0)