diff --git a/player/migrations/0005_title.py b/player/migrations/0005_title.py index 2d0eb85..a593602 100644 --- a/player/migrations/0005_title.py +++ b/player/migrations/0005_title.py @@ -1,16 +1,16 @@ -from urllib.parse import parse_qs import requests +import json 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): 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')) + q = json.loads(response.content.decode('utf-8')) try: - o.title = q['title'][0] + o.title = q['title'] except KeyError: o.delete() else: diff --git a/player/models.py b/player/models.py index d8ea557..9750a18 100644 --- a/player/models.py +++ b/player/models.py @@ -1,11 +1,11 @@ -from urllib.parse import parse_qs import requests +import json from django.db import models from django.shortcuts import reverse 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): date = models.DateTimeField(verbose_name="date") @@ -61,5 +61,5 @@ class Link(models.Model): 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.title = json.loads(response.content.decode('utf-8'))['title'] o.save() diff --git a/player/views.py b/player/views.py index c6c94f6..0bd6733 100644 --- a/player/views.py +++ b/player/views.py @@ -1,6 +1,5 @@ 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 @@ -53,8 +52,8 @@ def add_link(request, token): 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] + q = json.loads(response.content.decode('utf-8')) + title = q['title'] except KeyError: title = "Je n'arrive pas à retrouver le titre :(" p.last_update = timezone.now()