From 36ffec7778817c8fab992890dde6fc2754972321 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Fri, 1 Jul 2011 20:48:11 +0000 Subject: [PATCH] Port the remove duplicates script to PythonQt. Thanks schizosfera. Fixes issue 2036 --- .../remove-duplicates/remove_duplicates.py | 58 ++++++------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/scripts/remove-duplicates/remove_duplicates.py b/scripts/remove-duplicates/remove_duplicates.py index b4c4bd911..4fb4256b5 100644 --- a/scripts/remove-duplicates/remove_duplicates.py +++ b/scripts/remove-duplicates/remove_duplicates.py @@ -1,70 +1,46 @@ import clementine -from clementine import SongInsertVetoListener -class RemoveDuplicatesListener(SongInsertVetoListener): +class RemoveDuplicatesListener(clementine.SongInsertVetoListener): def __init__(self): - SongInsertVetoListener.__init__(self) + clementine.SongInsertVetoListener.__init__(self) def init_listener(self): for playlist in clementine.playlists.GetAllPlaylists(): playlist.AddSongInsertVetoListener(self) - - clementine.playlists.PlaylistAdded.connect(self.playlist_added) + + clementine.playlists.connect("PlaylistAdded(int,QString)", self.playlist_added) def remove_duplicates(self): for playlist in clementine.playlists.GetAllPlaylists(): self.remove_duplicates_from(playlist) - def playlist_added(self, playlist_id): + def playlist_added(self, playlist_id, playlist_name): playlist = clementine.playlists.playlist(playlist_id) playlist.AddSongInsertVetoListener(self) self.remove_duplicates_from(playlist) def AboutToInsertSongs(self, old_songs, new_songs): - vetoed = [] - used_urls = set() - - songs = old_songs + new_songs - for song in songs: - url = self.url_for_song(song) - - # don't veto songs without URL (possibly radios) - if len(url) > 0: - if url in used_urls: - vetoed.append(song) - used_urls.add(url) - - return vetoed + return [song for song in new_songs if song in old_songs] def remove_duplicates_from(self, playlist): - indices = [] - used_urls = set() - + duplicate_indices = [] + uniques = [] songs = playlist.GetAllSongs() - for i in range(0, len(songs)): - song = songs[i] - url = self.url_for_song(song) - # ignore songs without URL (possibly radios) - if len(url) > 0: - if url in used_urls: - indices.append(i) - used_urls.add(url) + for index in range(0, len(songs)): + song = songs[index] + if song not in uniques: + uniques.append(song) + else: + duplicate_indices.append(index) - if len(indices) > 0: - playlist.RemoveItemsWithoutUndo(indices) + if len(duplicate_indices) > 0: + playlist.RemoveItemsWithoutUndo(duplicate_indices) - def url_for_song(self, song): - if not song.filename() == "": - return song.filename() + ":" + str(song.beginning_nanosec()) - else: - return "" - script = RemoveDuplicatesListener() - script.init_listener() -script.remove_duplicates() \ No newline at end of file +script.remove_duplicates()