Commit 4ee091d8 authored by jvoisin's avatar jvoisin

Improve get_meta in various ways

- Normalize the case
- Strip \00, \r, space and \n
- Flatten metadata lists
- Add tests for audio files
parent 1ad81756
......@@ -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):
......
......@@ -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)
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment