From 9791b6973d27606088d4559917b0f870427adc7a Mon Sep 17 00:00:00 2001 From: Klafyvel Date: Wed, 28 Mar 2018 23:01:47 +0200 Subject: [PATCH] Titre des pistes --- player/forms.py | 10 ++++--- player/migrations/0005_title.py | 38 ++++++++++++++++++++++++ player/models.py | 16 ++++++++++ player/templates/player/playlist.html | 42 +++++++++++++-------------- player/views.py | 29 ++++++++++++++---- static_files/js/playlist.js | 8 +++-- 6 files changed, 109 insertions(+), 34 deletions(-) create mode 100644 player/migrations/0005_title.py diff --git a/player/forms.py b/player/forms.py index 21e8c2e..4e3286f 100644 --- a/player/forms.py +++ b/player/forms.py @@ -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] + diff --git a/player/migrations/0005_title.py b/player/migrations/0005_title.py new file mode 100644 index 0000000..2d0eb85 --- /dev/null +++ b/player/migrations/0005_title.py @@ -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"), + ), + ] diff --git a/player/models.py b/player/models.py index c994a81..d8ea557 100644 --- a/player/models.py +++ b/player/models.py @@ -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() diff --git a/player/templates/player/playlist.html b/player/templates/player/playlist.html index 3a865b4..ea4576f 100644 --- a/player/templates/player/playlist.html +++ b/player/templates/player/playlist.html @@ -60,17 +60,17 @@
-
+
- {{link.token}} -
-
-
-
- - - -
+ {{link.title}} +
+
+
+ + + +
+
@@ -82,17 +82,17 @@ diff --git a/player/views.py b/player/views.py index 1eaa15f..146324e 100644 --- a/player/views.py +++ b/player/views.py @@ -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') diff --git a/static_files/js/playlist.js b/static_files/js/playlist.js index 2c190d2..36ffde9 100644 --- a/static_files/js/playlist.js +++ b/static_files/js/playlist.js @@ -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