1
0
mirror of synced 2024-11-22 09:14:23 +01:00

Implement recursive metadata for FLAC files

Since FLAC files can contain covers, it makes sense
to parse their metadata
This commit is contained in:
jvoisin 2018-10-11 19:52:47 +02:00
parent b2e153b69c
commit b9dbd12ef9
3 changed files with 27 additions and 17 deletions

View File

@ -1,8 +1,11 @@
import mimetypes
import os
import shutil
import tempfile
import mutagen
from . import abstract
from . import abstract, parser_factory
class MutagenParser(abstract.AbstractParser):
@ -55,6 +58,14 @@ class FLACParser(MutagenParser):
def get_meta(self):
meta = super().get_meta()
if mutagen.File(self.filename).pictures:
meta['Picture'] = 'Cover'
for num, picture in enumerate(mutagen.File(self.filename).pictures):
name = picture.desc if picture.desc else 'Cover %d' % num
_, fname = tempfile.mkstemp()
with open(fname, 'wb') as f:
f.write(picture.data)
extension = mimetypes.guess_extension(picture.mime)
shutil.move(fname, fname + extension)
p, _ = parser_factory.get_parser(fname+extension)
meta[name] = p.get_meta() if p else 'harmful data'
os.remove(fname + extension)
return meta

21
mat2
View File

@ -65,25 +65,24 @@ def show_meta(filename: str):
if p is None:
print("[-] %s's format (%s) is not supported" % (filename, mtype))
return
print("[+] Metadata for %s:" % filename)
metadata = p.get_meta().items() # type: dict
__print_meta(metadata)
__print_meta(filename, p.get_meta())
def __print_meta(metadata: dict):
def __print_meta(filename:str, metadata: dict, depth:int=1):
padding = " " * depth*2
if not metadata:
print(" No metadata found")
print(padding + "No metadata found")
return
for k, v in metadata:
print("[%s] Metadata for %s:" % ('+'*depth, filename))
for k, v in metadata.items():
if isinstance(v, dict):
__print_meta(v)
else:
return __print_meta(k, v, depth+1)
try: # FIXME this is ugly.
print(" %s: %s" % (k, v))
print(padding + " %s: %s" % (k, v))
except UnicodeEncodeError:
print(" %s: harmful content" % k)
return
print(padding + " %s: harmful content" % k)
def clean_meta(filename: str, is_lightweight: bool, policy: UnknownMemberPolicy) -> bool:

View File

@ -96,7 +96,7 @@ class TestGetMeta(unittest.TestCase):
p = audio.FLACParser('./tests/data/dirty.flac')
meta = p.get_meta()
self.assertEqual(meta['title'], 'I am so')
self.assertEqual(meta['Picture'], 'Cover')
self.assertEqual(meta['Cover 0'], {'Comment': 'Created with GIMP'})
def test_docx(self):
p = office.MSOfficeParser('./tests/data/dirty.docx')