diff --git a/unsync.py b/unsync.py index eaa94dc..72e986b 100755 --- a/unsync.py +++ b/unsync.py @@ -68,7 +68,10 @@ def shouldsync(md): rating = G(md, 'rating') sync = G(md, 'sync') if rating != None: - rating = int(rating[0]) + try: + rating = int(rating[0]) + except ValueError: + rating = None if sync: sync = sync[0].lower() else: @@ -82,15 +85,36 @@ def shouldsync(md): return True return False +def fixmetadata(md): + if 'artist' not in md: + md['artist'] = "Unknown Artist" + if 'album' not in md: + md['album'] = "Unknown Album" + if 'title' not in md: + fn = os.path.basename(md.path) + fn = os.path.splitext(fn)[0] + # TODO: attempt to infer trackNum/diskNum from fn + md['title'] = fn + def findmatching(haystack, needle): + # TODO: don't match mismatched lengths (Xing?) artist = G(needle, 'artist') album = G(needle, 'album') title = G(needle, 'title') - for match in haystack: - if artist == G(match, 'artist') \ - and album == G(match, 'album') \ - and title == G(match, 'title'): - return match + match = None + for hay in haystack: + if artist == G(hay, 'artist') \ + and album == G(hay, 'album') \ + and title == G(hay, 'title'): + match = hay + if match.seen: + # TODO: check other tags and filename and such? + print("Warning: duplicate match found:", file=sys.stderr) + print("{0} by {1} from {2}".format(artist,album,title), file=sys.stderr) + else: + match.seen = True + break + return match def run(args): if not len(args) in (2, 3): @@ -108,26 +132,35 @@ def run(args): for p in paths(indir): md = mutagen.File(p, easy=True) if shouldsync(md): - if inonly: print(p) - else: tosync.append(md) + if inonly: + print(p) + else: + # TODO: don't use custom members on external metadata class + md.path = p + md.seen = False + fixmetadata(md) + tosync.append(md) - if inonly: return 0 + if inonly: + return 0 + # TODO: don't print anything print("Beginning matching...", file=sys.stderr) outdir = args[2] for p in paths(outdir): md = mutagen.File(p, easy=True) match = findmatching(tosync, md) - # TODO: don't print anything - # TODO: don't match mismatched lengths (Xing?) - # TODO: update important tags on mostly matching files - # TODO: don't sync files that wouldn't match! - # TODO: convert files in loop that works on altered tosync if match: - print("MATCHING", p) + print("UPD", p) + # update tags here if updatemetadata returns true else: - # TODO: just delete missing ones here - print("MISSING", p) + print("DEL", p) + # delete files here + + for md in tosync: + if md.seen: + continue + print("ADD", md.path) return 0