live_share_youtube/player/views.py

94 lines
2.6 KiB
Python
Raw Normal View History

2018-03-28 15:05:18 +00:00
import json
2018-03-28 21:01:47 +00:00
import requests
from urllib.parse import parse_qs
2018-03-24 18:34:01 +00:00
2018-03-25 15:52:52 +00:00
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.core.serializers import serialize
2018-03-28 15:05:18 +00:00
import django.utils.timezone as timezone
2018-03-25 15:52:52 +00:00
2018-03-28 21:01:47 +00:00
from player.models import Playlist, Link, YOUTUBE_INFO_URL
2018-03-25 15:52:52 +00:00
from player.forms import PlaylistForm, LinkForm
2018-03-28 21:01:47 +00:00
2018-03-25 15:52:52 +00:00
def new_playlist(request):
p = PlaylistForm(request.POST or None)
if p.is_valid():
playlist = p.save()
return redirect(playlist.get_absolute_url())
return render(request, 'form.html', {
'form':p,
'validate':'Créer',
'title':'Nouvelle playlist'
})
@csrf_exempt
def get_list(request, token):
p = get_object_or_404(Playlist, pk=Playlist.reverse_token(token))
2018-03-28 15:05:18 +00:00
p.last_get = timezone.now()
p.save()
2018-03-25 15:52:52 +00:00
2018-03-28 15:05:18 +00:00
d = {'tokens':[], 'updated':False}
2018-03-28 21:01:47 +00:00
print(p.last_update.timestamp(),int(request.GET.get('last_sync', 0))/1000)
print(p.last_update.timestamp() >= int(request.GET.get('last_sync', 0))/1000)
2018-03-28 15:05:18 +00:00
2018-03-28 21:01:47 +00:00
if p.last_update.timestamp() >= int(request.GET.get('last_sync', 0))/1000:
2018-03-28 15:05:18 +00:00
d['updated'] = True
2018-03-28 21:01:47 +00:00
d['videos'] = [
{'token':l.token, 'title':l.title} for l in p.link_set.all()
]
2018-03-28 15:05:18 +00:00
return HttpResponse(json.dumps(d), content_type='application/json')
2018-03-25 15:52:52 +00:00
@csrf_exempt
def add_link(request, token):
p = get_object_or_404(Playlist, pk=Playlist.reverse_token(token))
l = LinkForm(request.POST or None)
if l.is_valid():
2018-03-28 21:01:47 +00:00
yt_token = l.get_token()
response = requests.get(YOUTUBE_INFO_URL.format(yt_token))
try:
q = parse_qs(response.content.decode('utf-8'))
title = q['title'][0]
except KeyError:
print(yt_token)
print(q)
return HttpResponse('Error')
2018-03-28 15:05:18 +00:00
p.last_update = timezone.now()
p.save()
2018-03-25 15:52:52 +00:00
link = Link()
link.token = yt_token
2018-03-28 21:01:47 +00:00
link.title = title
2018-03-25 15:52:52 +00:00
link.playlist = p
link.save()
return HttpResponse('Ok')
return render(request, 'form_inline.html', {
'form':l,
'validate':'Ajouter'
})
def remove_link(request, pk):
l = get_object_or_404(Link, pk=pk)
l.delete()
return HttpResponse('Ok')
def playlist(request, token):
p = get_object_or_404(Playlist, pk=Playlist.reverse_token(token))
add_link_form = LinkForm()
return render(request, 'player/playlist.html', {
'playlist':p,
'form':add_link_form
})
def all_playlist(request):
2018-03-28 18:30:37 +00:00
p = Playlist.objects.filter(public=True).order_by('date')
2018-03-25 15:52:52 +00:00
return render(request, 'player/all_list.html', {'lists':p})