#!/usr/local/bin/python # This script requires "MUSIC.m3u" # MUSIC.m3u is a line-delimited list of all mp3 files you want sorted. # This generates the list of files and ID3 tags. You need 'migratemusic.py' to # do the actually moving import pyid3lib import pickle import re import os # remove upper and lower ascii # Sometimes it's a problem, depending on where your data # is being stored. naughty_high = "[%s]" % "".join([chr(x) for x in range(127,256)]) naughty_low = "[%s]" % "".join([chr(x) for x in range(0,32)]) def love(s): return re.sub(naughty_low, "", re.sub(naughty_high, "", s)) def readsong(filename): tag = pyid3lib.tag(filename) meta = {} #print filename def _set(key, data): #print "%s/%s: %s" % (key, data["frameid"], data["text"]) if key in data and data[key] != data["text"]: raise Exception("CONFLICT FOR %s: %r" % (key.upper(), data)) meta[key] = data["text"] def _artist(data): _set("artist", data) def _album(data): _set("album", data) def _title(data): _set("title", data) def _skip(data): pass def _unknown(data): print "Unknown frame: %s = %r" % (data["frameid"], data) dispatch = { "TPE1": _artist, "TPE2": _artist, "TALB": _album, "TIT1": _skip, #_title, "TIT2": _title, "UNKNOWN": _unknown, } for i in ("TRCK", "TYER", "TPOS", "TCON", "COMM", "TCON", "APIC", "TLAN", "TPUB", "TPE3", "TENC", "PRIV", "TCOM", "TXXX", "TSSE", "TDAT", "TEXT", "TSRC", "UFID", "TCOP", "TIT3", "MCDI", "TBPM", "WCOM", "TLEN", "WOAR", "WOAF", "TOPE", "WXXX", "TFLT", "GEOB", "TSIZ", "TMED", "PCNT", "POPM", "ENCR", "GRID", "TORY", "USER"): dispatch[i] = _skip for i in tag: dispatch.get(i["frameid"], _unknown)(i) for i in ("artist", "album", "title"): if i not in meta: meta[i] = "" return meta; fd = open("MUSIC.m3u") #fd = open("omg") songs = {} count = 1; for i in fd.readlines(): i = i.strip() songs[i] = readsong(i) print "%d:[%s] %s - %s" % (count, songs[i]["artist"], songs[i]["album"], songs[i]["title"], ) print "%d: %s" % (count, i) count += 1 if count % 1000 == 0: output = file("music.pickle.tmp", "w") pickle.dump(songs, output) output.close() output = file("music.pickle", "w") pickle.dump(songs, output) output.close()