Port the remove duplicates script to PythonQt. Thanks schizosfera. Fixes issue 2036

This commit is contained in:
David Sansome 2011-07-01 20:48:11 +00:00
parent 5dbdc8354b
commit 36ffec7778
1 changed files with 17 additions and 41 deletions

View File

@ -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)
if len(indices) > 0:
playlist.RemoveItemsWithoutUndo(indices)
def url_for_song(self, song):
if not song.filename() == "":
return song.filename() + ":" + str(song.beginning_nanosec())
for index in range(0, len(songs)):
song = songs[index]
if song not in uniques:
uniques.append(song)
else:
return ""
duplicate_indices.append(index)
if len(duplicate_indices) > 0:
playlist.RemoveItemsWithoutUndo(duplicate_indices)
script = RemoveDuplicatesListener()
script.init_listener()
script.remove_duplicates()