From b9dbd12ef91f4dfba8c1d0737ab52ff87ac515f1 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Thu, 11 Oct 2018 19:52:47 +0200 Subject: [PATCH] Implement recursive metadata for FLAC files Since FLAC files can contain covers, it makes sense to parse their metadata --- libmat2/audio.py | 17 ++++++++++++++--- mat2 | 25 ++++++++++++------------- tests/test_libmat2.py | 2 +- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/libmat2/audio.py b/libmat2/audio.py index f2a5e94..b67f766 100644 --- a/libmat2/audio.py +++ b/libmat2/audio.py @@ -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 diff --git a/mat2 b/mat2 index cc193e9..2a76b4c 100755 --- a/mat2 +++ b/mat2 @@ -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: - try: # FIXME this is ugly. - print(" %s: %s" % (k, v)) - except UnicodeEncodeError: - print(" %s: harmful content" % k) - return + return __print_meta(k, v, depth+1) + try: # FIXME this is ugly. + print(padding + " %s: %s" % (k, v)) + except UnicodeEncodeError: + print(padding + " %s: harmful content" % k) def clean_meta(filename: str, is_lightweight: bool, policy: UnknownMemberPolicy) -> bool: diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index 2f7d0c5..6a2af91 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py @@ -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')