switch to mutagenx and upgrade to python 3
mutagenx is about 13 times faster than mutagen!
This commit is contained in:
parent
d4fbd7a829
commit
2bbe301cdc
2 changed files with 18 additions and 24 deletions
20
mutaext.py
20
mutaext.py
|
@ -1,10 +1,10 @@
|
||||||
from collections import MutableMapping
|
from collections import MutableMapping
|
||||||
import mutagen
|
import mutagenx
|
||||||
import mutagen.id3
|
import mutagenx.id3
|
||||||
from mutagen.easyid3 import EasyID3
|
from mutagenx.easyid3 import EasyID3
|
||||||
|
|
||||||
def popms(id3):
|
def popms(id3):
|
||||||
for k, v in id3.iteritems():
|
for k, v in id3.items():
|
||||||
if k.startswith('POPM'):
|
if k.startswith('POPM'):
|
||||||
yield k, v
|
yield k, v
|
||||||
|
|
||||||
|
@ -29,11 +29,11 @@ def rating_get(id3, key):
|
||||||
rating = id3['TXXX:RATING']
|
rating = id3['TXXX:RATING']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
try:
|
try:
|
||||||
_, popm = popms(id3).next()
|
_, popm = next(popms(id3))
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
return [unicode(byte2rating(popm.rating))]
|
return [str(byte2rating(popm.rating))]
|
||||||
else:
|
else:
|
||||||
return list(rating.text)
|
return list(rating.text)
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ def _canconv(r):
|
||||||
def rating_set(id3, key, val):
|
def rating_set(id3, key, val):
|
||||||
rating_delete(id3, key)
|
rating_delete(id3, key)
|
||||||
if _canconv(val):
|
if _canconv(val):
|
||||||
popm = mutagen.id3.POPM()
|
popm = mutagenx.id3.POPM()
|
||||||
popm.email = "Windows Media Player 9 Series"
|
popm.email = "Windows Media Player 9 Series"
|
||||||
popm.count = 0
|
popm.count = 0
|
||||||
popm.rating = rating2byte(int(val))
|
popm.rating = rating2byte(int(val))
|
||||||
|
@ -56,7 +56,7 @@ def rating_set(id3, key, val):
|
||||||
else:
|
else:
|
||||||
if 'TXXX:RATING' in id3:
|
if 'TXXX:RATING' in id3:
|
||||||
del(id3['TXXX:RATING'])
|
del(id3['TXXX:RATING'])
|
||||||
id3.add(mutagen.id3.TXXX(encoding=3, desc='RATING', text=unicode(val)))
|
id3.add(mutagenx.id3.TXXX(encoding=3, desc='RATING', text=str(val)))
|
||||||
|
|
||||||
def rating_delete(id3, key):
|
def rating_delete(id3, key):
|
||||||
for k, v in popms(id3):
|
for k, v in popms(id3):
|
||||||
|
@ -81,7 +81,7 @@ class SyncFile(MutableMapping):
|
||||||
and a sense of self-importance"""
|
and a sense of self-importance"""
|
||||||
|
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
self.md = mutagen.File(path, easy=True)
|
self.md = mutagenx.File(path, easy=True)
|
||||||
self.path = path
|
self.path = path
|
||||||
self.seen = False
|
self.seen = False
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ class SyncFile(MutableMapping):
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
if type(value) != unicode:
|
if type(value) != str:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
self.md[key] = [value]
|
self.md[key] = [value]
|
||||||
|
|
||||||
|
|
22
unsync.py
22
unsync.py
|
@ -1,7 +1,4 @@
|
||||||
#!/bin/python2
|
#!/bin/python
|
||||||
|
|
||||||
# only using python2 because mutagen
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
|
@ -32,11 +29,11 @@ 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', '?'))
|
ansty = lambda u: str(u.decode('ascii', errors='replace').replace('\ufffd', '?'))
|
||||||
|
|
||||||
def shouldsync(md):
|
def shouldsync(md):
|
||||||
rating = md.get('rating', u'')
|
rating = md.get('rating', '')
|
||||||
sync = md.get('sync', u'')
|
sync = md.get('sync', '')
|
||||||
try:
|
try:
|
||||||
rating = int(rating)
|
rating = int(rating)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -47,12 +44,12 @@ 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'
|
||||||
|
|
||||||
def fixmetadata(md):
|
def fixmetadata(md):
|
||||||
md['artist'] = md.get('artist', u"Unknown Artist")
|
md['artist'] = md.get('artist', "Unknown Artist")
|
||||||
md['album'] = md.get('album', u"Unknown Album")
|
md['album'] = md.get('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]
|
||||||
md['title'] = unicode(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]
|
||||||
|
@ -97,8 +94,7 @@ 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)
|
||||||
|
@ -124,8 +120,6 @@ def run(args):
|
||||||
elif updatemetadata(md, match):
|
elif updatemetadata(md, match):
|
||||||
print("UPD", p)
|
print("UPD", p)
|
||||||
md.md.save()
|
md.md.save()
|
||||||
else:
|
|
||||||
print("___", p)
|
|
||||||
|
|
||||||
lament("[beginning tosync]")
|
lament("[beginning tosync]")
|
||||||
for md in tosync:
|
for md in tosync:
|
||||||
|
|
Loading…
Reference in a new issue