comparison src/plugins/plugin_misc_radiocol.py @ 1359:83127a4c89ce frontends_multi_profiles

plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
author souliane <souliane@mailoo.org>
date Wed, 11 Mar 2015 12:36:22 +0100
parents 83ed877541e3
children 069ad98b360d
comparison
equal deleted inserted replaced
1358:bf3f669a6052 1359:83127a4c89ce
65 65
66 def __init__(self, host): 66 def __init__(self, host):
67 log.info(_("Radio collective initialization")) 67 log.info(_("Radio collective initialization"))
68 self.inheritFromRoomGame(host) 68 self.inheritFromRoomGame(host)
69 RoomGame._init_(self, host, PLUGIN_INFO, (NC_RADIOCOL, RADIOC_TAG), 69 RoomGame._init_(self, host, PLUGIN_INFO, (NC_RADIOCOL, RADIOC_TAG),
70 game_init={'queue': [], 'upload': True, 'playing': None, 'playing_time': 0, 'to_delete': {}}) 70 game_init={'queue': [], 'upload': True, 'playing': None, 'playing_time': 0, 'to_delete': {}})
71 self.host = host 71 self.host = host
72 host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom, async=True) 72 host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='asss', out_sign='', method=self._prepareRoom, async=True)
73 host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) 73 host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='sass', out_sign='', method=self._createGame)
74 host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded, async=True) 74 host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded, async=True)
75 host.bridge.addSignal("radiocolPlayers", ".plugin", signature='ssass') # room_jid, referee, players, profile 75 host.bridge.addSignal("radiocolPlayers", ".plugin", signature='ssass') # room_jid, referee, players, profile
76 host.bridge.addSignal("radiocolStarted", ".plugin", signature='ssasais') # room_jid, referee, players, [QUEUE_TO_START, QUEUE_LIMIT], profile 76 host.bridge.addSignal("radiocolStarted", ".plugin", signature='ssasais') # room_jid, referee, players, [QUEUE_TO_START, QUEUE_LIMIT], profile
77 host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss') # room_jid, reason, profile 77 host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss') # room_jid, reason, profile
78 host.bridge.addSignal("radiocolPreload", ".plugin", signature='ssssssss') # room_jid, timestamp, filename, title, artist, album, profile 78 host.bridge.addSignal("radiocolPreload", ".plugin", signature='ssssssss') # room_jid, timestamp, filename, title, artist, album, profile
94 @param referee: JID of the referee in the room (room userhost + '/' + nick) 94 @param referee: JID of the referee in the room (room userhost + '/' + nick)
95 @song_path: absolute path of the song added 95 @song_path: absolute path of the song added
96 @param profile_key: %(doc_profile_key)s 96 @param profile_key: %(doc_profile_key)s
97 @return: a Deferred instance 97 @return: a Deferred instance
98 """ 98 """
99 #XXX: this is a Q&D way for the proof of concept. In the future, the song should 99 # XXX: this is a Q&D way for the proof of concept. In the future, the song should
100 # be streamed to the backend using XMPP file copy 100 # be streamed to the backend using XMPP file copy
101 # Here we cheat because we know we are on the same host, and we don't 101 # Here we cheat because we know we are on the same host, and we don't
102 # check data. Referee will have to parse the song himself to check it 102 # check data. Referee will have to parse the song himself to check it
103 try: 103 try:
104 if song_path.lower().endswith('.mp3'): 104 if song_path.lower().endswith('.mp3'):
113 except ID3NoHeaderError: 113 except ID3NoHeaderError:
114 song = actual_song 114 song = actual_song
115 else: 115 else:
116 song = OggVorbis(song_path) 116 song = OggVorbis(song_path)
117 except (OggVorbisHeaderError, HeaderNotFoundError): 117 except (OggVorbisHeaderError, HeaderNotFoundError):
118 #this file is not ogg vorbis nor mp3, we reject it 118 # this file is not ogg vorbis nor mp3, we reject it
119 self.deleteFile(song_path) # FIXME: same host trick (see note above) 119 self.deleteFile(song_path) # FIXME: same host trick (see note above)
120 return defer.fail(exceptions.DataError(D_("The uploaded file has been rejected, only Ogg Vorbis and MP3 songs are accepted."))) 120 return defer.fail(exceptions.DataError(D_("The uploaded file has been rejected, only Ogg Vorbis and MP3 songs are accepted.")))
121 121
122 attrs = {'filename': os.path.basename(song_path), 122 attrs = {'filename': os.path.basename(song_path),
123 'title': song.get("title", ["Unknown"])[0], 123 'title': song.get("title", ["Unknown"])[0],
124 'artist': song.get("artist", ["Unknown"])[0], 124 'artist': song.get("artist", ["Unknown"])[0],
125 'album': song.get("album", ["Unknown"])[0], 125 'album': song.get("album", ["Unknown"])[0],
126 'length': str(song.info.length) 126 'length': str(song.info.length)
127 } 127 }
128 radio_data = self.games[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure 128 radio_data = self.games[jid.JID(referee).userhostJID()] # FIXME: referee comes from Libervia's client side, it's unsecure
129 radio_data['to_delete'][attrs['filename']] = song_path # FIXME: works only because of the same host trick, see the note under the docstring 129 radio_data['to_delete'][attrs['filename']] = song_path # FIXME: works only because of the same host trick, see the note under the docstring
130 return self.send(jid.JID(referee), ('', 'song_added'), attrs, profile=profile) 130 return self.send(jid.JID(referee), ('', 'song_added'), attrs, profile=profile)
131 131
132 def playNext(self, room_jid, profile): 132 def playNext(self, room_jid, profile):
133 """"Play next song in queue if exists, and put a timer 133 """"Play next song in queue if exists, and put a timer
134 which trigger after the song has been played to play next one""" 134 which trigger after the song has been played to play next one"""
135 #TODO: songs need to be erased once played or found invalids 135 # TODO: songs need to be erased once played or found invalids
136 # ==> unlink done the Q&D way with the same host trick (see above) 136 # ==> unlink done the Q&D way with the same host trick (see above)
137 radio_data = self.games[room_jid.userhost()] 137 radio_data = self.games[room_jid]
138 if len(radio_data['players']) == 0: 138 if len(radio_data['players']) == 0:
139 log.debug(_('No more participants in the radiocol: cleaning data')) 139 log.debug(_('No more participants in the radiocol: cleaning data'))
140 radio_data['queue'] = [] 140 radio_data['queue'] = []
141 for filename in radio_data['to_delete']: 141 for filename in radio_data['to_delete']:
142 self.deleteFile(filename, radio_data) 142 self.deleteFile(filename, radio_data)
143 radio_data['to_delete'] = {} 143 radio_data['to_delete'] = {}
144 queue = radio_data['queue'] 144 queue = radio_data['queue']
145 if not queue: 145 if not queue:
146 #nothing left to play, we need to wait for uploads 146 # nothing left to play, we need to wait for uploads
147 radio_data['playing'] = None 147 radio_data['playing'] = None
148 return 148 return
149 song = queue.pop(0) 149 song = queue.pop(0)
150 filename, length = song['filename'], float(song['length']) 150 filename, length = song['filename'], float(song['length'])
151 self.send(room_jid, ('', 'play'), {'filename': filename}, profile=profile) 151 self.send(room_jid, ('', 'play'), {'filename': filename}, profile=profile)
152 radio_data['playing'] = song 152 radio_data['playing'] = song
153 radio_data['playing_time'] = time.time() 153 radio_data['playing_time'] = time.time()
154 154
155 if not radio_data['upload'] and len(queue) < QUEUE_LIMIT: 155 if not radio_data['upload'] and len(queue) < QUEUE_LIMIT:
156 #upload is blocked and we now have resources to get more, we reactivate it 156 # upload is blocked and we now have resources to get more, we reactivate it
157 self.send(room_jid, ('', 'upload_ok'), profile=profile) 157 self.send(room_jid, ('', 'upload_ok'), profile=profile)
158 radio_data['upload'] = True 158 radio_data['upload'] = True
159 159
160 reactor.callLater(length, self.playNext, room_jid, profile) 160 reactor.callLater(length, self.playNext, room_jid, profile)
161 #we wait more than the song length to delete the file, to manage poorly reactive networks/clients 161 # we wait more than the song length to delete the file, to manage poorly reactive networks/clients
162 reactor.callLater(length + 90, self.deleteFile, filename, radio_data) # FIXME: same host trick (see above) 162 reactor.callLater(length + 90, self.deleteFile, filename, radio_data) # FIXME: same host trick (see above)
163 163
164 def deleteFile(self, filename, radio_data=None): 164 def deleteFile(self, filename, radio_data=None):
165 """ 165 """
166 Delete a previously uploaded file. 166 Delete a previously uploaded file.
183 return False 183 return False
184 return True 184 return True
185 185
186 def room_game_cmd(self, mess_elt, profile): 186 def room_game_cmd(self, mess_elt, profile):
187 from_jid = jid.JID(mess_elt['from']) 187 from_jid = jid.JID(mess_elt['from'])
188 room_jid = jid.JID(from_jid.userhost()) 188 room_jid = from_jid.userhostJID()
189 nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile) 189 nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid, profile)
190 190
191 radio_elt = mess_elt.firstChildElement() 191 radio_elt = mess_elt.firstChildElement()
192 radio_data = self.games[room_jid.userhost()] 192 radio_data = self.games[room_jid]
193 if 'queue' in radio_data: 193 if 'queue' in radio_data:
194 queue = radio_data['queue'] 194 queue = radio_data['queue']
195 195
196 from_referee = self.isReferee(room_jid.userhost(), from_jid.resource) 196 from_referee = self.isReferee(room_jid, from_jid.resource)
197 to_referee = self.isReferee(room_jid.userhost(), jid.JID(mess_elt['to']).user) 197 to_referee = self.isReferee(room_jid, jid.JID(mess_elt['to']).user)
198 is_player = self.isPlayer(room_jid.userhost(), nick) 198 is_player = self.isPlayer(room_jid, nick)
199 for elt in radio_elt.elements(): 199 for elt in radio_elt.elements():
200 if not from_referee and not (to_referee and elt.name == 'song_added'): 200 if not from_referee and not (to_referee and elt.name == 'song_added'):
201 continue # sender must be referee, expect when a song is submitted 201 continue # sender must be referee, expect when a song is submitted
202 if not is_player and (elt.name not in ('started', 'players')): 202 if not is_player and (elt.name not in ('started', 'players')):
203 continue # user is in the room but not playing 203 continue # user is in the room but not playing
217 elif elt.name == 'no_upload': 217 elif elt.name == 'no_upload':
218 self.host.bridge.radiocolNoUpload(room_jid.userhost(), profile) 218 self.host.bridge.radiocolNoUpload(room_jid.userhost(), profile)
219 elif elt.name == 'upload_ok': 219 elif elt.name == 'upload_ok':
220 self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile) 220 self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile)
221 elif elt.name == 'song_added': # a song has been added 221 elif elt.name == 'song_added': # a song has been added
222 #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue. 222 # FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue.
223 # Need to manage some sort of rules to allow peoples to send songs 223 # Need to manage some sort of rules to allow peoples to send songs
224 if len(queue) >= QUEUE_LIMIT: 224 if len(queue) >= QUEUE_LIMIT:
225 #there are already too many songs in queue, we reject this one 225 # there are already too many songs in queue, we reject this one
226 #FIXME: add an error code 226 # FIXME: add an error code
227 self.send(from_jid, ('', 'song_rejected'), {'reason': "Too many songs in queue"}, profile=profile) 227 self.send(from_jid, ('', 'song_rejected'), {'reason': "Too many songs in queue"}, profile=profile)
228 return 228 return
229 229
230 #The song is accepted and added in queue 230 # The song is accepted and added in queue
231 preload_elt = self.__create_preload_elt(from_jid.resource, elt) 231 preload_elt = self.__create_preload_elt(from_jid.resource, elt)
232 queue.append(preload_elt) 232 queue.append(preload_elt)
233 233
234 if len(queue) >= QUEUE_LIMIT: 234 if len(queue) >= QUEUE_LIMIT:
235 #We are at the limit, we refuse new upload until next play 235 # We are at the limit, we refuse new upload until next play
236 self.send(room_jid, ('', 'no_upload'), profile=profile) 236 self.send(room_jid, ('', 'no_upload'), profile=profile)
237 radio_data['upload'] = False 237 radio_data['upload'] = False
238 238
239 self.send(room_jid, preload_elt, profile=profile) 239 self.send(room_jid, preload_elt, profile=profile)
240 if not radio_data['playing'] and len(queue) == QUEUE_TO_START: 240 if not radio_data['playing'] and len(queue) == QUEUE_TO_START:
242 # songs in queue. We can now start the party :) 242 # songs in queue. We can now start the party :)
243 self.playNext(room_jid, profile) 243 self.playNext(room_jid, profile)
244 else: 244 else:
245 log.error(_('Unmanaged game element: %s') % elt.name) 245 log.error(_('Unmanaged game element: %s') % elt.name)
246 246
247 def getSyncDataForPlayer(self, room_jid_s, nick): 247 def getSyncDataForPlayer(self, room_jid, nick):
248 game_data = self.games[room_jid_s] 248 game_data = self.games[room_jid]
249 elements = [] 249 elements = []
250 if game_data['playing']: 250 if game_data['playing']:
251 preload = copy.deepcopy(game_data['playing']) 251 preload = copy.deepcopy(game_data['playing'])
252 current_time = game_data['playing_time'] + 1 if self.testing else time.time() 252 current_time = game_data['playing_time'] + 1 if self.testing else time.time()
253 preload['filename'] += '#t=%.2f' % (current_time - game_data['playing_time']) 253 preload['filename'] += '#t=%.2f' % (current_time - game_data['playing_time'])