From 6f49ff57c1b3bba1ea2855783bdb2c42cc07fc99 Mon Sep 17 00:00:00 2001 From: William Date: Fri, 4 Sep 2015 22:27:09 +0000 Subject: [PATCH] Fix youtube get_info --- opps/multimedias/mediaapi.py | 41 ++++++++++++++++++------------------ requirements.txt | 3 +++ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/opps/multimedias/mediaapi.py b/opps/multimedias/mediaapi.py index 7560c6f..2c6fa08 100644 --- a/opps/multimedias/mediaapi.py +++ b/opps/multimedias/mediaapi.py @@ -9,6 +9,7 @@ import pytz import gdata.youtube.service import vimeo +import pafy from gdata.service import BadAuthentication, RequestError from django.utils import timezone @@ -381,15 +382,9 @@ def upload(self, type, media_path, title, description, tags): video_entry = gdata.youtube.YouTubeVideoEntry(media=my_media_group) video_entry = self.yt_service.InsertVideoEntry(video_entry, media_path) - return self._get_info(video_entry) - - def _get_video_status(self, video_entry): - status = self.yt_service.CheckUploadStatus(video_entry) - - if status is None: - return 'ok', None - - return 'error', status[1] + return { + 'id': video_entry.id.text.split('/')[-1] + } def _get_video_embed(self, video_id): return render_to_string('multimedias/youtube/video_embed.html', @@ -398,30 +393,34 @@ def _get_video_embed(self, video_id): def _get_info(self, video_entry): result = {} if video_entry: - video_id = video_entry.id.text.split('/')[-1] + video_id = video_entry.videoid + hour, minute, second = video_entry.duration.split(':') result.update({ u'id': video_id, - u'title': video_entry.media.title.text, - u'description': video_entry.media.description.text, - u'thumbnail': video_entry.media.thumbnail[-1].url, - u'tags': video_entry.media.keywords.text, + u'title': video_entry.title, + u'description': video_entry.description, + u'duration': time( + hour=int(hour), + minute=int(minute), + second=int(second) + ), + u'thumbnail': video_entry.thumb, + u'tags': video_entry.keywords, u'embed': self._get_video_embed(video_id), - u'url': video_entry.media.player.url, + u'url': video_entry.watchv_url, + u'status': u'ok', + u'status_msg': u'ok' }) - (result['status'], - result['status_msg']) = self._get_video_status(video_entry) - return result def get_info(self, video_id): self.authenticate() result = super(Youtube, self).get_info(video_id) result['id'] = video_id - try: - video_entry = self.yt_service.GetYouTubeVideoEntry( - video_id=video_id + video_entry = pafy.new( + 'https://www.youtube.com/watch?v={}'.format(video_id) ) except RequestError as reqerr: if reqerr.args[0].get('reason') == u'Not Found': diff --git a/requirements.txt b/requirements.txt index f7211bb..ea70689 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,6 @@ Django>=1.5,<1.7 opps>=0.2.9 django-celery PyVimeo==0.2.0 +-e git+https://github.com/rg3/youtube-dl.git#egg=youtube-dl +-e git+https://github.com/mps-youtube/pafy.git#egg=pafy +