1
0
mirror of synced 2024-11-06 01:18:51 +01:00

Implement mp3 and ogg support

This commit is contained in:
jvoisin 2018-03-25 16:17:41 +02:00
parent 4fa490d941
commit 19a8fd97aa
4 changed files with 77 additions and 2 deletions

35
src/parsers/audio.py Normal file
View File

@ -0,0 +1,35 @@
import subprocess
import shutil
import json
import mutagen
from . import abstract
class MutagenParser(abstract.AbstractParser):
def get_meta(self):
f = mutagen.File(self.filename)
if f.tags:
return f.tags
return {}
def remove_all(self):
shutil.copy(self.filename, self.output_filename)
f = mutagen.File(self.output_filename)
f.delete()
f.save()
return True
class MP3Parser(MutagenParser):
mimetypes = {'audio/mpeg', }
def get_meta(self):
metadata = {}
f = mutagen.File(self.filename)
if f.tags:
for key in f.tags:
metadata[key] = f.tags[key].text
return metadata
class OGGParser(MutagenParser):
mimetypes = {'audio/ogg', }

BIN
tests/data/dirty.mp3 Normal file

Binary file not shown.

BIN
tests/data/dirty.ogg Normal file

Binary file not shown.

View File

@ -5,7 +5,7 @@ import shutil
import os
from src import parsers
from src.parsers import pdf, png, jpg
from src.parsers import pdf, png, jpg, audio
class TestGetMeta(unittest.TestCase):
def test_pdf(self):
@ -25,6 +25,17 @@ class TestGetMeta(unittest.TestCase):
meta = p.get_meta()
self.assertEqual(meta['Comment'], 'Created with GIMP')
def test_mp3(self):
p = audio.MP3Parser('./tests/data/dirty.mp3')
meta = p.get_meta()
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'])
class TestCleaning(unittest.TestCase):
def test_pdf(self):
shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf')
@ -57,7 +68,6 @@ class TestCleaning(unittest.TestCase):
os.remove('./tests/data/clean.png')
def test_jpg(self):
shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg')
p = jpg.JPGParser('./tests/data/clean.jpg')
@ -72,3 +82,33 @@ class TestCleaning(unittest.TestCase):
self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.jpg')
def test_mp3(self):
shutil.copy('./tests/data/dirty.mp3', './tests/data/clean.mp3')
p = audio.MP3Parser('./tests/data/clean.mp3')
meta = p.get_meta()
self.assertEqual(meta['TXXX:I am a '], ['various comment'])
ret = p.remove_all()
self.assertTrue(ret)
p = audio.MP3Parser('./tests/data/clean.mp3.cleaned')
self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.mp3')
def test_ogg(self):
shutil.copy('./tests/data/dirty.ogg', './tests/data/clean.ogg')
p = audio.OGGParser('./tests/data/clean.ogg')
meta = p.get_meta()
self.assertEqual(meta['TITLE'], ['I am so'])
ret = p.remove_all()
self.assertTrue(ret)
p = audio.OGGParser('./tests/data/clean.ogg.cleaned')
self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.ogg')