diff src/plugins/plugin_misc_radiocol.py @ 455:72522263cbc9

plugin RadioCol: basic functionnality working approximately
author Goffi <goffi@goffi.org>
date Mon, 23 Jan 2012 00:14:33 +0100
parents 5731b038fc7f
children ba6e1feda03e
line wrap: on
line diff
--- a/src/plugins/plugin_misc_radiocol.py	Mon Jan 23 00:13:37 2012 +0100
+++ b/src/plugins/plugin_misc_radiocol.py	Mon Jan 23 00:14:33 2012 +0100
@@ -34,6 +34,8 @@
 from sat.tools.games import TarotCard
 
 from time import time
+import os.path
+from mutagen.oggvorbis import OggVorbis
 
 try:
     from twisted.words.protocols.xmlstream import XMPPHandler
@@ -63,9 +65,12 @@
         info(_("Radio collective initialization"))
         self.host = host
         self.radios={}
-        host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='ass', out_sign='', method=self.radiocolLaunch) #args: occupants, profile
-        host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='ss', out_sign='', method=self.radiocolCreate) #args: room_jid, profile
-        host.bridge.addSignal("radiocolStarted", ".plugin", signature='sss') #args: room_jid, referee, profile
+        host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='ass', out_sign='', method=self.radiocolLaunch)
+        host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='ss', out_sign='', method=self.radiocolCreate)
+        host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded)
+        host.bridge.addSignal("radiocolStarted", ".plugin", signature='sss') #room_jid, referee, profile
+        host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss') #room_jid, reason, profile
+        host.bridge.addSignal("radiocolPreload", ".plugin", signature='ssssss') #room_jid, filename, title, artist, album, profile
         host.trigger.add("MUC user joined", self.userJoinedTrigger)
 
     def createRadiocolElt(self, to_jid, type="normal"):
@@ -75,7 +80,7 @@
         elt["type"] = type
         elt.addElement((NC_RADIOCOL, RADIOC_TAG))
         return elt
-
+    
     def __create_started_elt(self):
         """Create a game_started domish element"""
         started_elt = domish.Element(('','started'))
@@ -159,6 +164,48 @@
             mess.firstChildElement().addChild(self.__create_started_elt())
             self.host.profiles[profile].xmlstream.send(mess)
 
+    def radiocolSongAdded(self, referee, song_path, profile):
+        """This method is called by libervia when a song has been uploaded
+        @param room_jid_param: jid of the room
+        @song_path: absolute path of the song added
+        @param profile_key: %(doc_profile_key)s"""
+        #XXX: this is a Q&D way for the proof of concept. In the future, the song should
+        #     be streamed to the backend using XMPP file copy
+        #     Here we cheat because we know we are on the same host, and we don't
+        #     check data. Referee will have to parse the song himself to check it
+        client = self.host.getClient(profile)
+        if not client:
+            error(_("Can't access profile's data"))
+            return 
+        try:
+            song = OggVorbis(song_path)
+        except OggVorbisHeaderError:
+            #this file is not ogg vorbis, we reject it
+            import pdb
+            pdb.set_trace()
+            """mess = self.createRadiocolElt(jid.JID(referee))
+            reject_elt = mess.firstChildElement().addElement(('','song_rejected'))
+            reject_elt['sender'] = client.jid
+            reject_elt['reason'] = _("Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable")
+            #FIXME: add an error code
+            self.host.profiles[profile].xmlstream.send(mess)"""
+            return
+        title = song.get("title", ["Unknown"])[0]
+        artist = song.get("artist", ["Unknown"])[0]
+        album = song.get("album", ["Unknown"])[0]
+        length = song.info.length
+        mess = self.createRadiocolElt(jid.JID(referee))
+        added_elt = mess.firstChildElement().addElement(('','song_added'))
+        added_elt['filename'] = os.path.basename(song_path)
+        added_elt['title'] = title
+        added_elt['artist'] = artist
+        added_elt['album'] = album
+        added_elt['length'] = str(length)
+        self.host.profiles[profile].xmlstream.send(mess)
+        return
+
+        
+
     def radiocol_game_cmd(self, mess_elt, profile):
         from_jid = jid.JID(mess_elt['from']) 
         room_jid = jid.JID(from_jid.userhost())
@@ -170,6 +217,22 @@
             
             if elt.name == 'started': #new game created
                 self.host.bridge.radiocolStarted(room_jid.userhost(), from_jid.full(), profile)
+            elif elt.name == 'preload': #a song is in queue and must be preloaded
+                self.host.bridge.radiocolPreload(room_jid.userhost(), elt['filename'], elt['title'], elt['artist'], elt['album'], profile)
+            elif elt.name == 'song_rejected': #a song has been refused
+                import pdb
+                pdb.set_trace()
+            elif elt.name == 'song_added': #a song has been refused
+                #FIXME: we are KISS for the PoC: every song is added. Need to manage some sort of rules to allow peoples to send songs
+                mess = self.createRadiocolElt(room_jid)
+                preload_elt = mess.firstChildElement().addElement(('','preload'))
+                preload_elt['sender'] = from_jid.resource
+                preload_elt['filename'] = elt['filename'] #XXX: the frontend should know the temporary directory where file is put
+                preload_elt['title'] = elt['title']
+                preload_elt['artist'] = elt['artist']
+                preload_elt['album'] = elt['album']
+                preload_elt['length'] = elt['length']
+                self.host.profiles[profile].xmlstream.send(mess)
             else:
                 error (_('Unmanaged game element: %s') % elt.name)