diff src/plugins/plugin_misc_radiocol.py @ 577:593331663b80

plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
author Goffi <goffi@goffi.org>
date Wed, 09 Jan 2013 21:41:40 +0100
parents ca13633d3b6b
children 952322b1d490
line wrap: on
line diff
--- a/src/plugins/plugin_misc_radiocol.py	Wed Jan 09 01:20:52 2013 +0100
+++ b/src/plugins/plugin_misc_radiocol.py	Wed Jan 09 21:41:40 2013 +0100
@@ -21,20 +21,15 @@
 
 from logging import debug, info, warning, error
 from twisted.words.xish import domish
-from twisted.internet import protocol, defer, threads, reactor
-from twisted.words.protocols.jabber import client, jid, xmlstream
-from twisted.words.protocols.jabber import error as jab_error
-from twisted.words.protocols.jabber.xmlstream import IQ
-import random
+from twisted.internet import reactor
+from twisted.words.protocols.jabber import jid
+
+from wokkel import disco, iwokkel
 
 from zope.interface import implements
 
-from wokkel import disco, iwokkel, data_form
-from sat.tools.xml_tools import dataForm2xml
-from sat.tools.games import TarotCard
-
-from time import time
 import os.path
+from os import unlink
 from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError
 
 try:
@@ -172,9 +167,7 @@
                 error ('Internal error')
                 return
             referee = room_jid.userhost() + '/' + room_nick
-            status = {}
-            occupants_data = {}
-            self.radios[room_jid.userhost()] = {'referee':referee, 'queue':[], 'upload':True, 'playing': False, 'occupants_data':occupants_data}
+            self.radios[room_jid.userhost()] = {'referee':referee, 'queue':[], 'upload':True, 'playing': False, 'occupants_data':{}, 'to_delete':{}}
             mess = self.createRadiocolElt(jid.JID(room_jid.userhost()))
             mess.firstChildElement().addChild(self.__create_started_elt())
             self.host.profiles[profile].xmlstream.send(mess)
@@ -196,6 +189,7 @@
             song = OggVorbis(song_path)
         except OggVorbisHeaderError:
             #this file is not ogg vorbis, we reject it
+            unlink(song_path) # FIXME: same host trick (see note above)
             self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(), \
                                                   "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable", profile)
             """mess = self.createRadiocolElt(jid.JID(referee))
@@ -211,13 +205,15 @@
         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['filename'] = 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
+
+        radio_data = self.radios[jid.JID(referee).userhost()] #FIXME: referee comes from Libervia's client side, it's unsecure
+        radio_data['to_delete'][filename] = song_path #FIXME: works only because of the same host trick, see the note under the docstring  
 
     def playNext(self, room_jid, profile):
         """"Play next sont in queue if exists, and put a timer
@@ -225,6 +221,7 @@
         #TODO: need to check that there are still peoples in the room
         #      and clean the datas/stop the playlist if it's not the case
         #TODO: songs need to be erased once played or found invalids
+        #      ==> unlink done the Q&D way with the same host trick (see above)
         radio_data = self.radios[room_jid.userhost()]
         queue = radio_data['queue']
         if not queue:
@@ -245,8 +242,15 @@
             self.host.profiles[profile].xmlstream.send(mess)
             radio_data['upload'] = True
 
-        print ("Playing next song in %s s" % length)
         reactor.callLater(length, self.playNext, room_jid, profile)
+        try:
+            file_to_delete = radio_data['to_delete'][filename]
+        except KeyError:
+            error(_("INTERNAL ERROR: can't find full path of the song to delete"))
+            return
+        
+        #we wait more than the song length to delete the file, to manage poorly reactive networks/clients  
+        reactor.callLater(length + 90, unlink, file_to_delete) #FIXME: same host trick (see above)
 
 
     def radiocol_game_cmd(self, mess_elt, profile):
@@ -267,7 +271,7 @@
             elif elt.name == 'play':
                 self.host.bridge.radiocolPlay(room_jid.userhost(), elt['filename'], profile)
             elif elt.name == 'song_rejected': #a song has been refused
-                self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(), elt['reason'], profile)
+                self.host.bridge.radiocolSongRejected(room_jid.userhost(), elt['reason'], profile)
             elif elt.name == 'no_upload':
                 self.host.bridge.radiocolNoUpload(room_jid.userhost(), profile)
             elif elt.name == 'upload_ok':