/
synctunes.py
executable file
·155 lines (118 loc) · 4.45 KB
/
synctunes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
import os
import os.path
import plistlib
import sys
import codecs
import urlparse
import urllib
import string
import shutil
theLibraryRealRoot = '/C:/Users/'
theLibraryRoot = os.path.expanduser('/mnt/smb/')
theLibraryHome = os.path.join(theLibraryRoot, 'Eamon Walsh/Music/iTunes/')
theLibraryFile = os.path.join(theLibraryHome, 'iTunes Music Library.xml')
theLibraryPath = os.path.join(theLibraryHome, 'iTunes Media/Music/')
thePhonePath = os.path.expanduser('/mnt/mtp/Music/')
theLibraryTracksFile = 'library.out'
thePhoneTracksFile = 'phone.out'
theAddingTracksFile = 'adding.out'
theRemovingTracksFile = 'removing.out'
dryRun = False if '-f' in sys.argv else True
########################################################################
def unquote(source):
try:
return urllib.unquote(str(source))
except:
return urllib.unquote(source)
########################################################################
def copyFile(thePath):
theSourcePath = os.path.join(theLibraryPath, thePath)
theDestinationPath = os.path.join(thePhonePath, thePath)
theDestinationDirectory = os.path.split(theDestinationPath)[0]
if not os.path.exists(theDestinationDirectory):
os.makedirs(theDestinationDirectory)
if not os.path.exists(theDestinationPath):
shutil.copyfile(theSourcePath, theDestinationPath)
########################################################################
try:
from Foundation import (NSDictionary)
d = NSDictionary.dictionaryWithContentsOfFile_(theLibraryFile)
except:
d = plistlib.readPlist(theLibraryFile)
theTracks = d['Tracks']
########################################################################
libraryPaths = []
phonePaths = []
addingPaths = []
removingPaths = []
validKinds = ['Matched AAC audio file', 'Purchased AAC audio file', 'AAC audio file', 'MPEG audio file']
count = 0
for theKey in theTracks:
theTrack = theTracks[theKey]
theLocation = theTrack['Location']
theURL = urlparse.urlparse(theLocation)
if theURL.scheme != 'file':
print 'Skipping non-file URL', theLocation
continue
if not('Rating' in theTrack and int(theTrack['Rating']) > 0):
# print 'Skipping (no rating)', theLocation
continue
if not(theTrack['Kind'] in validKinds):
print 'Skipping (wrong kind)', theLocation
continue
if int(theTrack['Total Time']) > 25 * 60 * 1000:
print 'Warning (too long)', theLocation
# continue
thePath = unquote(theURL.path)
thePath = string.replace(thePath, theLibraryRealRoot, theLibraryRoot, 1)
if os.path.exists(thePath) == False:
print 'Bad path', str(theURL.path)
sys.exit(1)
thePath = string.replace(thePath, theLibraryPath, '', 1)
libraryPaths.append(unicode(thePath, 'utf-8'))
count = count + 1
libraryPaths = set(libraryPaths)
f = codecs.open(theLibraryTracksFile, 'w', 'utf-8')
for thePath in libraryPaths:
f.write(thePath + '\r\n')
print 'Found %d tracks in library' % len(libraryPaths)
########################################################################
for root, dirs, files in os.walk(thePhonePath):
for name in files:
file = os.path.join(root, name)
file = unicode(file, 'utf-8')
if name[0] == '.':
print 'Skipping', file
continue
file = string.replace(file, thePhonePath, '', 1)
phonePaths.append(file)
f = codecs.open(thePhoneTracksFile, 'w', 'utf-8')
for thePath in phonePaths:
f.write(thePath + '\r\n')
print 'Found %d tracks on phone' % len(phonePaths)
########################################################################
libraryLowerPaths = set([thePath.lower() for thePath in libraryPaths])
phoneLowerPaths = set([thePath.lower() for thePath in phonePaths])
########################################################################
for thePath in phonePaths:
if thePath.lower() not in libraryLowerPaths:
removingPaths.append(thePath)
f = codecs.open(theRemovingTracksFile, 'w', 'utf-8')
for thePath in removingPaths:
f.write(thePath + '\r\n')
if dryRun:
print 'Would remove', thePath, 'from phone'
else:
os.remove(os.path.join(thePhonePath, thePath))
########################################################################
for thePath in libraryPaths:
if thePath.lower() not in phoneLowerPaths:
addingPaths.append(thePath)
f = codecs.open(theAddingTracksFile, 'w', 'utf-8')
for thePath in addingPaths:
f.write(thePath + '\r\n')
if dryRun:
print 'Would add', thePath, 'to phone'
else:
copyFile(thePath)