importer: provides search_ids into lookup_candidates explicitly

This commit is contained in:
Šarūnas Nejus 2025-07-13 20:35:46 +01:00
parent 5677f9beee
commit 21459c70ee
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
3 changed files with 19 additions and 23 deletions

View file

@ -143,9 +143,7 @@ def lookup_candidates(session: ImportSession, task: ImportTask):
# Restrict the initial lookup to IDs specified by the user via the -m # Restrict the initial lookup to IDs specified by the user via the -m
# option. Currently all the IDs are passed onto the tasks directly. # option. Currently all the IDs are passed onto the tasks directly.
task.search_ids = session.config["search_ids"].as_str_seq() task.lookup_candidates(session.config["search_ids"].as_str_seq())
task.lookup_candidates()
@pipeline.stage @pipeline.stage

View file

@ -32,6 +32,8 @@ from beets.dbcore.query import PathQuery
from .state import ImportState from .state import ImportState
if TYPE_CHECKING: if TYPE_CHECKING:
from beets.autotag.match import Recommendation
from .session import ImportSession from .session import ImportSession
# Global logger. # Global logger.
@ -159,6 +161,7 @@ class ImportTask(BaseImportTask):
cur_album: str | None = None cur_album: str | None = None
cur_artist: str | None = None cur_artist: str | None = None
candidates: Sequence[autotag.AlbumMatch | autotag.TrackMatch] = [] candidates: Sequence[autotag.AlbumMatch | autotag.TrackMatch] = []
rec: Recommendation | None = None
def __init__( def __init__(
self, self,
@ -167,11 +170,9 @@ class ImportTask(BaseImportTask):
items: Iterable[library.Item] | None, items: Iterable[library.Item] | None,
): ):
super().__init__(toppath, paths, items) super().__init__(toppath, paths, items)
self.rec = None
self.should_remove_duplicates = False self.should_remove_duplicates = False
self.should_merge_duplicates = False self.should_merge_duplicates = False
self.is_album = True self.is_album = True
self.search_ids = [] # user-supplied candidate IDs.
def set_choice( def set_choice(
self, choice: Action | autotag.AlbumMatch | autotag.TrackMatch self, choice: Action | autotag.AlbumMatch | autotag.TrackMatch
@ -356,18 +357,15 @@ class ImportTask(BaseImportTask):
tasks = [t for inner in tasks for t in inner] tasks = [t for inner in tasks for t in inner]
return tasks return tasks
def lookup_candidates(self): def lookup_candidates(self, search_ids: list[str]) -> None:
"""Retrieve and store candidates for this album. User-specified """Retrieve and store candidates for this album.
candidate IDs are stored in self.search_ids: if present, the
initial lookup is restricted to only those IDs. If User-specified ``search_ids`` list is not empty, the lookup is
restricted to only those IDs.
""" """
artist, album, prop = autotag.tag_album( self.cur_artist, self.cur_album, (self.candidates, self.rec) = (
self.items, search_ids=self.search_ids autotag.tag_album(self.items, search_ids=search_ids)
) )
self.cur_artist = artist
self.cur_album = album
self.candidates = prop.candidates
self.rec = prop.recommendation
def find_duplicates(self, lib: library.Library): def find_duplicates(self, lib: library.Library):
"""Return a list of albums from `lib` with the same artist and """Return a list of albums from `lib` with the same artist and
@ -695,10 +693,10 @@ class SingletonImportTask(ImportTask):
for item in self.imported_items(): for item in self.imported_items():
plugins.send("item_imported", lib=lib, item=item) plugins.send("item_imported", lib=lib, item=item)
def lookup_candidates(self): def lookup_candidates(self, search_ids: list[str]) -> None:
prop = autotag.tag_item(self.item, search_ids=self.search_ids) self.candidates, self.rec = autotag.tag_item(
self.candidates = prop.candidates self.item, search_ids=search_ids
self.rec = prop.recommendation )
def find_duplicates(self, lib): def find_duplicates(self, lib):
"""Return a list of items from `lib` that have the same artist """Return a list of items from `lib` that have the same artist

View file

@ -1627,9 +1627,9 @@ class ImportIdTest(ImportTestCase):
task = importer.ImportTask( task = importer.ImportTask(
paths=self.import_dir, toppath="top path", items=[_common.item()] paths=self.import_dir, toppath="top path", items=[_common.item()]
) )
task.search_ids = [self.ID_RELEASE_0, self.ID_RELEASE_1]
task.lookup_candidates() task.lookup_candidates([self.ID_RELEASE_0, self.ID_RELEASE_1])
assert {"VALID_RELEASE_0", "VALID_RELEASE_1"} == { assert {"VALID_RELEASE_0", "VALID_RELEASE_1"} == {
c.info.album for c in task.candidates c.info.album for c in task.candidates
} }
@ -1639,9 +1639,9 @@ class ImportIdTest(ImportTestCase):
task = importer.SingletonImportTask( task = importer.SingletonImportTask(
toppath="top path", item=_common.item() toppath="top path", item=_common.item()
) )
task.search_ids = [self.ID_RECORDING_0, self.ID_RECORDING_1]
task.lookup_candidates() task.lookup_candidates([self.ID_RECORDING_0, self.ID_RECORDING_1])
assert {"VALID_RECORDING_0", "VALID_RECORDING_1"} == { assert {"VALID_RECORDING_0", "VALID_RECORDING_1"} == {
c.info.title for c in task.candidates c.info.title for c in task.candidates
} }