Commit 90bc6569 authored by Valentin Samir's avatar Valentin Samir

[tv] Génération de playlist pour la tv

parent d9bdc2ed
from django.conf.urls import patterns as patterns from django.conf.urls import patterns, url
import views import views
urlpatterns = patterns('', urlpatterns = patterns('',
('^$', views.tv), url('^$', views.tv, name="index"),
('^auth$', views.icecast_auth), url('^auth$', views.icecast_auth, name="icecast_auth"),
url('^radio/(?P<radio>.+).m3u$', views.radio_playlist, name="radio_playlist"),
url('^(?P<tv>.+).m3u$', views.tv_playlist, name="tv_playlist"),
) )
...@@ -14,12 +14,14 @@ import tv.radio.config as tv_config ...@@ -14,12 +14,14 @@ import tv.radio.config as tv_config
from django.conf import settings from django.conf import settings
import django.shortcuts import django.shortcuts
from django.shortcuts import redirect
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, HttpResponseForbidden from django.http import HttpResponse, HttpResponseForbidden, HttpResponseNotFound
#from django.contrib.auth.decorators import login_required #from django.contrib.auth.decorators import login_required
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.template import RequestContext from django.template import RequestContext
from django.core.urlresolvers import reverse
from decorators import request_passes_test from decorators import request_passes_test
...@@ -57,6 +59,10 @@ def chaines(): ...@@ -57,6 +59,10 @@ def chaines():
except: except:
return {} return {}
def check_local(request):
ip=ipaddr.IPAddress(get_client_ip(request))
return True in [ip in net for net in net_crans]
def check_local_or_logged(request): def check_local_or_logged(request):
ip=ipaddr.IPAddress(get_client_ip(request)) ip=ipaddr.IPAddress(get_client_ip(request))
return True in [ip in net for net in net_crans] or request.user.is_authenticated() return True in [ip in net for net in net_crans] or request.user.is_authenticated()
...@@ -69,8 +75,18 @@ def tv(request): ...@@ -69,8 +75,18 @@ def tv(request):
tnt=[] tnt=[]
sat=[] sat=[]
else: else:
tnt=[(name, "udp://@%s:1234" % tv_dns.idn(name, fqdn=True, charset=None), image_url_for_channel(ip)) for (name, ip) in chaines().get('TNT', {}).items()] tnt=[
sat=[(name, "udp://@%s:1234" % tv_dns.idn(name, fqdn=True, charset=None), image_url_for_channel(ip)) for (name, ip) in chaines().get('SAT', {}).items()] (name,
"udp://@%s:1234" % tv_dns.idn(name, fqdn=True, charset=None),
image_url_for_channel(ip),
reverse("tv:tv_playlist", kwargs={'tv': name})
) for (name, ip) in chaines().get('TNT', {}).items()]
sat=[
(name,
"udp://@%s:1234" % tv_dns.idn(name, fqdn=True, charset=None),
image_url_for_channel(ip),
reverse("tv:tv_playlist", kwargs={'tv': name})
) for (name, ip) in chaines().get('SAT', {}).items()]
# radio=[(name, "udp://@%s:1234" % tv_dns.idn(name, fqdn=True, charset=None), image_url_for_channel(ip)) for (name, ip) in chaines().get('Radio', {}).items()] # radio=[(name, "udp://@%s:1234" % tv_dns.idn(name, fqdn=True, charset=None), image_url_for_channel(ip)) for (name, ip) in chaines().get('Radio', {}).items()]
radio = [] radio = []
user_auth = request.user.is_authenticated() user_auth = request.user.is_authenticated()
...@@ -81,8 +97,9 @@ def tv(request): ...@@ -81,8 +97,9 @@ def tv(request):
userpass = "" userpass = ""
radio.append( radio.append(
(name, (name,
"http://%stv.crans.org:8000/%s" % (userpass, params[0]), "http://%stv.crans.org/%s" % (userpass, params[0]),
image_url_for_channel(params[1])) image_url_for_channel(params[1]),
reverse("tv:radio_playlist", kwargs={'radio': params[0]})),
) )
key = lambda s: s[0].lower() key = lambda s: s[0].lower()
tnt.sort(key=key) tnt.sort(key=key)
...@@ -91,6 +108,23 @@ def tv(request): ...@@ -91,6 +108,23 @@ def tv(request):
params={ 'tnt' : tnt, 'sat': sat, 'radio': radio, 'user': request.user } params={ 'tnt' : tnt, 'sat': sat, 'radio': radio, 'user': request.user }
return django.shortcuts.render_to_response("tv/index.html", params, context_instance=RequestContext(request)) return django.shortcuts.render_to_response("tv/index.html", params, context_instance=RequestContext(request))
@request_passes_test(check_local_or_logged)
def radio_playlist(request, radio):
if radio in tv_config.multicast_tag["Radio"]:
if request.user.is_authenticated():
userpass = "%s:%s@" % (request.user.username, radio_password(request.user, radio))
else:
userpass = ""
return HttpResponse("http://%stv.crans.org/%s\n" % (userpass, radio), content_type="audio/x-mpegurl")
else:
return redirect("tv:index")
def tv_playlist(request, tv):
if check_local(request):
return HttpResponse("udp://@%s:1234\n" % tv_dns.idn(tv, fqdn=True, charset=None), content_type="audio/x-mpegurl")
else:
return redirect("tv:index")
def radio_password(user, radio=""): def radio_password(user, radio=""):
if radio.startswith("/"): if radio.startswith("/"):
radio = radio[1:] radio = radio[1:]
...@@ -109,6 +143,12 @@ def icecast_auth(request): ...@@ -109,6 +143,12 @@ def icecast_auth(request):
return HttpResponseForbidden() return HttpResponseForbidden()
if POST["action"] == "listener_add": if POST["action"] == "listener_add":
ip=ipaddr.IPAddress(POST["ip"]) ip=ipaddr.IPAddress(POST["ip"])
if str(ip) == "127.0.0.1":
if POST.get('ClientHeader.x-auth', '') != u'4b4cdb4fb7556214dea60b639845d2d5':
# untrust proxy
return HttpResponseForbidden()
else:
ip = ipaddr.IPAddress(POST['ClientHeader.x-real-ip'])
# Si ip crans on accepte # Si ip crans on accepte
if True in [ip in net for net in net_crans]: if True in [ip in net for net in net_crans]:
return response return response
......
...@@ -85,14 +85,15 @@ function add(url, vlc){ ...@@ -85,14 +85,15 @@ function add(url, vlc){
}catch(e){} }catch(e){}
} }
function play(url){ function play(url){
if(!test_vlc_plugin()){
return false;
}
if(url==playing){ if(url==playing){
return false; return false;
} else { } else {
playing = url; playing = url;
} }
if(!test_vlc_plugin()){
playing = false;
return false;
}
var vlc = document.getElementById("vlc"); var vlc = document.getElementById("vlc");
document.getElementById('player_div').style.visibility="inherit"; document.getElementById('player_div').style.visibility="inherit";
add(url, vlc); add(url, vlc);
...@@ -125,7 +126,7 @@ function warn_vlc_plugin(){ ...@@ -125,7 +126,7 @@ function warn_vlc_plugin(){
li.appendChild(document.createTextNode('Le ')); li.appendChild(document.createTextNode('Le '));
li.appendChild(a); li.appendChild(a);
li.appendChild(document.createTextNode(' est nécessaire pour regarder la télévision ou écouter la radio.')); li.appendChild(document.createTextNode(' est nécessaire pour regarder la télévision ou écouter la radio.'));
li.setAttribute("class","warning"); li.setAttribute("class","error");
li.setAttribute("id","warning_vlc_plugin"); li.setAttribute("id","warning_vlc_plugin");
ul.appendChild(li); ul.appendChild(li);
...@@ -137,15 +138,16 @@ function unwarn_vlc_plugin(){ ...@@ -137,15 +138,16 @@ function unwarn_vlc_plugin(){
} }
} }
function play_radio(url){ function play_radio(url){
if(!test_vlc_plugin()){
return false;
}
document.getElementById('player_div').style.visibility="hidden"; document.getElementById('player_div').style.visibility="hidden";
if(url==playing){ if(url==playing){
return false; return false;
} else { } else {
playing = url; playing = url;
} }
if(!test_vlc_plugin()){
playing = false;
return false;
}
var vlc = document.getElementById("vlc"); var vlc = document.getElementById("vlc");
add(url, vlc); add(url, vlc);
volume(document.getElementById("volume_slide").value); volume(document.getElementById("volume_slide").value);
...@@ -198,8 +200,8 @@ function init(){ ...@@ -198,8 +200,8 @@ function init(){
<input id="volume_slide" type="range" min="0" max="200" onclick="volume(this.value)" onchange="volume(this.value)" /><span id="range">NaN%</span> <input id="volume_slide" type="range" min="0" max="200" onclick="volume(this.value)" onchange="volume(this.value)" /><span id="range">NaN%</span>
{% if tnt %} {% if tnt %}
<ul> <ul>
{% for key, url, img in tnt %} {% for key, url, img, pls in tnt %}
<li class="tv"><a href="{{url}}" onclick="return play('{{url}}')"> <li class="tv"><a href="{{pls}}" onclick="return play('{{url}}')">
<div class="image_tv"> <div class="image_tv">
<img src="{{img}}"/> <img src="{{img}}"/>
<span class="image_tv" >{{key}}</span> <span class="image_tv" >{{key}}</span>
...@@ -219,12 +221,8 @@ function init(){ ...@@ -219,12 +221,8 @@ function init(){
</ul> </ul>
{% endif %} {% endif %}
</ul><br clear="all"/><ul> </ul><br clear="all"/><ul>
{% for key, url, img in radio %} {% for key, url, img, pls in radio %}
{% if user.is_authenticated %} <li class="tv"><a href="{{pls}}" onclick="return play_radio('{{url}}')">
<li class="tv"><a href="{{url}}" onclick="return play_radio('{{url}}')">
{% else %}
<li class="tv"><a href="{{url}}.m3u" onclick="return play_radio('{{url}}')">
{% endif %}
<div class="image_tv"> <div class="image_tv">
<img src="{{img}}"/> <img src="{{img}}"/>
<span class="image_tv" >{{key}}</span> <span class="image_tv" >{{key}}</span>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment