Skip to content

Commit

Permalink
Allow to configure which fields are used to find duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
jcassette committed Jan 22, 2022
1 parent 404229b commit 3fdfaaa
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion beets/autotag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
TrackMatch,
Distance,
)
from .match import tag_item, tag_album, Proposal # noqa
from .match import tag_item, tag_album, current_metadata, Proposal # noqa
from .match import Recommendation # noqa

# Global logger.
Expand Down
1 change: 1 addition & 0 deletions beets/config_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import:
group_albums: no
pretend: false
search_ids: []
duplicate_keys: albumartist album
duplicate_action: ask
bell: no
set_fields: {}
Expand Down
22 changes: 18 additions & 4 deletions beets/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,17 @@ def chosen_ident(self):
elif self.choice_flag is action.APPLY:
return (self.match.info.artist, self.match.info.album)

def chosen_info(self):
"""Returns a dictionnary of metadata about the current choice.
May only be called when the choice flag is ASIS or RETAG
(in which case the data comes from the files' current metadata)
or APPLY (in which case the data comes from the choice).
"""
if self.choice_flag in (action.ASIS, action.RETAG):
return self.cur_info
elif self.choice_flag is action.APPLY:
return self.match.info

def imported_items(self):
"""Return a list of Items that should be added to the library.
Expand Down Expand Up @@ -656,6 +667,8 @@ def lookup_candidates(self):
candidate IDs are stored in self.search_ids: if present, the
initial lookup is restricted to only those IDs.
"""
likelies, consensus = autotag.current_metadata(self.items)
self.cur_info = likelies
artist, album, prop = \
autotag.tag_album(self.items, search_ids=self.search_ids)
self.cur_artist = artist
Expand All @@ -675,10 +688,11 @@ def find_duplicates(self, lib):

duplicates = []
task_paths = {i.path for i in self.items if i}
duplicate_query = dbcore.AndQuery((
dbcore.MatchQuery('albumartist', artist),
dbcore.MatchQuery('album', album),
))
keys = config['import']['duplicate_keys'].as_str().split()
info = self.chosen_info().copy()
info['albumartist'] = artist
subqueries = [ dbcore.MatchQuery(k, info.get(k)) for k in keys ]
duplicate_query = dbcore.AndQuery(subqueries)

for album in lib.albums(duplicate_query):
# Check whether the album paths are all present in the task
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features:
* :doc:`/plugins/kodiupdate`: Now supports multiple kodi instances
:bug:`4101`
* Add the item fields ``bitrate_mode``, ``encoder_info`` and ``encoder_settings``.
* Allow to configure which fields are used to find duplicates

Bug fixes:

Expand Down
10 changes: 10 additions & 0 deletions docs/reference/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,16 @@ with the ``-a`` flag to the :ref:`import-cmd` command.)

Default: ``yes``.

.. _duplicate_keys:

duplicate_keys
~~~~~~~~~~~~~~

The fields used to find duplicates in import task.
If several items have the same value for each key, they will be considered duplicates.

Default: ``albumartist album``

.. _duplicate_action:

duplicate_action
Expand Down

0 comments on commit 3fdfaaa

Please sign in to comment.