Skip to content

Instantly share code, notes, and snippets.

@grahams
Created February 12, 2012 02:52
Show Gist options
  • Save grahams/1805937 to your computer and use it in GitHub Desktop.
Save grahams/1805937 to your computer and use it in GitHub Desktop.
a little script to help rebuild playlists from m3u files even if the file paths don't match. You'll get false positives as the search is intentionally inclusive. I don't really have a need to polish this up, but thought others may be able to jump off fr
#!/usr/bin/env python
from appscript import app,k
import re
import glob
pluginDir = "/Users/grahams/Desktop/playlists/"
it = app(u"iTunes")
playlistNames = glob.glob(pluginDir + "*.m3u")
for playlistName in playlistNames:
shortName = playlistName.replace(".m3u", "").replace(pluginDir, "")
pl = it.make(new=k.playlist, with_properties={k.name: 'ZZ' + shortName})
f = open(playlistName, 'r')
lines = f.readlines()
for line in lines:
if(line.startswith("#EXTINF")):
partline = line.partition(',')
trackphrase = partline[2].strip()
# remove non-alphanumeric characters and also remove ' and ' so we
# don't fail to match tracks which have ampersands instead
trackphrase = re.sub(r'\W+', ' ', trackphrase)
trackphrase = re.sub(r' and ', ' ', trackphrase)
trackphrase = re.sub(r'_', ' ', trackphrase)
tracks = it.playlists[u'Library'].search(for_=trackphrase)
for x in tracks:
try:
x.duplicate(to=pl)
except:
print("error: " + trackphrase + " pl: " + shortName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment