From 11b0a802f33ef47fcd6e755b83e1ed5126acb53c Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 24 Mar 2015 20:25:59 -0700 Subject: [PATCH] sync by track and disc numbers too --- mutaext.py | 4 +++- unsync.py | 28 +++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/mutaext.py b/mutaext.py index ce3a785..18a73d6 100644 --- a/mutaext.py +++ b/mutaext.py @@ -92,7 +92,9 @@ class SyncFile(MutableMapping): raise KeyError(key) def __setitem__(self, key, value): - if type(value) != str: + #if type(value) != str: + #raise ValueError + if type(value) is type(None): raise ValueError self.md[key] = [value] diff --git a/unsync.py b/unsync.py index 5c49a80..d70cb78 100755 --- a/unsync.py +++ b/unsync.py @@ -12,10 +12,9 @@ from mutaext import SyncFile goodexts = ('.mp3', '.flac', '.ogg') -matchtags = ['artist', 'album', 'title'] +matchtags = ['artist', 'album', 'title', 'tracknumber', 'discnumber'] alltags = [ 'albumartist', 'composer', 'comment', - 'tracknumber', 'discnumber', 'genre', 'date', ] alltags.extend(mutaext.replaygain_tags) @@ -37,17 +36,31 @@ def shouldsync(md): return sync == 'yes' or type(rating) == int and rating >= 3 and sync != 'no' and sync != 'space' +import re +re_digits = re.compile(r'\d+') +def tonumber(crap): + if crap is None or len(crap) == 0: + return 0 + nums = re_digits.findall(crap) + if len(nums) == 0: + return 0 + return int(nums[0]) + def fixmetadata(md): md['artist'] = md.get('artist', "Unknown Artist") md['album'] = md.get('album', "Unknown Album") + md['discnumber'] = str(tonumber(md.get('discnumber', '0'))) + md['tracknumber'] = str(tonumber(md.get('tracknumber', '0'))) if 'title' not in md: fn = os.path.basename(md.path) fn = os.path.splitext(fn)[0] md['title'] = str(fn) def findmatching(haystack, needle): - matchme = [needle[t].lower() for t in matchtags] - ismatch = lambda hay: [hay[t].lower() for t in matchtags] == matchme + #matchme = [needle[t].lower() for t in matchtags] + #ismatch = lambda hay: [hay[t].lower() for t in matchtags] == matchme + matchme = [needle[t] for t in matchtags] + ismatch = lambda hay: [hay[t] for t in matchtags] == matchme for match in (hay for hay in haystack if ismatch(hay)): if match.seen: # TODO: check other tags too? @@ -72,8 +85,10 @@ def makefilename(md): title = md['title'] artist = md['artist'] album = md['album'] + track = md['tracknumber'] + disc = md['discnumber'] - fn = "%(artist)s - %(album)s - %(title)s" % locals() + fn = "%(disc)s-%(track)s - %(artist)s - %(album)s - %(title)s" % locals() # FAT is a pain to deal with so just use nondescript filenames crc = crc32(fn.encode('utf-8')) & 0xFFFFFFFF fn = '{:08X}.ogg'.format(crc) @@ -107,9 +122,11 @@ def run(args): outdir = args[2] for p in paths(outdir): md = SyncFile(p) + fixmetadata(md) match = findmatching(tosync, md) if match == None: print("DEL", p) + print('was', md['title'], 'by', md['artist']) os.remove(p) elif updatemetadata(md, match): print("UPD", p) @@ -138,6 +155,7 @@ def run(args): for tag in alltags: if tag in md: mdnew[tag] = md[tag] + fixmetadata(mdnew) # redundant? mdnew.save() copy2(ftemp, fout) finally: