Port the remove duplicates script to PythonQt. Thanks schizosfera. Fixes issue 2036
This commit is contained in:
parent
5dbdc8354b
commit
36ffec7778
|
@ -1,70 +1,46 @@
|
||||||
import clementine
|
import clementine
|
||||||
from clementine import SongInsertVetoListener
|
|
||||||
|
|
||||||
|
|
||||||
class RemoveDuplicatesListener(SongInsertVetoListener):
|
class RemoveDuplicatesListener(clementine.SongInsertVetoListener):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
SongInsertVetoListener.__init__(self)
|
clementine.SongInsertVetoListener.__init__(self)
|
||||||
|
|
||||||
def init_listener(self):
|
def init_listener(self):
|
||||||
for playlist in clementine.playlists.GetAllPlaylists():
|
for playlist in clementine.playlists.GetAllPlaylists():
|
||||||
playlist.AddSongInsertVetoListener(self)
|
playlist.AddSongInsertVetoListener(self)
|
||||||
|
|
||||||
clementine.playlists.PlaylistAdded.connect(self.playlist_added)
|
clementine.playlists.connect("PlaylistAdded(int,QString)", self.playlist_added)
|
||||||
|
|
||||||
def remove_duplicates(self):
|
def remove_duplicates(self):
|
||||||
for playlist in clementine.playlists.GetAllPlaylists():
|
for playlist in clementine.playlists.GetAllPlaylists():
|
||||||
self.remove_duplicates_from(playlist)
|
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 = clementine.playlists.playlist(playlist_id)
|
||||||
|
|
||||||
playlist.AddSongInsertVetoListener(self)
|
playlist.AddSongInsertVetoListener(self)
|
||||||
self.remove_duplicates_from(playlist)
|
self.remove_duplicates_from(playlist)
|
||||||
|
|
||||||
def AboutToInsertSongs(self, old_songs, new_songs):
|
def AboutToInsertSongs(self, old_songs, new_songs):
|
||||||
vetoed = []
|
return [song for song in new_songs if song in old_songs]
|
||||||
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
|
|
||||||
|
|
||||||
def remove_duplicates_from(self, playlist):
|
def remove_duplicates_from(self, playlist):
|
||||||
indices = []
|
duplicate_indices = []
|
||||||
used_urls = set()
|
uniques = []
|
||||||
|
|
||||||
songs = playlist.GetAllSongs()
|
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)
|
for index in range(0, len(songs)):
|
||||||
if len(url) > 0:
|
song = songs[index]
|
||||||
if url in used_urls:
|
if song not in uniques:
|
||||||
indices.append(i)
|
uniques.append(song)
|
||||||
used_urls.add(url)
|
else:
|
||||||
|
duplicate_indices.append(index)
|
||||||
|
|
||||||
if len(indices) > 0:
|
if len(duplicate_indices) > 0:
|
||||||
playlist.RemoveItemsWithoutUndo(indices)
|
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 = RemoveDuplicatesListener()
|
||||||
|
|
||||||
script.init_listener()
|
script.init_listener()
|
||||||
script.remove_duplicates()
|
script.remove_duplicates()
|
||||||
|
|
Loading…
Reference in New Issue