From 2bbe301cdc7bcc869fde08cbda10667bac84c37e Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sun, 2 Nov 2014 12:30:29 -0800 Subject: [PATCH] switch to mutagenx and upgrade to python 3 mutagenx is about 13 times faster than mutagen! --- mutaext.py | 20 ++++++++++---------- unsync.py | 22 ++++++++-------------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/mutaext.py b/mutaext.py index ea3c0fe..504cabb 100644 --- a/mutaext.py +++ b/mutaext.py @@ -1,10 +1,10 @@ from collections import MutableMapping -import mutagen -import mutagen.id3 -from mutagen.easyid3 import EasyID3 +import mutagenx +import mutagenx.id3 +from mutagenx.easyid3 import EasyID3 def popms(id3): - for k, v in id3.iteritems(): + for k, v in id3.items(): if k.startswith('POPM'): yield k, v @@ -29,11 +29,11 @@ def rating_get(id3, key): rating = id3['TXXX:RATING'] except KeyError: try: - _, popm = popms(id3).next() + _, popm = next(popms(id3)) except StopIteration: return [] else: - return [unicode(byte2rating(popm.rating))] + return [str(byte2rating(popm.rating))] else: return list(rating.text) @@ -48,7 +48,7 @@ def _canconv(r): def rating_set(id3, key, val): rating_delete(id3, key) if _canconv(val): - popm = mutagen.id3.POPM() + popm = mutagenx.id3.POPM() popm.email = "Windows Media Player 9 Series" popm.count = 0 popm.rating = rating2byte(int(val)) @@ -56,7 +56,7 @@ def rating_set(id3, key, val): else: if 'TXXX:RATING' in id3: 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): for k, v in popms(id3): @@ -81,7 +81,7 @@ class SyncFile(MutableMapping): and a sense of self-importance""" def __init__(self, path): - self.md = mutagen.File(path, easy=True) + self.md = mutagenx.File(path, easy=True) self.path = path self.seen = False @@ -95,7 +95,7 @@ class SyncFile(MutableMapping): raise KeyError(key) def __setitem__(self, key, value): - if type(value) != unicode: + if type(value) != str: raise ValueError self.md[key] = [value] diff --git a/unsync.py b/unsync.py index 7770115..1adfa36 100755 --- a/unsync.py +++ b/unsync.py @@ -1,7 +1,4 @@ -#!/bin/python2 - -# only using python2 because mutagen -from __future__ import print_function +#!/bin/python import os 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) extof = lambda p: os.path.splitext(p)[1].lower() 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): - rating = md.get('rating', u'') - sync = md.get('sync', u'') + rating = md.get('rating', '') + sync = md.get('sync', '') try: rating = int(rating) 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' def fixmetadata(md): - md['artist'] = md.get('artist', u"Unknown Artist") - md['album'] = md.get('album', u"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] - md['title'] = unicode(fn) + md['title'] = str(fn) def findmatching(haystack, needle): matchme = [needle[t].lower() for t in matchtags] @@ -97,8 +94,7 @@ def run(args): tosync = [] indir = args[1] - _paths = lambda dir: filterext(walkfiles(os.walk(dir)), goodexts) - paths = lambda dir: _paths(unicode(dir).encode('utf-8')) + paths = lambda dir: filterext(walkfiles(os.walk(dir)), goodexts) for p in paths(indir): md = SyncFile(p) @@ -124,8 +120,6 @@ def run(args): elif updatemetadata(md, match): print("UPD", p) md.md.save() - else: - print("___", p) lament("[beginning tosync]") for md in tosync: