-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiTunes Synch.command
executable file
·311 lines (246 loc) · 9.8 KB
/
iTunes Synch.command
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash
# Playlists are set in the python code below!
# Device Path: [ to edit for changes ]
CONNECTED_DEVICES_PATH="/Volumes/"
USB_DEVICE_NAME="My USB Drive"
printf "\n#################### iTunes-USB Synchronizer ####################\n\n"
usb_device_path="$CONNECTED_DEVICES_PATH$USB_DEVICE_NAME/"
# Check if the device is connected
if [ $(ls $CONNECTED_DEVICES_PATH | grep -c "$USB_DEVICE_NAME") -eq 1 ]; then
printf "Selected Device Path: '$usb_device_path'\n\n"
else
printf "Device '$USB_DEVICE_NAME' not found!\n\n"
echo "Connected devides:"
echo "$(ls $CONNECTED_DEVICES_PATH | sed 's/^/ - /')"
printf "\nSet USB_DEVICE_NAME to the correct device name!\nBye\n\n"
exit 0
fi
# ----- BEGIN PYTHON CALL -----
python - "$usb_device_path" << 'EOF'
# -*- coding: utf-8 -*-
import os
import sys
import subprocess
import unicodedata
from pprint import pprint
# Playlists to synch: [ to edit for changes ]
# from iTunes to USB (or another device) only
PLAYLISTS = [
{'iTunes': "Itunes Playlist 1", 'USB': "USB Folder 1"},
{'iTunes': "Itunes Playlist 2", 'USB': "USB Folder 2"}
]
# Uncomment the appropriate line
app_name = "Music" # for Catalina
# app_name = "iTunes" # previous versions
def normalize(s):
# To match strings with accents in different encodings
return unicodedata.normalize('NFC', s.decode('utf8'))
def getFileMetadata(filepath,tag):
p1 = subprocess.Popen(['ffprobe',filepath,'-show_format'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p2 = subprocess.Popen(['grep','TAG:' + tag], stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(['cut','-d','=','-f','2'], stdin=p2.stdout, stdout=subprocess.PIPE)
output, error = p3.communicate()
return output.strip()
def getItunesPlaylistsNames():
applescript_command = "tell application \"" + app_name + "\" to get name of playlists"
itunes_process = subprocess.Popen(['osascript','-e',applescript_command], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
itunes_playlists, ip_err = itunes_process.communicate()
if ip_err:
raise Exception("Error: " + str(ip_err))
itunes_playlists_names = []
print "iTunes playlists names:"
for playlist_name in (itunes_playlists).split(','):
print "- " + str(playlist_name)
itunes_playlists_names.append(playlist_name.strip())
return itunes_playlists_names
def getItunesPlaylistFilepaths(itunes_playlist_name):
applescript_command = "tell application \"" + app_name + "\" to get {location} of (every track in playlist \"" + itunes_playlist_name + "\")"
itunes_process = subprocess.Popen(['osascript','-e',applescript_command], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
itunes_data, ip_err = itunes_process.communicate()
if ip_err:
raise Exception("Error: " + str(ip_err))
itunes_songs_list = []
for path in (itunes_data).split('alias')[1:]:
path = path.strip()
if path[-1] == ',':
path = path[:-1]
itunes_songs_list.append('/' + '/'.join(path.split(':')[1:]))
return itunes_songs_list
def getUsbSongsList(usb_playlist_path):
usb_songs_list = []
for f in os.listdir(usb_playlist_path):
if os.path.isfile(os.path.join(usb_playlist_path,f)):
usb_songs_list.append(os.path.join(usb_playlist_path,f))
return usb_songs_list
def getUsbSongs(usb_playlist_path, usb_songs_list):
songs = {}
usb_filenames = []
print "\nUSB songs: ( in '" + usb_playlist_path + "' )"
if len(usb_songs_list) == 0:
print " No songs found."
else:
for usb_song in usb_songs_list:
usb_filename = usb_song.split('/')[-1]
print usb_filename
usb_filenames.append(usb_filename)
# Get artist and title of the song from file metadata
# if the title is missing get it from the filename
artist = getFileMetadata(usb_song,'artist')
title = getFileMetadata(usb_song,'title')
if title == '':
title = usb_song.split('/')[-1][:-4]
# print " " + artist + " - " + title
# The title is normalized to prevent the unmatching of
# the same accented character in different encodings
if artist in songs:
songs[artist][normalize(title)] = usb_song
else:
songs[artist] = {normalize(title): usb_song}
# pprint(songs)
return songs, usb_filenames
def getSongsToAddAndToRemove(itunes_songs_list,itunes_playlist_name, songs, usb_filenames):
songs_to_add = []
songs_to_remove = []
if len(itunes_songs_list) == 0:
print "\niTunes playlist '" + itunes_playlist_name + "' is empty!"
else:
print "\niTunes songs in playlist '" + itunes_playlist_name + "'"
print "( prefix: " + '/'.join(itunes_songs_list[0].split('/')[:-3]) + " )"
for itunes_song in itunes_songs_list:
print ('/').join(itunes_song.split('/')[-3:])
# Get artist and title of the song from file metadata
# if the title is missing get it from the filename
artist = getFileMetadata(itunes_song,'artist')
title = getFileMetadata(itunes_song,'title')
if title == '':
title = itunes_song.split('/')[-1][:-4]
# print " " + artist + " - " + title
if (artist not in songs) or (normalize(title) not in songs[artist]):
# iTunes song is not in USB => it must be added.
# The new filename is the same as the filename in iTunes
# except for the eventual track number at the beginning used by iTunes
itunes_filename = itunes_song.split('/')[-1]
if title[0] == itunes_filename[0]:
# If no track number at the beginning use the filename
new_filename = itunes_filename
else:
# Skip the track number at the beginning
# or use the same filename
# if cannot find the first char of the title
first_char_index = itunes_filename.find(title[0])
if first_char_index == -1:
new_filename = itunes_filename
else:
new_filename = itunes_filename[first_char_index:]
if new_filename in usb_filenames:
# If the filename is already in use in the directory
# append at the end the artist
itunes_artist = itunes_song.split('/')[-3]
extension = new_filename[-4:]
new_filename = new_filename[:-4] + " (" + itunes_artist + ")" + extension
song = {'new_filename': new_filename, 'path': itunes_song}
songs_to_add.append(song)
else:
# iTunes song is in USB => no need to update
# can remove it from songs
del songs[artist][normalize(title)]
if not songs[artist]:
# If no more songs of that artist
# can remove the artist from songs
del songs[artist]
# The remaining songs are not in the iTunes playlist anymore
# so can be removed from usb
for artist in songs:
for title in songs[artist]:
songs_to_remove.append(songs[artist][title])
# printSongsToAdd(songs_to_add)
# printSongsToRemove(songs_to_remove)
return songs_to_add, songs_to_remove
def printSongsToAdd(songs_to_add):
print "\nSongs to add:"
if len(songs_to_add) > 0:
for song in songs_to_add:
print song['path']
else:
print "No new songs to add!"
def printSongsToRemove(songs_to_remove):
print "\nSongs to remove:"
if len(songs_to_remove) > 0:
for song in songs_to_remove:
print song
else:
print "No songs to remove!"
def addNewSongs(songs_to_add, usb_playlist_path):
num_songs_to_add = len(songs_to_add)
if num_songs_to_add > 0:
print "\nAdd new songs:"
path_from = ('/').join(songs_to_add[0]['path'].split('/')[:-3])
print "( from: " + path_from + ", to: " + usb_playlist_path + " )"
counter = 0
for song in songs_to_add:
counter += 1
new_path = usb_playlist_path + "/" + song['new_filename']
path = ('/').join(song['path'].split('/')[-3:])
print "[ " + str(counter) + " di " + str(num_songs_to_add) + " ] " + path + " -> " + song['new_filename'],
copy_process = subprocess.Popen(['cp','-v',song['path'],new_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, error = copy_process.communicate()
if error:
print " [ " + str(error) + " ]"
elif output.find(':') > -1:
print " [ " + str(output) + " ]"
else:
print " [ ok ]"
print " " + str(output)
else:
print "\nNo new songs to add!"
def removeSongs(songs_to_remove, usb_playlist_path):
num_songs_to_remove = len(songs_to_remove)
if num_songs_to_remove > 0:
print "\nRemove songs: ( from: " + usb_playlist_path + " )"
counter = 0
for song in songs_to_remove:
counter += 1
print "[ " + str(counter) + " di " + str(num_songs_to_remove) + " ] " + song.split('/')[-1],
remove_process = subprocess.Popen(['rm',song], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, error = remove_process.communicate()
if error:
print " [ " + str(error) + " ]"
else:
print " [ removed ]"
else:
print "\nNo songs to remove!"
if __name__ == "__main__":
usb_device_path = sys.argv[1]
try:
itunes_playlists_names = getItunesPlaylistsNames()
for playlist in PLAYLISTS:
print "\nSynch USB playlist '" + playlist['USB'] + "' with iTunes playlist '" + playlist['iTunes'] + "':\n"
# Check iTunes playlist
if playlist['iTunes'] in itunes_playlists_names:
itunes_playlist_name = playlist['iTunes']
else:
print "iTunes playlist '" + playlist['iTunes'] + "' not found."
print "Skip to the next playlist."
continue
# Check usb path
usb_playlist_path = usb_device_path + playlist['USB'] # No slash at the end
if not os.path.exists(usb_playlist_path):
print "Directory '" + usb_playlist_path + "' not found."
print "Create directory '" + usb_playlist_path + "'... ",
os.makedirs(usb_playlist_path)
print "ok"
usb_songs_list = getUsbSongsList(usb_playlist_path)
songs, usb_filenames = getUsbSongs(usb_playlist_path, usb_songs_list)
itunes_songs_list = getItunesPlaylistFilepaths(itunes_playlist_name)
songs_to_add, songs_to_remove = getSongsToAddAndToRemove(itunes_songs_list,itunes_playlist_name, songs, usb_filenames)
addNewSongs(songs_to_add, usb_playlist_path)
removeSongs(songs_to_remove, usb_playlist_path)
print "\n"
except Exception, msg:
print msg
sys.exit(1)
sys.exit(0)
EOF
# ----- END PYTHON CALL -----
echo ""
echo "done"