1
0
mirror of synced 2024-11-22 17:24:23 +01:00

Improve get_meta in various ways

- Normalize the case
- Strip \00, \r, space and \n
- Flatten metadata lists
- Add tests for audio files
This commit is contained in:
jvoisin 2018-04-04 21:59:46 +02:00
parent 1ad817566d
commit 4ee091d833
3 changed files with 33 additions and 9 deletions

View File

@ -10,7 +10,7 @@ class MutagenParser(abstract.AbstractParser):
def get_meta(self): def get_meta(self):
f = mutagen.File(self.filename) f = mutagen.File(self.filename)
if f.tags: if f.tags:
return f.tags return {k:', '.join(v) for k,v in f.tags.items()}
return {} return {}
def remove_all(self): def remove_all(self):
@ -24,10 +24,10 @@ class MP3Parser(MutagenParser):
mimetypes = {'audio/mpeg', } mimetypes = {'audio/mpeg', }
def get_meta(self): def get_meta(self):
meta = super().get_meta()
metadata = {} metadata = {}
meta = mutagen.File(self.filename).tags
for key in meta: for key in meta:
metadata[key] = meta[key].text metadata[key.rstrip(' \t\r\n\0')] = ', '.join(map(str, meta[key].text))
return metadata return metadata
class OGGParser(MutagenParser): class OGGParser(MutagenParser):

View File

@ -43,3 +43,27 @@ class TestGetMeta(unittest.TestCase):
self.assertIn(b'generator: LibreOffice/3.3$Unix', stdout) self.assertIn(b'generator: LibreOffice/3.3$Unix', stdout)
self.assertIn(b'creator: jvoisin', stdout) self.assertIn(b'creator: jvoisin', stdout)
self.assertIn(b'date_time: 2011-07-26 02:40:16', stdout) self.assertIn(b'date_time: 2011-07-26 02:40:16', stdout)
def test_mp3(self):
proc = subprocess.Popen(['./main.py', '--show', './tests/data/dirty.mp3'],
stdout=subprocess.PIPE)
stdout, _ = proc.communicate()
self.assertIn(b'TALB: harmfull', stdout)
self.assertIn(b'COMM::: Thank you for using MAT !', stdout)
def test_flac(self):
proc = subprocess.Popen(['./main.py', '--show', './tests/data/dirty.flac'],
stdout=subprocess.PIPE)
stdout, _ = proc.communicate()
self.assertIn(b'comments: Thank you for using MAT !', stdout)
self.assertIn(b'genre: Python', stdout)
self.assertIn(b'title: I am so', stdout)
def test_ogg(self):
proc = subprocess.Popen(['./main.py', '--show', './tests/data/dirty.ogg'],
stdout=subprocess.PIPE)
stdout, _ = proc.communicate()
self.assertIn(b'comments: Thank you for using MAT !', stdout)
self.assertIn(b'genre: Python', stdout)
self.assertIn(b'i am a : various comment', stdout)
self.assertIn(b'artist: jvoisin', stdout)

View File

@ -43,17 +43,17 @@ class TestGetMeta(unittest.TestCase):
def test_mp3(self): def test_mp3(self):
p = audio.MP3Parser('./tests/data/dirty.mp3') p = audio.MP3Parser('./tests/data/dirty.mp3')
meta = p.get_meta() meta = p.get_meta()
self.assertEqual(meta['TXXX:I am a '], ['various comment']) self.assertEqual(meta['TXXX:I am a'], 'various comment')
def test_ogg(self): def test_ogg(self):
p = audio.OGGParser('./tests/data/dirty.ogg') p = audio.OGGParser('./tests/data/dirty.ogg')
meta = p.get_meta() meta = p.get_meta()
self.assertEqual(meta['TITLE'], ['I am so']) self.assertEqual(meta['title'], 'I am so')
def test_flac(self): def test_flac(self):
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')
def test_docx(self): def test_docx(self):
p = office.MSOfficeParser('./tests/data/dirty.docx') p = office.MSOfficeParser('./tests/data/dirty.docx')
@ -184,7 +184,7 @@ class TestCleaning(unittest.TestCase):
p = audio.MP3Parser('./tests/data/clean.mp3') p = audio.MP3Parser('./tests/data/clean.mp3')
meta = p.get_meta() meta = p.get_meta()
self.assertEqual(meta['TXXX:I am a '], ['various comment']) self.assertEqual(meta['TXXX:I am a'], 'various comment')
ret = p.remove_all() ret = p.remove_all()
self.assertTrue(ret) self.assertTrue(ret)
@ -199,7 +199,7 @@ class TestCleaning(unittest.TestCase):
p = audio.OGGParser('./tests/data/clean.ogg') p = audio.OGGParser('./tests/data/clean.ogg')
meta = p.get_meta() meta = p.get_meta()
self.assertEqual(meta['TITLE'], ['I am so']) self.assertEqual(meta['title'], 'I am so')
ret = p.remove_all() ret = p.remove_all()
self.assertTrue(ret) self.assertTrue(ret)
@ -214,7 +214,7 @@ class TestCleaning(unittest.TestCase):
p = audio.FLACParser('./tests/data/clean.flac') p = audio.FLACParser('./tests/data/clean.flac')
meta = p.get_meta() meta = p.get_meta()
self.assertEqual(meta['TITLE'], ['I am so']) self.assertEqual(meta['title'], 'I am so')
ret = p.remove_all() ret = p.remove_all()
self.assertTrue(ret) self.assertTrue(ret)