2 # Copyright 2009 James Bunton <jamesbunton@fastmail.fm>
3 # Licensed for distribution under the GPL version 2, check COPYING for details
11 from Foundation
import *
14 def read_plist(filename
):
16 data
= buffer(open(filename
).read())
19 plist
, fmt
, err
= NSPropertyListSerialization
.propertyListFromData_mutabilityOption_format_errorDescription_(data
, NSPropertyListMutableContainers
, None, None)
21 errStr
= err
.encode("utf-8")
22 err
.release() # Doesn't follow Cocoa conventions for some reason
23 raise TypeError(errStr
)
26 class Playlist(NSObject
):
30 def set(self
, name
, pid
, ptype
, tracks
, parent
):
37 if parent
is not None:
38 parent
.children
.append(self
)
40 class ITunesLibrary(NSObject
):
41 def load_(self
, filename
):
43 filename
= "~/Music/iTunes/iTunes Music Library.xml"
44 filename
= os
.path
.expanduser(filename
)
45 yield "Reading library..."
46 plist
= read_plist(os
.path
.expanduser(filename
))
48 raise Exception("Could not find music library: " + filename
)
49 self
.folder
= self
.loc2name(plist
["Music Folder"])
50 pl_tracks
= plist
["Tracks"]
53 for pl_playlist
in plist
["Playlists"]:
54 playlist
= self
.make_playlist(pl_playlist
, pl_tracks
, pl_lookup
)
57 yield "Read playlist: " + playlist
.name
58 self
.playlists
.append(playlist
)
59 pl_lookup
[playlist
.pid
] = playlist
61 def loc2name(self
, location
):
62 return urllib
.splithost(urllib
.splittype(urllib
.unquote(location
))[1])[1]
64 def make_playlist(self
, pl_playlist
, pl_tracks
, pl_lookup
):
65 if int(pl_playlist
.get("Master", 0)):
67 kind
= int(pl_playlist
.get("Distinguished Kind", -1))
72 name
= pl_playlist
["Name"]
73 pid
= pl_playlist
["Playlist Persistent ID"]
84 }.get(kind
, "playlist")
85 elif pl_playlist
.has_key("Smart Info"):
86 ptype
= "smart-playlist"
87 elif int(pl_playlist
.get("Folder", 0)):
94 parent_pid
= pl_playlist
["Parent Persistent ID"]
95 parent
= pl_lookup
[parent_pid
]
99 for item
in pl_playlist
.get("Playlist Items", []):
100 trackID
= item
["Track ID"]
101 filename
= str(pl_tracks
[str(trackID
)]["Location"])
102 filename
= self
.loc2name(filename
)
103 filename
= filename
.decode("utf-8")
104 if not filename
.startswith(self
.folder
):
105 logging
.warn("Skipping: " + filename
)
107 filename
= strip_prefix(filename
, self
.folder
)
108 tracks
.append(filename
)
109 playlist
= Playlist
.alloc().init()
110 playlist
.set(name
, pid
, ptype
, tracks
, parent
)
113 def has_playlist_name(self
, name
):
114 for p
in self
.get_playlists():
119 def get_playlist_name(self
, name
):
120 for playlist
in self
.get_playlists():
121 if playlist
.name
== name
:
124 def get_playlist_pid(self
, pid
):
125 for playlist
in self
.get_playlists():
126 if playlist
.pid
== pid
:
129 def get_playlists(self
):
130 return self
.playlists
134 valid_chars
= frozenset("\\/-_.() abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
135 def encode_filename(filename
):
137 return encoded_names
[filename
]
140 orig_filename
= filename
141 filename
= filename
.encode("ascii", "ignore")
142 filename
= "".join(c
for c
in filename
if c
in valid_chars
)
143 if filename
in encoded_names
:
144 a
, b
= os
.path
.splitext(filename
)
147 encoded_names
[orig_filename
] = filename
150 def strip_prefix(s
, prefix
):
151 assert s
.startswith(prefix
)
153 if s
.startswith("/"):
158 if os
.path
.isdir(path
):
162 path
= os
.path
.split(path
)[0]
164 for path
in reversed(paths
):
170 def export_m3u(dry_run
, dest
, path_prefix
, playlist_name
, files
):
175 playlist_file
= os
.path
.join(dest
, "-Playlists-", playlist_name
) + ".m3u"
176 playlist_file
= encode_filename(playlist_file
)
177 mkdirhier(os
.path
.dirname(playlist_file
))
178 logging
.info("Writing: " + playlist_file
)
179 f
= open(playlist_file
, "w")
180 for filename
in files
:
181 if path_prefix
.find("\\") > 0:
182 filename
= filename
.replace("/", "\\")
183 filename
= encode_filename(filename
)
184 f
.write("%s%s\n" % (path_prefix
, filename
))
187 def sync(dry_run
, source
, dest
, files_to_copy
):
190 logging
.info("Calculating files to sync and deleting old files")
191 source
= source
.encode("utf-8")
192 dest
= dest
.encode("utf-8")
194 class SyncFile(object): pass
195 for f
in files_to_copy
:
197 sf
.orig_filename
= f
.encode("utf-8")
198 sf
.encoded_filename
= encode_filename(f
)
199 filemap
[sf
.encoded_filename
.lower()] = sf
200 files_to_copy
= set(filemap
)
202 for dirpath
, dirnames
, filenames
in os
.walk(dest
):
203 full_dirpath
= dirpath
204 dirpath
= strip_prefix(dirpath
, dest
)
206 for filename
in filenames
:
207 filename
= join(dirpath
, filename
)
209 # Whenever 'file' is deleted OSX will helpfully remove '._file'
210 if not os
.path
.exists(join(dest
, filename
)):
213 if filename
.lower() in files_to_copy
:
214 source_filename
= filemap
[filename
.lower()].orig_filename
215 sourcestat
= os
.stat(join(source
, source_filename
))
216 deststat
= os
.stat(join(dest
, filename
))
217 same_time
= abs(sourcestat
.st_mtime
- deststat
.st_mtime
) < 5
218 same_size
= sourcestat
.st_size
== deststat
.st_size
219 if same_time
and same_size
:
220 files_to_copy
.remove(filename
.lower())
221 yield "Keep: " + filename
223 yield "Update: " + filename
225 elif not filename
.startswith("-Playlists-"):
226 yield "Delete: " + filename
228 os
.unlink(join(dest
, filename
))
230 if len(os
.listdir(full_dirpath
)) == 0:
231 yield "Delete: " + dirpath
233 os
.rmdir(full_dirpath
)
236 logging
.info("Copying new files")
237 files_to_copy
= list(files_to_copy
)
239 for filename
in files_to_copy
:
240 yield "Copy: " + filemap
[filename
].orig_filename
242 source_file
= join(source
, filemap
[filename
].orig_filename
)
243 dest_file
= join(dest
, filemap
[filename
].encoded_filename
)
244 mkdirhier(os
.path
.dirname(dest_file
))
245 shutil
.copy2(source_file
, dest_file
)