#!/usr/bin/env python # -*- coding: utf-8 -*- """A small little program to grab all playlists from Winamp's media library, name them (nicely), and put them wherever. NOTE: playlists may need search + replace to correct the file paths within. """ from xml.dom import minidom from os import getenv, listdir from os.path import exists # Variables: change these to your liking # the drive letter that your music is in, like 'C:' or 'D:' # default assumes that your music is on the same drive as your user profile musicDrive = getenv("HOMEDRIVE") # where to put playlists outDir = getenv("USERPROFILE") + "\\Music\\" # should be the same as outDir (minus drive letter), since m3u looks for files from the directory that the m3u file is in stripFromEntries = outDir.split(musicDrive)[1] # locate the file on disk and correct filename case in the output. # useful if you want to use playlists on case sensitive systems (Linux) # True will correct, False will leave as is correctCase = True # if file does not exist, it will still be included in the output playlist. # program will always check and output if a file does not exist, regardless of this variable. # True will keep, False will omit keepDeadFiles = True # don't touch these, this is the actual program # where the playlists are mlDir = getenv("APPDATA") + "\\Winamp\\Plugins\\ml\\" # the 'playlist m3u file name' to 'media library playlist name' XML file playlistXML = mlDir + "playlists.xml" if __name__ == "__main__": print(__doc__) if not exists(playlistXML): try: input("Can't find playlist xml. Goodbye!") except: pass exit(0) print("Writing to " + outDir) if correctCase: print("Correcting filename case in playlists.") else: print("Keeping original filename case in playlists.") if keepDeadFiles: print("Keeping dead files in playlists.") else: print("Omitting dead files in playlists.") doc = minidom.parse(playlistXML) for playlist in doc.documentElement.childNodes: if playlist.nodeName == 'playlist': # found playlist in XML outName = playlist.getAttribute("title") + ".m3u8" print("\nProcessing " + outName) inFile = open(mlDir + playlist.getAttribute("filename"), "rU", encoding = "utf-8") outFile = open(outDir + outName, "w", encoding = "utf-8") firstLine = True position = 0 for line in inFile: # loop over ALL lines of the m3u toWrite = "" try: if line[0] != '#' and not firstLine: position += 1 if not exists(musicDrive + line.strip()): raise WindowsError if correctCase: parts = line.strip().split('\\') parts = parts[1:] nextPart = musicDrive for part in parts: found = False toWrite += (nextPart + '\\') for entry in listdir(toWrite): if entry.lower() == part.lower(): nextPart = entry found = True break if not found: raise WindowsError toWrite += parts[-1] + '\n' toWrite = toWrite.split(musicDrive)[1] if toWrite.strip().lower() != line.strip().lower(): # match failure print("compare failure:") print(toWrite.strip()+"\n"+line.strip()+"\n") toWrite = line else: toWrite = line outFile.write(toWrite.replace(stripFromEntries,"")) except WindowsError as W: # file not found print("\tCan't find "+ str(position) + ". " + musicDrive + line.strip()) if keepDeadFiles: outFile.write(line.replace(stripFromEntries,"")) firstLine = False inFile.close() outFile.close() print("Wrote " + outName) try: input("\nDone!") except: pass