@@ -521,17 +521,18 @@ def skip(self):
521521
522522 # Convenient data.
523523
524- def chosen_ident (self ):
525- """Returns identifying metadata about the current choice. For
526- albums, this is an (artist, album) pair. For items, this is
527- (artist, title). May only be called when the choice flag is ASIS
528- or RETAG (in which case the data comes from the files' current
529- metadata) or APPLY (data comes from the choice).
524+ def chosen_info (self ):
525+ """Return a dictionary of metadata about the current choice.
526+ May only be called when the choice flag is ASIS or RETAG
527+ (in which case the data comes from the files' current metadata)
528+ or APPLY (in which case the data comes from the choice).
530529 """
531530 if self .choice_flag in (action .ASIS , action .RETAG ):
532- return (self .cur_artist , self .cur_album )
531+ likelies , consensus = autotag .current_metadata (self .items )
532+ return likelies
533533 elif self .choice_flag is action .APPLY :
534- return (self .match .info .artist , self .match .info .album )
534+ return self .match .info .copy ()
535+ assert False
535536
536537 def imported_items (self ):
537538 """Return a list of Items that should be added to the library.
@@ -667,26 +668,34 @@ def find_duplicates(self, lib):
667668 """Return a list of albums from `lib` with the same artist and
668669 album name as the task.
669670 """
670- artist , album = self .chosen_ident ()
671+ info = self .chosen_info ()
672+ info ['albumartist' ] = info ['artist' ]
671673
672- if artist is None :
674+ if info [ ' artist' ] is None :
673675 # As-is import with no artist. Skip check.
674676 return []
675677
676- duplicates = []
678+ # Construct a query to find duplicates with this metadata. We
679+ # use a temporary Album object to generate any computed fields.
680+ tmp_album = library .Album (lib , ** info )
681+ keys = config ['import' ]['duplicate_keys' ]['album' ].as_str_seq ()
682+ dup_query = library .Album .all_fields_query ({
683+ key : tmp_album .get (key )
684+ for key in keys
685+ })
686+
687+ # Don't count albums with the same files as duplicates.
677688 task_paths = {i .path for i in self .items if i }
678- duplicate_query = dbcore .AndQuery ((
679- dbcore .MatchQuery ('albumartist' , artist ),
680- dbcore .MatchQuery ('album' , album ),
681- ))
682689
683- for album in lib .albums (duplicate_query ):
690+ duplicates = []
691+ for album in lib .albums (dup_query ):
684692 # Check whether the album paths are all present in the task
685693 # i.e. album is being completely re-imported by the task,
686694 # in which case it is not a duplicate (will be replaced).
687695 album_paths = {i .path for i in album .items ()}
688696 if not (album_paths <= task_paths ):
689697 duplicates .append (album )
698+
690699 return duplicates
691700
692701 def align_album_level_fields (self ):
@@ -892,12 +901,17 @@ def __init__(self, toppath, item):
892901 self .is_album = False
893902 self .paths = [item .path ]
894903
895- def chosen_ident (self ):
896- assert self .choice_flag in (action .ASIS , action .APPLY , action .RETAG )
904+ def chosen_info (self ):
905+ """Return a dictionary of metadata about the current choice.
906+ May only be called when the choice flag is ASIS or RETAG
907+ (in which case the data comes from the files' current metadata)
908+ or APPLY (in which case the data comes from the choice).
909+ """
910+ assert self .choice_flag in (action .ASIS , action .RETAG , action .APPLY )
897911 if self .choice_flag in (action .ASIS , action .RETAG ):
898- return (self .item . artist , self . item . title )
912+ return dict (self .item )
899913 elif self .choice_flag is action .APPLY :
900- return ( self .match .info .artist , self . match . info . title )
914+ return self .match .info .copy ( )
901915
902916 def imported_items (self ):
903917 return [self .item ]
@@ -918,14 +932,19 @@ def find_duplicates(self, lib):
918932 """Return a list of items from `lib` that have the same artist
919933 and title as the task.
920934 """
921- artist , title = self .chosen_ident ()
935+ info = self .chosen_info ()
936+
937+ # Query for existing items using the same metadata. We use a
938+ # temporary `Item` object to generate any computed fields.
939+ tmp_item = library .Item (lib , ** info )
940+ keys = config ['import' ]['duplicate_keys' ]['item' ].as_str_seq ()
941+ dup_query = library .Album .all_fields_query ({
942+ key : tmp_item .get (key )
943+ for key in keys
944+ })
922945
923946 found_items = []
924- query = dbcore .AndQuery ((
925- dbcore .MatchQuery ('artist' , artist ),
926- dbcore .MatchQuery ('title' , title ),
927- ))
928- for other_item in lib .items (query ):
947+ for other_item in lib .items (dup_query ):
929948 # Existing items not considered duplicates.
930949 if other_item .path != self .item .path :
931950 found_items .append (other_item )
0 commit comments