All pastes #492413 Raw Edit

scabootssca

public python v1 · immutable
·published 2007-05-17 04:47 UTC
rendered paste body
import osimport reimport shutilimport mutagenfrom mutagen.easyid3 import EasyID3# Change this to where your music is storedsearchBases = [r'H:\Music',r'H:\Shared',r'C:\Music',r'C:\Shared']# Change this to where you want to make the new, sorted collectionnewRoot = r'H:\NewMusic'# Change this to where you want the xml to be outputedxmlOut = r'H:\musicList.xml'# Change this to where you want the debug file to appeardebugFile = r'H:\musicSorter.txt'# Dont change this artists = {}counter = {"artists":0,"albums":0,"tracks":0}resub = re.compile("[^a-zA-Z0-9 ]").subdef Debug(bug):	f = file(debugFile,"a")	f.write(bug)	f.close()for dirBase in searchBases:	for root, dirs, files in os.walk(dirBase):		Debug(root)		for fileName in files:			if fileName.lower().endswith(".mp3"):				try:					id3 = EasyID3(root+os.sep+fileName)										artist = resub(""," ".join(id3["artist"]).lower()).strip()					album = resub(""," ".join(id3["album"]).lower()).strip()					title = resub(""," ".join(id3["title"]).lower()).strip()										if not artist in artists: artists[artist] = {}					if not album in artists[artist]: artists[artist][album] = {}					if not title in artists[artist][album]: artists[artist][album][title] = ""										artists[artist][album][title] = root+os.sep+fileName										except:					Debug("%s Has no tag"%fileName)			else:				Debug("%s Is an invalid filetype"%fileName)	print "Finished parsing %s"%dirBase	print "Finished Parsing..."print "Creating Directory tree and copying tracks......."# Generate the structurelastFolder = ""for folder in newRoot.split(os.sep):	lastFolder += folder+os.sep	if not os.path.exists(lastFolder): os.mkdir(lastFolder)	for artist in artists:	counter["artists"] += 1	artistPath = newRoot+os.sep+artist.title()		if not os.path.exists(artistPath): os.mkdir(artistPath)		for album in artists[artist]:		counter["albums"] += 1		albumPath = artistPath+os.sep+album.title()			if not os.path.exists(albumPath): os.mkdir(albumPath)				for track in artists[artist][album]:			counter["tracks"] += 1			print "Coping %s | %s | %s"%(artist.title(),album.title(),track.title())			inFile = artists[artist][album][track]			outFile = albumPath+os.sep+track.title()+".mp3"						if not os.path.exists(outFile):				shutil.copyfile(inFile,outFile)			else:				print outFile,"Exists, skipping..."				print "Creating XML"xml = file(xmlOut,"w")# Write library infoxml.write("<main>\n")xml.write("<libraryInfo>\n")xml.write("	<artists>%s</artists>\n"%counter["artists"])xml.write("	<albums>%s</albums>\n"%counter["albums"])xml.write("	<tracks>%s</tracks>\n"%counter["tracks"])xml.write("</libraryInfo>\n")for artist in artists:	xml.write("<artist name=\"%s\">\n"%artist.title())	for album in artists[artist]:		xml.write("	<album name=\"%s\">\n"%album.title())		for track in artists[artist][album]:			xml.write("		<track>%s</track>\n"%track.title())		xml.write("	</album>\n")	xml.write("</artist>\n")xml.write("</main>\n")xml.close()				print "Done"print "%s Tracks, %s Albums, %s Tracks"%(counter["artists"],counter["albums"],counter["tracks"])