#!/usr/bin/env python import pickle import re import os import shutil input = file("music.pickle", "r") songs = pickle.load(input) def mkpath(filename, meta): newpath = "/tank/media/Audio.sorted" for i in ("artist", "album", "title"): if meta[i] == "{missing}": meta[i] = ""; artist = meta["artist"].lower() album = meta["album"].lower() title = meta["title"].lower() score = bool(artist) + bool(album) + bool(title) if score > 0: if not artist: artist = "_artist" if not album: album = "_album" if not title: title = re.sub(r"/", "=slash=", filename) if artist and album and title: return "%s/%s/%s/%s.mp3" % (newpath, artist, album, title) # All data is missing. return "%s/_unknown/%s" % (newpath, re.sub(r"/", "=slash=", filename)) def copysong(src, dst): return (src, dst) data = [] for filename,meta in songs.iteritems(): newpath = mkpath(filename, meta); if newpath: data.append(copysong(filename, newpath)) else: print "# unknown: %s" % filename data.sort() for i in data: src,dst = i src = re.sub(r"^/mnt/", "/tank/media/", src) dstdir = "/".join(dst.split("/")[0:-1]) if not os.path.exists(dstdir): os.makedirs(dstdir) c = 1; while os.path.exists(dst): dst = "%s %s.mp3" % (dst[:-4], c) c += 1 print src print "-> %s" % dst shutil.copy(src, dst)