annotate src/plugins/plugin_misc_radiocol.py @ 705:6c8a119dcc94

plugin text syntaxes: clean_xhtml now accept lxml's HtmlElement to avoid parsing two times the same xml
author Goffi <goffi@goffi.org>
date Thu, 14 Nov 2013 17:53:47 +0100
parents 75e4f5e2cc65
children f610864eb7a5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
4 # SAT plugin for managing Radiocol
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org)
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
10 # (at your option) any later version.
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
15 # GNU Affero General Public License for more details.
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from logging import debug, info, warning, error
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.words.xish import domish
577
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
22 from twisted.internet import reactor
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
23 from twisted.words.protocols.jabber import jid
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
24
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
25 from wokkel import disco, iwokkel
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from zope.interface import implements
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
29 import os.path
577
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
30 from os import unlink
464
794f60e2da27 plugin radiocol: fixed missing import
Goffi <goffi@goffi.org>
parents: 459
diff changeset
31 from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
32 from sat.tools.plugins.games import RoomGame
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 try:
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from twisted.words.protocols.xmlstream import XMPPHandler
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 except ImportError:
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from wokkel.subprotocols import XMPPHandler
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 MESSAGE = '/message'
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 NC_RADIOCOL = 'http://www.goffi.org/protocol/radiocol'
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 RADIOC_TAG = 'radiocol'
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 RADIOC_REQUEST = MESSAGE + '/' + RADIOC_TAG + '[@xmlns="' + NC_RADIOCOL + '"]'
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 PLUGIN_INFO = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
44 "name": "Radio collective plugin",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
45 "import_name": "Radiocol",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
46 "type": "Exp",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
47 "protocols": [],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
48 "dependencies": ["XEP-0045", "XEP-0249"],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
49 "main": "Radiocol",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
50 "handler": "yes",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
51 "description": _("""Implementation of radio collective""")
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 }
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
54 QUEUE_LIMIT = 2
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
55
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
57 class Radiocol(RoomGame):
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def __init__(self, host):
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 info(_("Radio collective initialization"))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
61 RoomGame.__init__(self, host, PLUGIN_INFO, (NC_RADIOCOL, RADIOC_TAG),
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
62 options={'queue': [], 'upload': True, 'playing': False, 'to_delete': {}})
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 self.host = host
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
64 host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='ass', out_sign='', method=self.prepareRoom)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
65 host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='ss', out_sign='', method=self.createCollectiveGame)
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
66 host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
67 host.bridge.addSignal("radiocolStarted", ".plugin", signature='sss') # room_jid, referee, profile
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
68 host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss') # room_jid, reason, profile
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
69 host.bridge.addSignal("radiocolPreload", ".plugin", signature='ssssss') # room_jid, filename, title, artist, album, profile
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
70 host.bridge.addSignal("radiocolPlay", ".plugin", signature='sss') # room_jid, filename, profile
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
71 host.bridge.addSignal("radiocolNoUpload", ".plugin", signature='ss') # room_jid, profile
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
72 host.bridge.addSignal("radiocolUploadOk", ".plugin", signature='ss') # room_jid, profile
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 host.trigger.add("MUC user joined", self.userJoinedTrigger)
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
75 def __create_preload_elt(self, sender, filename, title, artist, album):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
76 preload_elt = domish.Element((None, 'preload'))
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
77 preload_elt['sender'] = sender
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
78 preload_elt['filename'] = filename # XXX: the frontend should know the temporary directory where file is put
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
79 preload_elt['title'] = title
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
80 preload_elt['artist'] = artist
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
81 preload_elt['album'] = album
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
82 return preload_elt
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
83
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
84 def radiocolSongAdded(self, referee, song_path, profile):
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
85 """This method is called by libervia when a song has been uploaded
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
86 @param room_jid_param: jid of the room
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
87 @song_path: absolute path of the song added
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
88 @param profile_key: %(doc_profile_key)s"""
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
89 #XXX: this is a Q&D way for the proof of concept. In the future, the song should
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
90 # be streamed to the backend using XMPP file copy
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
91 # Here we cheat because we know we are on the same host, and we don't
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
92 # check data. Referee will have to parse the song himself to check it
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
93 client = self.host.getClient(profile)
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
94 if not client:
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
95 error(_("Can't access profile's data"))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 577
diff changeset
96 return
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
97 try:
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
98 song = OggVorbis(song_path)
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
99 except OggVorbisHeaderError:
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
100 #this file is not ogg vorbis, we reject it
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
101 unlink(song_path) # FIXME: same host trick (see note above)
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
102 self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(),
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
103 "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable", profile)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
104 """mess = self.createGameElt(jid.JID(referee))
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
105 reject_elt = mess.firstChildElement().addElement(('','song_rejected'))
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
106 reject_elt['sender'] = client.jid
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
107 reject_elt['reason'] = "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable"
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
108 #FIXME: add an error code
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
109 self.host.profiles[profile].xmlstream.send(mess)"""
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
110 return
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
111 title = song.get("title", ["Unknown"])[0]
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
112 artist = song.get("artist", ["Unknown"])[0]
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
113 album = song.get("album", ["Unknown"])[0]
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
114 length = song.info.length
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
115 mess = self.createGameElt(jid.JID(referee))
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
116 added_elt = mess.firstChildElement().addElement(('', 'song_added'))
577
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
117 added_elt['filename'] = filename = os.path.basename(song_path)
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
118 added_elt['title'] = title
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
119 added_elt['artist'] = artist
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
120 added_elt['album'] = album
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
121 added_elt['length'] = str(length)
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
122 self.host.profiles[profile].xmlstream.send(mess)
577
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
123
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
124 radio_data = self.games[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
125 radio_data['to_delete'][filename] = song_path # FIXME: works only because of the same host trick, see the note under the docstring
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
126
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
127 def playNext(self, room_jid, profile):
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
128 """"Play next sont in queue if exists, and put a timer
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
129 which trigger after the song has been played to play next one"""
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
130 #TODO: need to check that there are still peoples in the room
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
131 # and clean the datas/stop the playlist if it's not the case
464
794f60e2da27 plugin radiocol: fixed missing import
Goffi <goffi@goffi.org>
parents: 459
diff changeset
132 #TODO: songs need to be erased once played or found invalids
577
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
133 # ==> unlink done the Q&D way with the same host trick (see above)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
134 radio_data = self.games[room_jid.userhost()]
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
135 queue = radio_data['queue']
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
136 if not queue:
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
137 #nothing left to play, we need to wait for uploads
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
138 radio_data['playing'] = False
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
139 return
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
140
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
141 filename, length = queue.pop(0)
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
142 mess = self.createGameElt(room_jid)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
143 play_elt = mess.firstChildElement().addElement(('', 'play'))
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
144 play_elt['filename'] = filename
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
145 self.host.profiles[profile].xmlstream.send(mess)
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
146
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
147 if not radio_data['upload'] and len(queue) < QUEUE_LIMIT:
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
148 #upload is blocked and we now have resources to get more, we reactivate it
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
149 mess = self.createGameElt(room_jid)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
150 no_upload_elt = mess.firstChildElement().addElement(('', 'upload_ok'))
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
151 self.host.profiles[profile].xmlstream.send(mess)
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
152 radio_data['upload'] = True
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
153
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
154 reactor.callLater(length, self.playNext, room_jid, profile)
577
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
155 try:
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
156 file_to_delete = radio_data['to_delete'][filename]
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
157 except KeyError:
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
158 error(_("INTERNAL ERROR: can't find full path of the song to delete"))
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
159 return
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 577
diff changeset
160
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 577
diff changeset
161 #we wait more than the song length to delete the file, to manage poorly reactive networks/clients
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
162 reactor.callLater(length + 90, unlink, file_to_delete) # FIXME: same host trick (see above)
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
163
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
164 def radiocol_game_cmd(self, mess_elt, profile):
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
165 #FIXME: we should check sender (is it referee ?) here before accepting commands
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 577
diff changeset
166 from_jid = jid.JID(mess_elt['from'])
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
167 room_jid = jid.JID(from_jid.userhost())
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 radio_elt = mess_elt.firstChildElement()
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
169 radio_data = self.games[room_jid.userhost()]
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
170 queue = radio_data['queue']
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 577
diff changeset
171
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172 for elt in radio_elt.elements():
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 577
diff changeset
173
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
174 if elt.name == 'started': # new game created
453
5731b038fc7f plugin radiocol: removed occupants list in radiocolStarted signal
Goffi <goffi@goffi.org>
parents: 450
diff changeset
175 self.host.bridge.radiocolStarted(room_jid.userhost(), from_jid.full(), profile)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
176 elif elt.name == 'preload': # a song is in queue and must be preloaded
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
177 self.host.bridge.radiocolPreload(room_jid.userhost(), elt['filename'], elt['title'], elt['artist'], elt['album'], profile)
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
178 elif elt.name == 'play':
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
179 self.host.bridge.radiocolPlay(room_jid.userhost(), elt['filename'], profile)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
180 elif elt.name == 'song_rejected': # a song has been refused
577
593331663b80 plugin radiocol: songs are deleted once played (Q&D need to be reworked once file are properly transfered to server)
Goffi <goffi@goffi.org>
parents: 572
diff changeset
181 self.host.bridge.radiocolSongRejected(room_jid.userhost(), elt['reason'], profile)
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
182 elif elt.name == 'no_upload':
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
183 self.host.bridge.radiocolNoUpload(room_jid.userhost(), profile)
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
184 elif elt.name == 'upload_ok':
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
185 self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
186 elif elt.name == 'song_added': # a song has been added
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
187 #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue.
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
188 # Need to manage some sort of rules to allow peoples to send songs
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 577
diff changeset
189
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
190 if len(queue) >= QUEUE_LIMIT:
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
191 #there are already too many songs in queue, we reject this one
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
192 mess = self.createGameElt(room_jid)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
193 reject_elt = mess.firstChildElement().addElement(('', 'song_rejected'))
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
194 reject_elt['sender'] = from_jid.resource
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
195 reject_elt['reason'] = "Too many songs in queue"
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
196 #FIXME: add an error code
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
197 self.host.profiles[profile].xmlstream.send(mess)
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
198 return
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 577
diff changeset
199
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
200 #The song is accepted and added in queue
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
201 queue.append((elt['filename'], float(elt['length'])))
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
202
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
203 if len(queue) >= QUEUE_LIMIT:
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
204 #We are at the limit, we refuse new upload until next play
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
205 mess = self.createGameElt(room_jid)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
206 no_upload_elt = mess.firstChildElement().addElement(('', 'no_upload'))
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
207 #FIXME: add an error code
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
208 self.host.profiles[profile].xmlstream.send(mess)
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
209 radio_data['upload'] = False
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
210
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 609
diff changeset
211 mess = self.createGameElt(room_jid)
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
212 preload_elt = self.__create_preload_elt(from_jid.resource,
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
213 elt['filename'],
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
214 elt['title'],
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
215 elt['artist'],
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
216 elt['album'])
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
217 mess.firstChildElement().addChild(preload_elt)
455
72522263cbc9 plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents: 453
diff changeset
218 self.host.profiles[profile].xmlstream.send(mess)
457
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
219 if not radio_data['playing'] and len(queue) == 2:
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
220 #we have not started playing yet, and we have 2 songs in queue
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
221 #we can now start the party :)
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
222 radio_data['playing'] = True
fbe7c9118ce4 plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents: 456
diff changeset
223 self.playNext(room_jid, profile)
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
224 else:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
225 error(_('Unmanaged game element: %s') % elt.name)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 577
diff changeset
226
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
227 def getHandler(self, profile):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
228 return RadiocolHandler(self)
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
229
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
230
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
231 class RadiocolHandler (XMPPHandler):
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
232 implements(iwokkel.IDisco)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 577
diff changeset
233
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
234 def __init__(self, plugin_parent):
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
235 self.plugin_parent = plugin_parent
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
236 self.host = plugin_parent.host
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
237
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
238 def connectionInitialized(self):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
239 self.xmlstream.addObserver(RADIOC_REQUEST, self.plugin_parent.radiocol_game_cmd, profile=self.parent.profile)
450
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
240
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
241 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
242 return [disco.DiscoFeature(NC_RADIOCOL)]
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
243
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
244 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
afe9cfd2ddbb plugins: radio collective first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
245 return []