diff --git a/mutaext.py b/mutaext.py index 149828d..8cb42e0 100644 --- a/mutaext.py +++ b/mutaext.py @@ -77,6 +77,8 @@ EasyID3.RegisterTextKey('albumartist', 'TPE2') EasyID3.RegisterKey('rating', rating_get, rating_set, rating_delete) class SyncFile(MutableMapping): + """Dumb OOP crap that just adds more code + and a sense of self-importance""" def __init__(self, path): self.md = mutagen.File(path, easy=True) diff --git a/unsync.py b/unsync.py index a5bfafb..bfcb068 100755 --- a/unsync.py +++ b/unsync.py @@ -17,20 +17,20 @@ from mutaext import SyncFile goodexts = ('.mp3', '.m4a', '.flac', '.ogg') matchtags = ['artist', 'album', 'title'] -updatabletags = [\ +alltags = [\ 'albumartist', 'composer', 'comment' \ 'tracknumber', 'discnumber', \ 'genre', 'date', \ ] -updatabletags.extend(mutaext.replaygain_tags) -updatabletags.extend(mutaext.extra_tags) -alltags = list(updatabletags) +alltags.extend(mutaext.replaygain_tags) +alltags.extend(mutaext.extra_tags) alltags.extend(matchtags) lament = lambda *args, **kwargs: print(*args, file=sys.stderr, **kwargs) walkfiles = lambda w: (os.path.join(r, f) for r, _, fs in w for f in fs) extof = lambda p: os.path.splitext(p)[1].lower() filterext = lambda ps, es: (p for p in ps if extof(p) in es) +ansty = lambda u: str(u.decode('ascii', errors='replace').replace(u'\ufffd', '?')) def shouldsync(md): rating = md.get('rating', u'') @@ -53,21 +53,21 @@ def fixmetadata(md): md['title'] = unicode(fn) def findmatching(haystack, needle): - matchme = [needle[t] for t in matchtags] - ismatch = lambda hay: [hay[t] for t in matchtags] == matchme + matchme = [needle[t].lower() for t in matchtags] + ismatch = lambda hay: [hay[t].lower() for t in matchtags] == matchme for match in (hay for hay in haystack if ismatch(hay)): if match.seen: # TODO: check other tags too? - lament("Warning: duplicate match found:") - lament(u"%(title)s by %(artist)s from %(album)s" % locals()) - match.seen = True + lament("Duplicate") + return None + match.seen = needle.path return match def updatemetadata(mdold, mdnew): modified = False - for tag in updatabletags: + for tag in alltags: if tag in mdnew: - if mdnew[tag] != mdold[tag]: + if tag not in mdold or mdnew[tag] != mdold[tag]: mdold[tag] = mdnew[tag] modified = True elif tag in mdold: @@ -79,8 +79,12 @@ def makefilename(md): title = md['title'] artist = md['artist'] album = md['album'] + sync = md.get('sync') - return u"%(artist)s - %(album)s - %(title)s.ogg" % locals() + # TODO: strip /'s and other possible nasties + if sync: + return "!%(sync)s %(artist)s - %(album)s - %(title)s.ogg" % locals() + return "%(artist)s - %(album)s - %(title)s.ogg" % locals() def run(args): if not len(args) in (2, 3): @@ -90,7 +94,8 @@ def run(args): tosync = [] indir = args[1] - paths = lambda dir: filterext(walkfiles(os.walk(dir)), goodexts) + _paths = lambda dir: filterext(walkfiles(os.walk(dir)), goodexts) + paths = lambda dir: _paths(unicode(dir).encode('utf-8')) for p in paths(indir): md = SyncFile(p) @@ -118,11 +123,28 @@ def run(args): md.md.save() for md in tosync: + fn = makefilename(md) + fout = os.path.join(outdir, fn) + if md.seen: - continue + try: + _from = md.seen + _to = fout.encode('utf-8') + if type(_from) != type(_to): + raise TypeError + + if _from != _to: + print("MOV", _from) + #os.rename(_from, _to) + continue + except: + lament(type(_from), type(_to)) + lament("_from:", [_from]) + lament("_to:", [_to]) + raise + print("ADD", md.path) - fout = os.path.join(outdir, makefilename(md)) _, ftemp = tempfile.mkstemp() try: convert.ogg(md.path, ftemp)