diff src/plugins/plugin_misc_radiocol.py @ 901:c238d2c02237

plugin radiocol: add MP3 support
author souliane <souliane@mailoo.org>
date Wed, 05 Mar 2014 23:24:03 +0100
parents c5a8f602662b
children c3fdf187a73a
line wrap: on
line diff
--- a/src/plugins/plugin_misc_radiocol.py	Wed Mar 05 16:09:27 2014 +0100
+++ b/src/plugins/plugin_misc_radiocol.py	Wed Mar 05 23:24:03 2014 +0100
@@ -17,7 +17,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from sat.core.i18n import _
+from sat.core.i18n import _, D_
 from logging import debug, info, warning, error
 from twisted.words.xish import domish
 from twisted.internet import reactor
@@ -29,6 +29,9 @@
 import time
 from os import unlink
 from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError
+from mutagen.mp3 import MP3, HeaderNotFoundError
+from mutagen.easyid3 import EasyID3
+from mutagen.id3 import ID3NoHeaderError
 
 
 NC_RADIOCOL = 'http://www.goffi.org/protocol/radiocol'
@@ -100,11 +103,24 @@
         if not client:
             raise exceptions.NotConnectedProfileError(_("Can't access profile's data"))
         try:
-            song = OggVorbis(song_path)
-        except OggVorbisHeaderError:
-            #this file is not ogg vorbis, we reject it
+            if song_path.lower().endswith('.mp3'):
+                actual_song = MP3(song_path)
+                try:
+                    song = EasyID3(song_path)
+
+                    class Info(object):
+                        def __init__(self, length):
+                            self.length = length
+                    song.info = Info(actual_song.info.length)
+                except ID3NoHeaderError:
+                    song = actual_song
+            else:
+                song = OggVorbis(song_path)
+        except (OggVorbisHeaderError, HeaderNotFoundError):
+            #this file is not ogg vorbis nor mp3, we reject it
             self.deleteFile(song_path)  # FIXME: same host trick (see note above)
-            return defer.fail(exceptions.DataError("Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable"))
+            return defer.fail(exceptions.DataError(D_("The uploaded file has been rejected, only Ogg Vorbis and MP3 songs are accepted.")))
+
         attrs = {'filename': os.path.basename(song_path),
                  'title': song.get("title", ["Unknown"])[0],
                  'artist': song.get("artist", ["Unknown"])[0],