#!/usr/bin/python # # squeezify_playlists.py """Utility for converting M3U playlists to a format and location that the SqueezeBox SlimServer groks. Given: a directory hierarchy containing per-directory playlists, with songs listed as relative filenames in each playlist. Output: s single directory that contains versions of all those playlists, with the songs listed as absolute filenames. """ __author__ = "Leo Breebaart " __version__ = "1.0" __date__ = "16 September 2006" __license__ = "MIT License " import sys import os def collect_playlists(directory): """ Traverse 'directory' and yield all the .m3u filenamess found. Files are detected based on the presence of a case-insensitive '.m3u' extension, and returned as full, absolute pathnames.""" for root, dirs, files in os.walk(directory): for f in files: _, extension = os.path.splitext(f) if extension.lower() == ".m3u": yield os.path.abspath(os.path.join(root, f)) def store_playlist(filename, contents): """ Write 'contents' to 'filename', where 'contents' is a list of lines (newlines included).""" f = open(filename, "w") f.writelines(contents) f.close() def convert_name(filename, targetdir): """ Returns a path that consists of the basename of 'filename' grafted onto 'targetdir': >>> convert_name('/a/b/c/d/joop.m3u', '/x/y/z') '/x/y/z/joop.m3u' """ basename = os.path.basename(filename) return os.path.join(targetdir, basename) def convert_contents(filename): def is_relative_songline(line): line = line.strip() if line == "": return False if line[0] == '#': return False if line[0] == '/': return False if line[1] == ':': return False return True directory = os.path.dirname(filename) newcontents = [] for line in open(filename): if is_relative_songline(line): newcontents.append(os.path.join(directory, line)) else: newcontents.append(line) return newcontents def convert_playlist(filename, targetdir): return (convert_name(filename, targetdir), convert_contents(filename)) def squeezify_playlists(sourcedir, targetdir): for filename in collect_playlists(sourcedir): # print "found", os.path.join(filename) (newname, newcontents) = convert_playlist(filename, targetdir) store_playlist(newname, newcontents) if __name__ == "__main__": try: sourcedir = sys.argv[1] except IndexError: sourcedir = "/backup/cygnus/music/music" targetdir = "/hex/slimserver-playlists" squeezify_playlists(sourcedir, targetdir)