Skip to content

Instantly share code, notes, and snippets.

@dupleix
Created July 4, 2010 14:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dupleix/463489 to your computer and use it in GitHub Desktop.
Save dupleix/463489 to your computer and use it in GitHub Desktop.
Synchronize an itunes playlist with any mp3 player
from appscript import *
import re, os
from shutil import copy
MY_PLAYLIST = u'walkman'
DEST = '/Volumes/WALKMAN/MUSIC'
iTunes = app('iTunes')
playlist = None
for p in iTunes.user_playlists():
if p.name() == MY_PLAYLIST:
playlist = p
break
files = [f.location().path for f in playlist.file_tracks()]
musicFiles = [(f, os.path.join(DEST,re.findall('iTunes Music/(.*)', f)[0]) ) for f in files]
#List music files in the mp3 player and delete files not selected
inPlayer = []
toBeCopied = [f[1] for f in musicFiles]
toBeDeleted = []
for root, dirs, files in os.walk(DEST):
for f in files:
if unicode(os.path.join(root, f), 'utf-8') not in toBeCopied:
toBeDeleted.append(os.path.join(root, f))
print len(toBeDeleted), "files to be deleted..."
i=0
for f in toBeDeleted:
try:
os.remove(f)
except:
pass
i=i+1
print i, '/', len(toBeDeleted)
#delete empty directories
os.popen("find %s -type d -empty -delete" % DEST)
#Copy music
print len(musicFiles), "files to be copied..."
i=0
for f in musicFiles:
print i, '/', len(musicFiles), f[0]
if not os.path.exists(f[1]):
if not os.path.exists(os.path.dirname(f[1])):
os.makedirs(os.path.dirname(f[1]))
copy(f[0], os.path.dirname(f[1]))
i = i+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment