~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to scripts/remove-duplicates/remove_duplicates.py

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import clementine
2
 
from clementine import SongInsertVetoListener
3
 
 
4
 
 
5
 
class RemoveDuplicatesListener(SongInsertVetoListener):
6
 
 
7
 
  def __init__(self):
8
 
    SongInsertVetoListener.__init__(self)
9
 
 
10
 
  def init_listener(self):
11
 
    for playlist in clementine.playlists.GetAllPlaylists():
12
 
      playlist.AddSongInsertVetoListener(self)
13
 
      
14
 
    clementine.playlists.PlaylistAdded.connect(self.playlist_added)
15
 
 
16
 
  def remove_duplicates(self):
17
 
    for playlist in clementine.playlists.GetAllPlaylists():
18
 
      self.remove_duplicates_from(playlist)
19
 
 
20
 
  def playlist_added(self, playlist_id):
21
 
    playlist = clementine.playlists.playlist(playlist_id)
22
 
 
23
 
    playlist.AddSongInsertVetoListener(self)
24
 
    self.remove_duplicates_from(playlist)
25
 
 
26
 
  def AboutToInsertSongs(self, old_songs, new_songs):
27
 
    vetoed = []
28
 
    used_urls = set()
29
 
 
30
 
    songs = old_songs + new_songs
31
 
    for song in songs:
32
 
      url = self.url_for_song(song)
33
 
 
34
 
      # don't veto songs without URL (possibly radios)
35
 
      if len(url) > 0:
36
 
        if url in used_urls:
37
 
          vetoed.append(song)
38
 
        used_urls.add(url)
39
 
 
40
 
    return vetoed
41
 
 
42
 
  def remove_duplicates_from(self, playlist):
43
 
    indices = []
44
 
    used_urls = set()
45
 
 
46
 
    songs = playlist.GetAllSongs()
47
 
    for i in range(0, len(songs)):
48
 
      song = songs[i]
49
 
      url = self.url_for_song(song)
50
 
 
51
 
      # ignore songs without URL (possibly radios)
52
 
      if len(url) > 0:
53
 
        if url in used_urls:
54
 
          indices.append(i)
55
 
        used_urls.add(url)
56
 
 
57
 
    if len(indices) > 0:
58
 
      playlist.RemoveItemsWithoutUndo(indices)
59
 
 
60
 
  def url_for_song(self, song):
61
 
    if not song.filename() == "":
62
 
      return song.filename() + ":" + str(song.beginning_nanosec())
63
 
    else:
64
 
      return ""
65
 
                
66
 
 
67
 
script = RemoveDuplicatesListener()
68
 
 
69
 
script.init_listener()
70
 
script.remove_duplicates()
 
 
b'\\ No newline at end of file'