From b4eeededf89853a9eb725d1f294188081f2b8ba7 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 28 Apr 2014 19:31:24 -0700 Subject: [PATCH] it's workingggg better --- convert.py | 16 +++++++-------- mutaext.py | 2 +- unsync.py | 60 +++++++++++++++++++++++------------------------------- 3 files changed, 34 insertions(+), 44 deletions(-) diff --git a/convert.py b/convert.py index 96ff42c..aecb515 100644 --- a/convert.py +++ b/convert.py @@ -1,12 +1,10 @@ import subprocess as sp -import tempfile +import os -def ogg(inf): - _, temppath = tempfile.mkstemp() - p1 = sp.Popen(["ffmpeg", "-loglevel", "warning", "-i", inf, "-f", "flac", "-"], stdout=sp.PIPE) - p2 = sp.Popen(["oggenc", "-Q", "-", "-o", temppath], stdin=p1.stdout, stdout=sp.PIPE) +def ogg(fin, fout): + p1 = sp.Popen(["ffmpeg", "-loglevel", "error", "-i", fin, "-f", "flac", "-"], stdout=sp.PIPE) + p2 = sp.Popen(["oggenc", "-Q", "-q", "5", "-", "-o", fout], stdin=p1.stdout, stdout=sp.PIPE) p1.stdout.close() - _, err = p2.communicate() - if err: - raise Exception(err) - return temppath + p2.communicate() + ret = p1.poll() or p2.poll() + return ret diff --git a/mutaext.py b/mutaext.py index 1cb774b..bea3556 100644 --- a/mutaext.py +++ b/mutaext.py @@ -41,7 +41,7 @@ def _canconv(r): if int(r) != str(int(r)): return False return int(r) >= 1 and int(r) <= 5 - except ValueError, TypeError: + except (ValueError, TypeError): return False def rating_set(id3, key, val): diff --git a/unsync.py b/unsync.py index 9625c36..56ca89b 100755 --- a/unsync.py +++ b/unsync.py @@ -7,6 +7,7 @@ import os import os.path import sys import shutil +import tempfile import mutagen import mutaext @@ -23,12 +24,6 @@ updatabletags.extend(mutaext.extra_tags) alltags = list(updatabletags) alltags.extend(['artist', 'album', 'title']) -def G(d, k): - try: - return d[k] - except KeyError: - return None - def walkfiles(walker): for root, _, files in walker: for f in files: @@ -41,16 +36,14 @@ def filterext(paths, exts): yield p def shouldsync(md): - rating = G(md, 'rating') - sync = G(md, 'sync') + rating = md.get('rating') + sync = md.get('sync', u'') try: rating = int(rating[0]) except: pass if sync: sync = sync[0].lower() - else: - sync = u'' if sync == u'no' or sync == u'space': return False @@ -61,10 +54,8 @@ def shouldsync(md): return False def fixmetadata(md): - if 'artist' not in md: - md['artist'] = "Unknown Artist" - if 'album' not in md: - md['album'] = "Unknown Album" + md['artist'] = md.get('artist', "Unknown Artist") + md['album'] = md.get('album', "Unknown Album") if 'title' not in md: fn = os.path.basename(md.path) fn = os.path.splitext(fn)[0] @@ -73,14 +64,14 @@ def fixmetadata(md): def findmatching(haystack, needle): # TODO: don't match mismatched lengths (Xing?) - artist = G(needle, 'artist') - album = G(needle, 'album') - title = G(needle, 'title') + artist = needle.get('artist') + album = needle.get('album') + title = needle.get('title') match = None for hay in haystack: - if artist == G(hay, 'artist') \ - and album == G(hay, 'album') \ - and title == G(hay, 'title'): + if artist == hay.get('artist') \ + and album == hay.get('album') \ + and title == hay.get('title'): match = hay if match.seen: # TODO: check other tags and filename and such? @@ -93,18 +84,14 @@ def findmatching(haystack, needle): def updatemetadata(mdold, mdnew): modified = False - # TODO: ensure genre and date work properly (special cases in EasyID3) for tag in updatabletags: - if tag in mdnew: - if not tag in mdold: - #print("not in old:",tag) - mdold[tag] = mdnew[tag] - elif mdnew[tag][0] != mdold[tag][0]: - #print("in old:",tag) + if tag in mdnew and len(mdnew[tag]): + if not tag in mdold or mdnew[tag][0] != mdold[tag][0]: mdold[tag] = mdnew[tag] modified = True elif tag in mdold: del mdold[tag] + print('del', tag) modified = True return modified @@ -160,17 +147,22 @@ def run(args): os.remove(p) for md in tosync: - if md.seen == False: - print("ADD", md.path) - outf = os.path.join(outdir, makefilename(md)) - #print("TO", outf) - temppath = convert.ogg(md.path) - mdnew = mutagen.File(temppath, easy=True) + if md.seen: + continue + print("ADD", md.path) + + fout = os.path.join(outdir, makefilename(md)) + _, ftemp = tempfile.mkstemp() + try: + convert.ogg(md.path, ftemp) + mdnew = mutagen.File(ftemp, easy=True) for tag in alltags: if tag in md: mdnew[tag] = md[tag] mdnew.save() - shutil.move(temppath, outf) + shutil.copy2(ftemp, fout) + finally: + os.remove(ftemp) return 0