From 4ee091d833b55932fec345cc7403ef3723ecbd2f Mon Sep 17 00:00:00 2001 From: jvoisin Date: Wed, 4 Apr 2018 21:59:46 +0200 Subject: [PATCH] Improve get_meta in various ways - Normalize the case - Strip \00, \r, space and \n - Flatten metadata lists - Add tests for audio files --- src/audio.py | 6 +++--- tests/test_climat2.py | 24 ++++++++++++++++++++++++ tests/test_libmat2.py | 12 ++++++------ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/audio.py b/src/audio.py index 4da298c..0f4b5c0 100644 --- a/src/audio.py +++ b/src/audio.py @@ -10,7 +10,7 @@ class MutagenParser(abstract.AbstractParser): def get_meta(self): f = mutagen.File(self.filename) if f.tags: - return f.tags + return {k:', '.join(v) for k,v in f.tags.items()} return {} def remove_all(self): @@ -24,10 +24,10 @@ class MP3Parser(MutagenParser): mimetypes = {'audio/mpeg', } def get_meta(self): - meta = super().get_meta() metadata = {} + meta = mutagen.File(self.filename).tags for key in meta: - metadata[key] = meta[key].text + metadata[key.rstrip(' \t\r\n\0')] = ', '.join(map(str, meta[key].text)) return metadata class OGGParser(MutagenParser): diff --git a/tests/test_climat2.py b/tests/test_climat2.py index 16f97a2..cf7a63b 100644 --- a/tests/test_climat2.py +++ b/tests/test_climat2.py @@ -43,3 +43,27 @@ class TestGetMeta(unittest.TestCase): self.assertIn(b'generator: LibreOffice/3.3$Unix', stdout) self.assertIn(b'creator: jvoisin', 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) diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index 34eea49..c2864c6 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py @@ -43,17 +43,17 @@ class TestGetMeta(unittest.TestCase): def test_mp3(self): p = audio.MP3Parser('./tests/data/dirty.mp3') 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): p = audio.OGGParser('./tests/data/dirty.ogg') meta = p.get_meta() - self.assertEqual(meta['TITLE'], ['I am so']) + self.assertEqual(meta['title'], 'I am so') def test_flac(self): p = audio.FLACParser('./tests/data/dirty.flac') meta = p.get_meta() - self.assertEqual(meta['TITLE'], ['I am so']) + self.assertEqual(meta['title'], 'I am so') def test_docx(self): p = office.MSOfficeParser('./tests/data/dirty.docx') @@ -184,7 +184,7 @@ class TestCleaning(unittest.TestCase): p = audio.MP3Parser('./tests/data/clean.mp3') 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() self.assertTrue(ret) @@ -199,7 +199,7 @@ class TestCleaning(unittest.TestCase): p = audio.OGGParser('./tests/data/clean.ogg') meta = p.get_meta() - self.assertEqual(meta['TITLE'], ['I am so']) + self.assertEqual(meta['title'], 'I am so') ret = p.remove_all() self.assertTrue(ret) @@ -214,7 +214,7 @@ class TestCleaning(unittest.TestCase): p = audio.FLACParser('./tests/data/clean.flac') meta = p.get_meta() - self.assertEqual(meta['TITLE'], ['I am so']) + self.assertEqual(meta['title'], 'I am so') ret = p.remove_all() self.assertTrue(ret)