Skip to content

Instantly share code, notes, and snippets.

@diegodurs
Created July 17, 2012 11:13
Show Gist options
  • Save diegodurs/3128797 to your computer and use it in GitHub Desktop.
Save diegodurs/3128797 to your computer and use it in GitHub Desktop.
iTunes Playlist extractor
# FRENCH - POUR LES NULS
# Pour utiliser ce script il vous faut le language de programmation "python" installé sur votre machine (ce qui est d'origine si vous êtes sur MacOS ou un system Unix). Pour windows, il vous faudra l'installer (http://www.python.org/download), ensuite taper dans la console 'path=C:/Python__/' pour pouvoir utiliser directement python à partir de nimporte où.
#
# Utilisation:
# Exporter une playlist d'iTunes en fichier .txt.
# copier coller le script (le fichier .py) dans le dossier ou se trouve votre playlist.
# Ensuite ouvez un terminal et aller dans ce dossier (le commande 'cd' pour change directory vous permet de naviguer dans les dossiers, 'ls' vous affiche le contenu du dossier courant et 'pwd' vous affiche votre emplacement).
# Taper 'python playlist_script.py <votre_fichier.txt>' sans les '<,>' dans le dossier.
import sys
if len(sys.argv) < 2:
print 'usage \'python '+sys.argv[0]+' <source_filename> <boolean to show track number>'
print 'example \'python '+sys.argv[0]+' mytracklist.txt 1'
exit()
src_file = str(sys.argv[1])
print "Executing from file: %s"%src_file
if len(sys.argv) == 3:
show_number = int(sys.argv[2])
else:
show_number = 1;
fileencoding = "utf-8"
fs = open(src_file,'r')
lines = fs.readline().replace('\00','').replace('\xff','').split('\r') # clean & split in lines
# retrieve column index of name and artist
line = lines[0].split('\t')
index = 0
names = ['Name' , 'Nom' , 'Titre'] # possible header names for Name, add yours here (for other language)
artists = ['Artists' ,'Artiste'] # same for artists
index_name = 0
index_art = 1
for header in line:
if header in names:
index_name = index
if header in artists:
index_art = index
index +=1
# get to the songs
songs = list() # list of tuple (artist, name)
for line in lines:
line = line.split('\t')
try :
songs.append( (line[index_name],line[index_art]) )
except:
jenairienafoutre = True
# save in a file
# name will be <original>_tracklist.txt
dfile = src_file.split('.txt')[0] + '_tracklist.txt'
fd = open(dfile,'w')
print 'Result (stored in \'%s\'):\n'%dfile
i = 0;
for song in songs:
prt = ''
if(show_number):
prt = str(i) + ' - '
prt += song[1]+' - '+song[0]
i = i+1;
fd.write(prt)
print prt
print '\n'+('-'*25)+'by Diego d\'Ursel'
fs.close()
fd.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment