Titre des pistes

This commit is contained in:
Klafyvel 2018-03-28 23:01:47 +02:00
parent 01ec7be090
commit 9791b6973d
6 changed files with 109 additions and 34 deletions

View file

@ -1,10 +1,11 @@
from urllib.parse import urlparse
import django.utils.timezone as timezone
from urllib.parse import urlparse, parse_qs
import django.utils.timezone as timezone
from django import forms
from player.models import Playlist, Link
class PlaylistForm(forms.ModelForm):
class Meta:
model = Playlist
@ -21,6 +22,7 @@ class LinkForm(forms.Form):
def get_token(self):
p=urlparse(self.cleaned_data['url'])
print(p.query)
return [i for i in p.query.split('&') if i and i[0]=='v'][0].split('=')[-1]
p = parse_qs(p.query)
return p['v'][0]

View file

@ -0,0 +1,38 @@
from urllib.parse import parse_qs
import requests
from django.db import models, migrations, transaction
YOUTUBE_INFO_URL = 'http://youtube.com/get_video_info?video_id={}'
def gen_title(apps, schema_editor):
Link = apps.get_model('player', 'Link')
for o in Link.objects.all():
response = requests.get(YOUTUBE_INFO_URL.format(o.token))
q = parse_qs(response.content.decode('utf-8'))
try:
o.title = q['title'][0]
except KeyError:
o.delete()
else:
o.save()
class Migration(migrations.Migration):
dependencies = [
('player', '0004_playlist_public'),
]
atomic = False
operations = [
migrations.AddField(
model_name='link',
name='title',
field= models.CharField(null=True, max_length=200, verbose_name="Titre"),
),
migrations.RunPython(gen_title),
migrations.AlterField(
model_name='link',
name='title',
field= models.CharField(max_length=255, verbose_name="Titre"),
),
]

View file

@ -1,7 +1,11 @@
from urllib.parse import parse_qs
import requests
from django.db import models
from django.shortcuts import reverse
PK_LENGTH = 23
YOUTUBE_INFO_URL = 'http://youtube.com/get_video_info?video_id={}'
class Playlist(models.Model):
date = models.DateTimeField(verbose_name="date")
@ -44,6 +48,18 @@ class Link(models.Model):
on_delete=models.CASCADE,
verbose_name="Playlist",
)
title = models.CharField(
max_length=255,
verbose_name="Titre"
)
def __str__(self):
return "Link : " + self.token + " of " + str(self.playlist)
@classmethod
def update_titles(cls):
for o in cls.objects.all():
response = requests.get(YOUTUBE_INFO_URL.format(o.token))
o.title = parse_qs(response.content.decode('utf-8'))['title'][0]
o.save()

View file

@ -60,17 +60,17 @@
<div class="card bg-secondary text-white" onclick="setCurrent(parseInt(this.id));" id="{{ forloop.counter0 }}">
<div class="card-body">
<div class="row container">
<div class="col-md-6" >
<div class="col-md-9" >
<i class="far fa-play-circle"></i>
{{link.token}}
</div>
<div class="col-md-3">
</div>
<div class="col-md-3 btn-group">
<a class="btn btn-outline-light" href="#"><i class="fas fa-thumbs-up"></i></a>
<a class="btn btn-outline-light" href="#"><i class="fas fa-thumbs-down"></i></a>
<a class="btn btn-outline-light" href="#"><i class="fas fa-trash"></i></a>
</div>
{{link.title}}
</div>
<div class="col-md-3">
<div class="btn-group">
<a class="btn btn-outline-light" href="#"><i class="fas fa-thumbs-up"></i></a>
<a class="btn btn-outline-light" href="#"><i class="fas fa-thumbs-down"></i></a>
<a class="btn btn-outline-light" href="#"><i class="fas fa-trash"></i></a>
</div>
</div>
</div>
</div>
</div>
@ -82,17 +82,17 @@
<div style="display:none;" onclick="setCurrent(parseInt(this.id));" class="card bg-secondary text-white" id="link_template">
<div class="card-body">
<div class="row container">
<div class="col-md-6">
<i class="far fa-play-circle"></i>
<span class="link_name"></span>
</div>
<div class="col-md-3">
</div>
<div class="col-md-3 btn-group">
<a class="btn btn-outline-light" href="#"><i class="fas fa-thumbs-up"></i></a>
<a class="btn btn-outline-light" href="#"><i class="fas fa-thumbs-down"></i></a>
<a class="btn btn-outline-light" href="#"><i class="fas fa-trash"></i></a>
</div>
<div class="col-md-9">
<i class="far fa-play-circle"></i>
<span class="link_name"></span>
</div>
<div class="col-md-3">
<div class="btn-group">
<a class="btn btn-outline-light" href="#"><i class="fas fa-thumbs-up"></i></a>
<a class="btn btn-outline-light" href="#"><i class="fas fa-thumbs-down"></i></a>
<a class="btn btn-outline-light" href="#"><i class="fas fa-trash"></i></a>
</div>
</div>
</div>
</div>
</div>

View file

@ -1,4 +1,6 @@
import json
import requests
from urllib.parse import parse_qs
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
@ -6,9 +8,12 @@ from django.views.decorators.csrf import csrf_exempt
from django.core.serializers import serialize
import django.utils.timezone as timezone
from player.models import Playlist, Link
from player.models import Playlist, Link, YOUTUBE_INFO_URL
from player.forms import PlaylistForm, LinkForm
def new_playlist(request):
p = PlaylistForm(request.POST or None)
if p.is_valid():
@ -28,12 +33,15 @@ def get_list(request, token):
p.save()
d = {'tokens':[], 'updated':False}
last_up = p.last_update.timestamp()
last_sync = (int(request.GET['last_sync'])/1000)
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)
if p.last_update.timestamp() >= int(request.GET['last_sync'])/1000:
if p.last_update.timestamp() >= int(request.GET.get('last_sync', 0))/1000:
d['updated'] = True
d['tokens'] = [l.token for l in p.link_set.all()]
d['videos'] = [
{'token':l.token, 'title':l.title} for l in p.link_set.all()
]
return HttpResponse(json.dumps(d), content_type='application/json')
@ -42,11 +50,20 @@ 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():
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')
p.last_update = timezone.now()
p.save()
yt_token = l.get_token()
link = Link()
link.token = yt_token
link.title = title
link.playlist = p
link.save()
return HttpResponse('Ok')

View file

@ -88,14 +88,16 @@ function updateLinks(data) {
var model = document.getElementById('link_template');
var rerun = (current_link >= tracks.length ) || current_link < 0;
tracks = [];
for (var i=0; i<data.tokens.length; i++)
for (var i=0; i<data.videos.length; i++)
{
var token = data.tokens[i];
var token = data.videos[i].token;
var title = data.videos[i].title;
console.debug(title);
tracks.push(token);
var card = model.cloneNode(true);
card.style.display = 'block';
card.id = i.toString();
card.getElementsByClassName('link_name')[0].innerHTML = token;
card.getElementsByClassName('link_name')[0].innerHTML = title;
if (i==current_link) {
card.classList.add('bg-success');
card.classList.remove('bg-secondary');