cc-4105: Fixed up mutagen metadata related stuff. Included utf8 routines from old media monitor
This commit is contained in:
parent
1c09907fd5
commit
58fb152a8d
|
@ -92,13 +92,14 @@ def truncate_to_length(item, length):
|
|||
|
||||
class Metadata(Loggable):
|
||||
def __init__(self, fpath):
|
||||
try: full_mutagen = mutagen.File(self.path, easy=True)
|
||||
except Exception: raise BadSongFile(self.path)
|
||||
try: full_mutagen = mutagen.File(fpath, easy=True)
|
||||
except Exception: raise BadSongFile(fpath)
|
||||
self.path = fpath
|
||||
# TODO : Simplify the way all of these rules are handled right not it's
|
||||
# extremely unclear and needs to be refactored.
|
||||
metadata = {}
|
||||
# Load only the metadata avilable in mutagen into metdata
|
||||
for k,v in full_mutagen:
|
||||
for k,v in full_mutagen.iteritems():
|
||||
# Special handling of attributes here
|
||||
if isinstance(v, list):
|
||||
if len(v) == 1: metadata[k] = v[0]
|
||||
|
@ -118,15 +119,17 @@ class Metadata(Loggable):
|
|||
self.__metadata[ airtime_key ] = muta_v
|
||||
# Now we extra the special values that are calculated from the mutagen
|
||||
# object itself:
|
||||
for special_key,f in airtime_special:
|
||||
for special_key,f in airtime_special.iteritems():
|
||||
new_val = f(full_mutagen)
|
||||
if new_val is not None:
|
||||
self.__metadata[special_key] = f(full_mutagen)
|
||||
# Finally, we "normalize" all the metadata here:
|
||||
self.__metadata = mmp.normalized_metadata(self.__metadata, fpath)
|
||||
# Now we must load the md5:
|
||||
self.__metadata['MDATA_KEY_MD5'] = mmp.fild_md5(fpath)
|
||||
self.__metadata['MDATA_KEY_MD5'] = mmp.file_md5(fpath)
|
||||
|
||||
def extract(self):
|
||||
return copy.deepcopy(self.__metadata)
|
||||
|
||||
def utf8(self):
|
||||
return mmp.convert_dict_value_to_utf8(self.extract())
|
||||
|
|
|
@ -125,7 +125,7 @@ def normalized_metadata(md, original_path):
|
|||
new_md = copy.deepcopy(md)
|
||||
# replace all slashes with dashes
|
||||
for k,v in new_md.iteritems():
|
||||
new_md[k] = str(v).replace('/','-')
|
||||
new_md[k] = unicode(v).replace('/','-')
|
||||
# Specific rules that are applied in a per attribute basis
|
||||
format_rules = {
|
||||
# It's very likely that the following isn't strictly necessary. But the old
|
||||
|
@ -206,6 +206,15 @@ def file_md5(path,max_length=100):
|
|||
return m.hexdigest()
|
||||
else: raise ValueError("'%s' must exist to find its md5")
|
||||
|
||||
def encode_to(obj, encoding='utf-8'):
|
||||
if isinstance(obj, unicode):
|
||||
obj = obj.encode(encoding)
|
||||
return obj
|
||||
|
||||
def convert_dict_value_to_utf8(md):
|
||||
#list comprehension to convert all values of md to utf-8
|
||||
return dict([(item[0], encode_to(item[1], "utf-8")) for item in md.items()])
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
|
Loading…
Reference in New Issue