Trouve les bons noms :D

This commit is contained in:
Klafyvel 2018-03-29 00:52:50 +02:00
parent c8f00f0a4c
commit b8825bb213
3 changed files with 9 additions and 10 deletions

View file

@ -1,16 +1,16 @@
from urllib.parse import parse_qs
import requests import requests
import json
from django.db import models, migrations, transaction from django.db import models, migrations, transaction
YOUTUBE_INFO_URL = 'http://youtube.com/get_video_info?video_id={}' YOUTUBE_INFO_URL = 'http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v={}&format=json'
def gen_title(apps, schema_editor): def gen_title(apps, schema_editor):
Link = apps.get_model('player', 'Link') Link = apps.get_model('player', 'Link')
for o in Link.objects.all(): for o in Link.objects.all():
response = requests.get(YOUTUBE_INFO_URL.format(o.token)) response = requests.get(YOUTUBE_INFO_URL.format(o.token))
q = parse_qs(response.content.decode('utf-8')) q = json.loads(response.content.decode('utf-8'))
try: try:
o.title = q['title'][0] o.title = q['title']
except KeyError: except KeyError:
o.delete() o.delete()
else: else:

View file

@ -1,11 +1,11 @@
from urllib.parse import parse_qs
import requests import requests
import json
from django.db import models from django.db import models
from django.shortcuts import reverse from django.shortcuts import reverse
PK_LENGTH = 23 PK_LENGTH = 23
YOUTUBE_INFO_URL = 'http://youtube.com/get_video_info?video_id={}' YOUTUBE_INFO_URL = 'http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v={}&format=json'
class Playlist(models.Model): class Playlist(models.Model):
date = models.DateTimeField(verbose_name="date") date = models.DateTimeField(verbose_name="date")
@ -61,5 +61,5 @@ class Link(models.Model):
def update_titles(cls): def update_titles(cls):
for o in cls.objects.all(): for o in cls.objects.all():
response = requests.get(YOUTUBE_INFO_URL.format(o.token)) response = requests.get(YOUTUBE_INFO_URL.format(o.token))
o.title = parse_qs(response.content.decode('utf-8'))['title'][0] o.title = json.loads(response.content.decode('utf-8'))['title']
o.save() o.save()

View file

@ -1,6 +1,5 @@
import json import json
import requests import requests
from urllib.parse import parse_qs
from django.shortcuts import render, get_object_or_404, redirect from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse from django.http import HttpResponse
@ -53,8 +52,8 @@ def add_link(request, token):
yt_token = l.get_token() yt_token = l.get_token()
response = requests.get(YOUTUBE_INFO_URL.format(yt_token)) response = requests.get(YOUTUBE_INFO_URL.format(yt_token))
try: try:
q = parse_qs(response.content.decode('utf-8')) q = json.loads(response.content.decode('utf-8'))
title = q['title'][0] title = q['title']
except KeyError: except KeyError:
title = "Je n'arrive pas à retrouver le titre :(" title = "Je n'arrive pas à retrouver le titre :("
p.last_update = timezone.now() p.last_update = timezone.now()