sync by track and disc numbers too

This commit is contained in:
Connor Olding 2015-03-24 20:25:59 -07:00
parent 62f12d031a
commit 11b0a802f3
2 changed files with 26 additions and 6 deletions

View file

@ -92,7 +92,9 @@ class SyncFile(MutableMapping):
raise KeyError(key) raise KeyError(key)
def __setitem__(self, key, value): def __setitem__(self, key, value):
if type(value) != str: #if type(value) != str:
#raise ValueError
if type(value) is type(None):
raise ValueError raise ValueError
self.md[key] = [value] self.md[key] = [value]

View file

@ -12,10 +12,9 @@ from mutaext import SyncFile
goodexts = ('.mp3', '.flac', '.ogg') goodexts = ('.mp3', '.flac', '.ogg')
matchtags = ['artist', 'album', 'title'] matchtags = ['artist', 'album', 'title', 'tracknumber', 'discnumber']
alltags = [ alltags = [
'albumartist', 'composer', 'comment', 'albumartist', 'composer', 'comment',
'tracknumber', 'discnumber',
'genre', 'date', 'genre', 'date',
] ]
alltags.extend(mutaext.replaygain_tags) 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' 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): def fixmetadata(md):
md['artist'] = md.get('artist', "Unknown Artist") md['artist'] = md.get('artist', "Unknown Artist")
md['album'] = md.get('album', "Unknown Album") 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: if 'title' not in md:
fn = os.path.basename(md.path) fn = os.path.basename(md.path)
fn = os.path.splitext(fn)[0] fn = os.path.splitext(fn)[0]
md['title'] = str(fn) md['title'] = str(fn)
def findmatching(haystack, needle): def findmatching(haystack, needle):
matchme = [needle[t].lower() for t in matchtags] #matchme = [needle[t].lower() for t in matchtags]
ismatch = lambda hay: [hay[t].lower() for t in matchtags] == matchme #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)): for match in (hay for hay in haystack if ismatch(hay)):
if match.seen: if match.seen:
# TODO: check other tags too? # TODO: check other tags too?
@ -72,8 +85,10 @@ def makefilename(md):
title = md['title'] title = md['title']
artist = md['artist'] artist = md['artist']
album = md['album'] 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 # FAT is a pain to deal with so just use nondescript filenames
crc = crc32(fn.encode('utf-8')) & 0xFFFFFFFF crc = crc32(fn.encode('utf-8')) & 0xFFFFFFFF
fn = '{:08X}.ogg'.format(crc) fn = '{:08X}.ogg'.format(crc)
@ -107,9 +122,11 @@ def run(args):
outdir = args[2] outdir = args[2]
for p in paths(outdir): for p in paths(outdir):
md = SyncFile(p) md = SyncFile(p)
fixmetadata(md)
match = findmatching(tosync, md) match = findmatching(tosync, md)
if match == None: if match == None:
print("DEL", p) print("DEL", p)
print('was', md['title'], 'by', md['artist'])
os.remove(p) os.remove(p)
elif updatemetadata(md, match): elif updatemetadata(md, match):
print("UPD", p) print("UPD", p)
@ -138,6 +155,7 @@ def run(args):
for tag in alltags: for tag in alltags:
if tag in md: if tag in md:
mdnew[tag] = md[tag] mdnew[tag] = md[tag]
fixmetadata(mdnew) # redundant?
mdnew.save() mdnew.save()
copy2(ftemp, fout) copy2(ftemp, fout)
finally: finally: