Implement recursive metadata for FLAC files
Since FLAC files can contain covers, it makes sense to parse their metadata
This commit is contained in:
parent
b2e153b69c
commit
b9dbd12ef9
@ -1,8 +1,11 @@
|
|||||||
|
import mimetypes
|
||||||
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
import tempfile
|
||||||
|
|
||||||
import mutagen
|
import mutagen
|
||||||
|
|
||||||
from . import abstract
|
from . import abstract, parser_factory
|
||||||
|
|
||||||
|
|
||||||
class MutagenParser(abstract.AbstractParser):
|
class MutagenParser(abstract.AbstractParser):
|
||||||
@ -55,6 +58,14 @@ class FLACParser(MutagenParser):
|
|||||||
|
|
||||||
def get_meta(self):
|
def get_meta(self):
|
||||||
meta = super().get_meta()
|
meta = super().get_meta()
|
||||||
if mutagen.File(self.filename).pictures:
|
for num, picture in enumerate(mutagen.File(self.filename).pictures):
|
||||||
meta['Picture'] = 'Cover'
|
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
|
return meta
|
||||||
|
21
mat2
21
mat2
@ -65,25 +65,24 @@ def show_meta(filename: str):
|
|||||||
if p is None:
|
if p is None:
|
||||||
print("[-] %s's format (%s) is not supported" % (filename, mtype))
|
print("[-] %s's format (%s) is not supported" % (filename, mtype))
|
||||||
return
|
return
|
||||||
print("[+] Metadata for %s:" % filename)
|
__print_meta(filename, p.get_meta())
|
||||||
metadata = p.get_meta().items() # type: dict
|
|
||||||
__print_meta(metadata)
|
|
||||||
|
|
||||||
|
|
||||||
def __print_meta(metadata: dict):
|
def __print_meta(filename:str, metadata: dict, depth:int=1):
|
||||||
|
padding = " " * depth*2
|
||||||
if not metadata:
|
if not metadata:
|
||||||
print(" No metadata found")
|
print(padding + "No metadata found")
|
||||||
return
|
return
|
||||||
|
|
||||||
for k, v in metadata:
|
print("[%s] Metadata for %s:" % ('+'*depth, filename))
|
||||||
|
|
||||||
|
for k, v in metadata.items():
|
||||||
if isinstance(v, dict):
|
if isinstance(v, dict):
|
||||||
__print_meta(v)
|
return __print_meta(k, v, depth+1)
|
||||||
else:
|
|
||||||
try: # FIXME this is ugly.
|
try: # FIXME this is ugly.
|
||||||
print(" %s: %s" % (k, v))
|
print(padding + " %s: %s" % (k, v))
|
||||||
except UnicodeEncodeError:
|
except UnicodeEncodeError:
|
||||||
print(" %s: harmful content" % k)
|
print(padding + " %s: harmful content" % k)
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def clean_meta(filename: str, is_lightweight: bool, policy: UnknownMemberPolicy) -> bool:
|
def clean_meta(filename: str, is_lightweight: bool, policy: UnknownMemberPolicy) -> bool:
|
||||||
|
@ -96,7 +96,7 @@ class TestGetMeta(unittest.TestCase):
|
|||||||
p = audio.FLACParser('./tests/data/dirty.flac')
|
p = audio.FLACParser('./tests/data/dirty.flac')
|
||||||
meta = p.get_meta()
|
meta = p.get_meta()
|
||||||
self.assertEqual(meta['title'], 'I am so')
|
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):
|
def test_docx(self):
|
||||||
p = office.MSOfficeParser('./tests/data/dirty.docx')
|
p = office.MSOfficeParser('./tests/data/dirty.docx')
|
||||||
|
Loading…
Reference in New Issue
Block a user