it's workingggg better

This commit is contained in:
Connor Olding 2014-04-28 19:31:24 -07:00
parent a236ef5a51
commit b4eeededf8
3 changed files with 34 additions and 44 deletions

View file

@ -1,12 +1,10 @@
import subprocess as sp import subprocess as sp
import tempfile import os
def ogg(inf): def ogg(fin, fout):
_, temppath = tempfile.mkstemp() p1 = sp.Popen(["ffmpeg", "-loglevel", "error", "-i", fin, "-f", "flac", "-"], stdout=sp.PIPE)
p1 = sp.Popen(["ffmpeg", "-loglevel", "warning", "-i", inf, "-f", "flac", "-"], stdout=sp.PIPE) p2 = sp.Popen(["oggenc", "-Q", "-q", "5", "-", "-o", fout], stdin=p1.stdout, stdout=sp.PIPE)
p2 = sp.Popen(["oggenc", "-Q", "-", "-o", temppath], stdin=p1.stdout, stdout=sp.PIPE)
p1.stdout.close() p1.stdout.close()
_, err = p2.communicate() p2.communicate()
if err: ret = p1.poll() or p2.poll()
raise Exception(err) return ret
return temppath

View file

@ -41,7 +41,7 @@ def _canconv(r):
if int(r) != str(int(r)): if int(r) != str(int(r)):
return False return False
return int(r) >= 1 and int(r) <= 5 return int(r) >= 1 and int(r) <= 5
except ValueError, TypeError: except (ValueError, TypeError):
return False return False
def rating_set(id3, key, val): def rating_set(id3, key, val):

View file

@ -7,6 +7,7 @@ import os
import os.path import os.path
import sys import sys
import shutil import shutil
import tempfile
import mutagen import mutagen
import mutaext import mutaext
@ -23,12 +24,6 @@ updatabletags.extend(mutaext.extra_tags)
alltags = list(updatabletags) alltags = list(updatabletags)
alltags.extend(['artist', 'album', 'title']) alltags.extend(['artist', 'album', 'title'])
def G(d, k):
try:
return d[k]
except KeyError:
return None
def walkfiles(walker): def walkfiles(walker):
for root, _, files in walker: for root, _, files in walker:
for f in files: for f in files:
@ -41,16 +36,14 @@ def filterext(paths, exts):
yield p yield p
def shouldsync(md): def shouldsync(md):
rating = G(md, 'rating') rating = md.get('rating')
sync = G(md, 'sync') sync = md.get('sync', u'')
try: try:
rating = int(rating[0]) rating = int(rating[0])
except: except:
pass pass
if sync: if sync:
sync = sync[0].lower() sync = sync[0].lower()
else:
sync = u''
if sync == u'no' or sync == u'space': if sync == u'no' or sync == u'space':
return False return False
@ -61,10 +54,8 @@ def shouldsync(md):
return False return False
def fixmetadata(md): def fixmetadata(md):
if 'artist' not in md: md['artist'] = md.get('artist', "Unknown Artist")
md['artist'] = "Unknown Artist" md['album'] = md.get('album', "Unknown Album")
if 'album' not in md:
md['album'] = "Unknown Album"
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]
@ -73,14 +64,14 @@ def fixmetadata(md):
def findmatching(haystack, needle): def findmatching(haystack, needle):
# TODO: don't match mismatched lengths (Xing?) # TODO: don't match mismatched lengths (Xing?)
artist = G(needle, 'artist') artist = needle.get('artist')
album = G(needle, 'album') album = needle.get('album')
title = G(needle, 'title') title = needle.get('title')
match = None match = None
for hay in haystack: for hay in haystack:
if artist == G(hay, 'artist') \ if artist == hay.get('artist') \
and album == G(hay, 'album') \ and album == hay.get('album') \
and title == G(hay, 'title'): and title == hay.get('title'):
match = hay match = hay
if match.seen: if match.seen:
# TODO: check other tags and filename and such? # TODO: check other tags and filename and such?
@ -93,18 +84,14 @@ def findmatching(haystack, needle):
def updatemetadata(mdold, mdnew): def updatemetadata(mdold, mdnew):
modified = False modified = False
# TODO: ensure genre and date work properly (special cases in EasyID3)
for tag in updatabletags: for tag in updatabletags:
if tag in mdnew: if tag in mdnew and len(mdnew[tag]):
if not tag in mdold: if not tag in mdold or mdnew[tag][0] != mdold[tag][0]:
#print("not in old:",tag)
mdold[tag] = mdnew[tag]
elif mdnew[tag][0] != mdold[tag][0]:
#print("in old:",tag)
mdold[tag] = mdnew[tag] mdold[tag] = mdnew[tag]
modified = True modified = True
elif tag in mdold: elif tag in mdold:
del mdold[tag] del mdold[tag]
print('del', tag)
modified = True modified = True
return modified return modified
@ -160,17 +147,22 @@ def run(args):
os.remove(p) os.remove(p)
for md in tosync: for md in tosync:
if md.seen == False: if md.seen:
print("ADD", md.path) continue
outf = os.path.join(outdir, makefilename(md)) print("ADD", md.path)
#print("TO", outf)
temppath = convert.ogg(md.path) fout = os.path.join(outdir, makefilename(md))
mdnew = mutagen.File(temppath, easy=True) _, ftemp = tempfile.mkstemp()
try:
convert.ogg(md.path, ftemp)
mdnew = mutagen.File(ftemp, easy=True)
for tag in alltags: for tag in alltags:
if tag in md: if tag in md:
mdnew[tag] = md[tag] mdnew[tag] = md[tag]
mdnew.save() mdnew.save()
shutil.move(temppath, outf) shutil.copy2(ftemp, fout)
finally:
os.remove(ftemp)
return 0 return 0