@@ -521,17 +521,18 @@ def skip(self):
521
521
522
522
# Convenient data.
523
523
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).
530
529
"""
531
530
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
533
533
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
535
536
536
537
def imported_items (self ):
537
538
"""Return a list of Items that should be added to the library.
@@ -667,26 +668,34 @@ def find_duplicates(self, lib):
667
668
"""Return a list of albums from `lib` with the same artist and
668
669
album name as the task.
669
670
"""
670
- artist , album = self .chosen_ident ()
671
+ info = self .chosen_info ()
672
+ info ['albumartist' ] = info ['artist' ]
671
673
672
- if artist is None :
674
+ if info [ ' artist' ] is None :
673
675
# As-is import with no artist. Skip check.
674
676
return []
675
677
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.
677
688
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
- ))
682
689
683
- for album in lib .albums (duplicate_query ):
690
+ duplicates = []
691
+ for album in lib .albums (dup_query ):
684
692
# Check whether the album paths are all present in the task
685
693
# i.e. album is being completely re-imported by the task,
686
694
# in which case it is not a duplicate (will be replaced).
687
695
album_paths = {i .path for i in album .items ()}
688
696
if not (album_paths <= task_paths ):
689
697
duplicates .append (album )
698
+
690
699
return duplicates
691
700
692
701
def align_album_level_fields (self ):
@@ -892,12 +901,17 @@ def __init__(self, toppath, item):
892
901
self .is_album = False
893
902
self .paths = [item .path ]
894
903
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 )
897
911
if self .choice_flag in (action .ASIS , action .RETAG ):
898
- return (self .item . artist , self . item . title )
912
+ return dict (self .item )
899
913
elif self .choice_flag is action .APPLY :
900
- return ( self .match .info .artist , self . match . info . title )
914
+ return self .match .info .copy ( )
901
915
902
916
def imported_items (self ):
903
917
return [self .item ]
@@ -918,14 +932,19 @@ def find_duplicates(self, lib):
918
932
"""Return a list of items from `lib` that have the same artist
919
933
and title as the task.
920
934
"""
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
+ })
922
945
923
946
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 ):
929
948
# Existing items not considered duplicates.
930
949
if other_item .path != self .item .path :
931
950
found_items .append (other_item )
0 commit comments