Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
900:21681070f913 | 901:c238d2c02237 |
---|---|
15 # GNU Affero General Public License for more details. | 15 # GNU Affero General Public License for more details. |
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 17 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _, D_ |
21 from logging import debug, info, warning, error | 21 from logging import debug, info, warning, error |
22 from twisted.words.xish import domish | 22 from twisted.words.xish import domish |
23 from twisted.internet import reactor | 23 from twisted.internet import reactor |
24 from twisted.words.protocols.jabber import jid | 24 from twisted.words.protocols.jabber import jid |
25 from twisted.internet import defer | 25 from twisted.internet import defer |
27 import os.path | 27 import os.path |
28 import copy | 28 import copy |
29 import time | 29 import time |
30 from os import unlink | 30 from os import unlink |
31 from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError | 31 from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError |
32 from mutagen.mp3 import MP3, HeaderNotFoundError | |
33 from mutagen.easyid3 import EasyID3 | |
34 from mutagen.id3 import ID3NoHeaderError | |
32 | 35 |
33 | 36 |
34 NC_RADIOCOL = 'http://www.goffi.org/protocol/radiocol' | 37 NC_RADIOCOL = 'http://www.goffi.org/protocol/radiocol' |
35 RADIOC_TAG = 'radiocol' | 38 RADIOC_TAG = 'radiocol' |
36 | 39 |
98 # check data. Referee will have to parse the song himself to check it | 101 # check data. Referee will have to parse the song himself to check it |
99 client = self.host.getClient(profile) | 102 client = self.host.getClient(profile) |
100 if not client: | 103 if not client: |
101 raise exceptions.NotConnectedProfileError(_("Can't access profile's data")) | 104 raise exceptions.NotConnectedProfileError(_("Can't access profile's data")) |
102 try: | 105 try: |
103 song = OggVorbis(song_path) | 106 if song_path.lower().endswith('.mp3'): |
104 except OggVorbisHeaderError: | 107 actual_song = MP3(song_path) |
105 #this file is not ogg vorbis, we reject it | 108 try: |
109 song = EasyID3(song_path) | |
110 | |
111 class Info(object): | |
112 def __init__(self, length): | |
113 self.length = length | |
114 song.info = Info(actual_song.info.length) | |
115 except ID3NoHeaderError: | |
116 song = actual_song | |
117 else: | |
118 song = OggVorbis(song_path) | |
119 except (OggVorbisHeaderError, HeaderNotFoundError): | |
120 #this file is not ogg vorbis nor mp3, we reject it | |
106 self.deleteFile(song_path) # FIXME: same host trick (see note above) | 121 self.deleteFile(song_path) # FIXME: same host trick (see note above) |
107 return defer.fail(exceptions.DataError("Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable")) | 122 return defer.fail(exceptions.DataError(D_("The uploaded file has been rejected, only Ogg Vorbis and MP3 songs are accepted."))) |
123 | |
108 attrs = {'filename': os.path.basename(song_path), | 124 attrs = {'filename': os.path.basename(song_path), |
109 'title': song.get("title", ["Unknown"])[0], | 125 'title': song.get("title", ["Unknown"])[0], |
110 'artist': song.get("artist", ["Unknown"])[0], | 126 'artist': song.get("artist", ["Unknown"])[0], |
111 'album': song.get("album", ["Unknown"])[0], | 127 'album': song.get("album", ["Unknown"])[0], |
112 'length': str(song.info.length) | 128 'length': str(song.info.length) |