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)
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]

View File

@ -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: