beginning filesync syncing

This commit is contained in:
Connor Olding 2014-05-03 19:33:38 -07:00
parent 9d475fdba7
commit fef8ae685d
2 changed files with 39 additions and 15 deletions

View file

@ -77,6 +77,8 @@ EasyID3.RegisterTextKey('albumartist', 'TPE2')
EasyID3.RegisterKey('rating', rating_get, rating_set, rating_delete) EasyID3.RegisterKey('rating', rating_get, rating_set, rating_delete)
class SyncFile(MutableMapping): class SyncFile(MutableMapping):
"""Dumb OOP crap that just adds more code
and a sense of self-importance"""
def __init__(self, path): def __init__(self, path):
self.md = mutagen.File(path, easy=True) self.md = mutagen.File(path, easy=True)

View file

@ -17,20 +17,20 @@ from mutaext import SyncFile
goodexts = ('.mp3', '.m4a', '.flac', '.ogg') goodexts = ('.mp3', '.m4a', '.flac', '.ogg')
matchtags = ['artist', 'album', 'title'] matchtags = ['artist', 'album', 'title']
updatabletags = [\ alltags = [\
'albumartist', 'composer', 'comment' \ 'albumartist', 'composer', 'comment' \
'tracknumber', 'discnumber', \ 'tracknumber', 'discnumber', \
'genre', 'date', \ 'genre', 'date', \
] ]
updatabletags.extend(mutaext.replaygain_tags) alltags.extend(mutaext.replaygain_tags)
updatabletags.extend(mutaext.extra_tags) alltags.extend(mutaext.extra_tags)
alltags = list(updatabletags)
alltags.extend(matchtags) alltags.extend(matchtags)
lament = lambda *args, **kwargs: print(*args, file=sys.stderr, **kwargs) 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) 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() extof = lambda p: os.path.splitext(p)[1].lower()
filterext = lambda ps, es: (p for p in ps if extof(p) in es) 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): def shouldsync(md):
rating = md.get('rating', u'') rating = md.get('rating', u'')
@ -53,21 +53,21 @@ def fixmetadata(md):
md['title'] = unicode(fn) md['title'] = unicode(fn)
def findmatching(haystack, needle): def findmatching(haystack, needle):
matchme = [needle[t] for t in matchtags] matchme = [needle[t].lower() for t in matchtags]
ismatch = lambda hay: [hay[t] for t in matchtags] == matchme ismatch = lambda hay: [hay[t].lower() 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?
lament("Warning: duplicate match found:") lament("Duplicate")
lament(u"%(title)s by %(artist)s from %(album)s" % locals()) return None
match.seen = True match.seen = needle.path
return match return match
def updatemetadata(mdold, mdnew): def updatemetadata(mdold, mdnew):
modified = False modified = False
for tag in updatabletags: for tag in alltags:
if tag in mdnew: if tag in mdnew:
if mdnew[tag] != mdold[tag]: if tag not in mdold or mdnew[tag] != mdold[tag]:
mdold[tag] = mdnew[tag] mdold[tag] = mdnew[tag]
modified = True modified = True
elif tag in mdold: elif tag in mdold:
@ -79,8 +79,12 @@ def makefilename(md):
title = md['title'] title = md['title']
artist = md['artist'] artist = md['artist']
album = md['album'] 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): def run(args):
if not len(args) in (2, 3): if not len(args) in (2, 3):
@ -90,7 +94,8 @@ def run(args):
tosync = [] tosync = []
indir = args[1] 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): for p in paths(indir):
md = SyncFile(p) md = SyncFile(p)
@ -118,11 +123,28 @@ def run(args):
md.md.save() md.md.save()
for md in tosync: for md in tosync:
fn = makefilename(md)
fout = os.path.join(outdir, fn)
if md.seen: 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) print("ADD", md.path)
fout = os.path.join(outdir, makefilename(md))
_, ftemp = tempfile.mkstemp() _, ftemp = tempfile.mkstemp()
try: try:
convert.ogg(md.path, ftemp) convert.ogg(md.path, ftemp)