From afe4a8c7699e70ecaa7f273b55a038b4d8a1050f Mon Sep 17 00:00:00 2001 From: FireDart Date: Wed, 15 Apr 2015 22:17:45 -0400 Subject: [PATCH 1/5] [gfycat] Add new extractor --- youtube_dl/extractor/__init__.py | 1 + youtube_dl/extractor/gfycat.py | 107 +++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 youtube_dl/extractor/gfycat.py diff --git a/youtube_dl/extractor/__init__.py b/youtube_dl/extractor/__init__.py index bbf3be41d..d32f1cbd2 100644 --- a/youtube_dl/extractor/__init__.py +++ b/youtube_dl/extractor/__init__.py @@ -185,6 +185,7 @@ from .gazeta import GazetaIE from .gdcvault import GDCVaultIE from .generic import GenericIE +from .gfycat import GfycatIE from .giantbomb import GiantBombIE from .giga import GigaIE from .glide import GlideIE diff --git a/youtube_dl/extractor/gfycat.py b/youtube_dl/extractor/gfycat.py new file mode 100644 index 000000000..d103693cd --- /dev/null +++ b/youtube_dl/extractor/gfycat.py @@ -0,0 +1,107 @@ +# coding: utf-8 + +from __future__ import unicode_literals + +import datetime + +from .common import InfoExtractor + +class GfycatIE(InfoExtractor): + _VALID_URL = r'https?://(?:www\.)?gfycat\.com/(?P[^/?#]+)' + _TESTS = [{ + 'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher', + 'info_dict': { + 'id': 'DeadlyDecisiveGermanpinscher', + 'title': 'Ghost in the Shell', + 'ext': 'mp4', + 'upload_date': '20140913' + } + },{ + 'url': 'http://gfycat.com/pleasinghilariouskusimanse', + 'info_dict': { + 'id': 'pleasinghilariouskusimanse', + 'title': 'PleasingHilariousKusimanse', + 'ext': 'webm', + 'upload_date': '20150412' + } + },{ + 'url': 'http://gfycat.com/requiredunkemptbuzzard', + 'info_dict': { + 'id': 'requiredunkemptbuzzard', + 'title': 'Headshot!', + 'ext': 'gif', + 'upload_date': '20150130' + } + }] + + def _real_extract(self, url): + video_id = self._match_id(url) + json = self._download_json("http://gfycat.com/cajax/get/" + video_id, video_id, 'Downloading video info')['gfyItem'] + + # Title + # Use user title first, else fallback to url formated name + if json['title']: + video_title = json['title'] + else: + video_title = json['gfyName'] + + # Formats + # Pref: mp4, webm, gif + formats = [{ + 'format_id': 'mp4', + 'ext': 'mp4', + 'url': json['mp4Url'], + 'width': json['width'], + 'height': json['height'], + 'fps': json['frameRate'], + 'filesize': json['mp4Size'], + 'preference': '-1' + }, { + 'format_id': 'webm', + 'ext': 'webm', + 'url': json['webmUrl'], + 'width': json['width'], + 'height': json['height'], + 'fps': json['frameRate'], + 'filesize': json['webmSize'], + 'preference': 0 + }, { + 'format_id': 'gif', + 'ext': 'gif', + 'url': json['gifUrl'], + 'width': json['width'], + 'height': json['height'], + 'fps': json['frameRate'], + 'filesize': json['gifSize'], + 'preference': 1 + }] + + self._sort_formats(formats) + + # Date + date = datetime.datetime.fromtimestamp( + int(json['createDate']) + ).strftime('%Y%m%d') + + # Length + duration = json['numFrames'] / json['frameRate'] + + # Age limit + # 1 = nsfw / 0 = sfw + if json['nsfw'] == 1: + age_limit = 18 + else: + age_limit = 0 + + return { + 'id': video_id, + 'title': video_title, + 'formats': formats, + 'creator': json['userName'], + 'description': json['description'], + 'upload_date': date, + 'categories': json['tags'], + 'age_limit': age_limit, + 'duration': duration, + 'view_count': json['views'] + } From 4aec95f3c932ee7042ca4dcae9fcd8c57341bb55 Mon Sep 17 00:00:00 2001 From: FireDart Date: Thu, 16 Apr 2015 18:10:53 -0400 Subject: [PATCH 2/5] [gfycat] Updated tests. --- youtube_dl/extractor/gfycat.py | 58 +++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/youtube_dl/extractor/gfycat.py b/youtube_dl/extractor/gfycat.py index d103693cd..6de78c49d 100644 --- a/youtube_dl/extractor/gfycat.py +++ b/youtube_dl/extractor/gfycat.py @@ -8,31 +8,39 @@ class GfycatIE(InfoExtractor): _VALID_URL = r'https?://(?:www\.)?gfycat\.com/(?P[^/?#]+)' - _TESTS = [{ - 'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher', - 'info_dict': { - 'id': 'DeadlyDecisiveGermanpinscher', - 'title': 'Ghost in the Shell', - 'ext': 'mp4', - 'upload_date': '20140913' - } - },{ - 'url': 'http://gfycat.com/pleasinghilariouskusimanse', - 'info_dict': { - 'id': 'pleasinghilariouskusimanse', - 'title': 'PleasingHilariousKusimanse', - 'ext': 'webm', - 'upload_date': '20150412' - } - },{ - 'url': 'http://gfycat.com/requiredunkemptbuzzard', - 'info_dict': { - 'id': 'requiredunkemptbuzzard', - 'title': 'Headshot!', - 'ext': 'gif', - 'upload_date': '20150130' - } - }] + _TESTS = [ + { + 'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher', + 'info_dict': { + 'id': 'DeadlyDecisiveGermanpinscher', + 'title': 'Ghost in the Shell', + 'ext': 'mp4', + 'upload_date': '20140913' + } + },{ + 'url': 'http://gfycat.com/pleasinghilariouskusimanse', + 'info_dict': { + 'id': 'pleasinghilariouskusimanse', + 'title': 'PleasingHilariousKusimanse', + 'ext': 'webm', + 'upload_date': '20150412' + }, + 'params': { + 'format': 'webm', + }, + },{ + 'url': 'http://gfycat.com/requiredunkemptbuzzard', + 'info_dict': { + 'id': 'requiredunkemptbuzzard', + 'title': 'Headshot!', + 'ext': 'gif', + 'upload_date': '20150129' + }, + 'params': { + 'format': 'gif', + }, + }, + ] def _real_extract(self, url): video_id = self._match_id(url) From 954352c4c08dab0dd2d9ca20f5a414a307cea96f Mon Sep 17 00:00:00 2001 From: FireDart Date: Thu, 16 Apr 2015 18:11:30 -0400 Subject: [PATCH 3/5] [gfycat] Fixed preferences. --- youtube_dl/extractor/gfycat.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/youtube_dl/extractor/gfycat.py b/youtube_dl/extractor/gfycat.py index 6de78c49d..5e70ed3f6 100644 --- a/youtube_dl/extractor/gfycat.py +++ b/youtube_dl/extractor/gfycat.py @@ -63,7 +63,7 @@ def _real_extract(self, url): 'height': json['height'], 'fps': json['frameRate'], 'filesize': json['mp4Size'], - 'preference': '-1' + 'preference': 2 }, { 'format_id': 'webm', 'ext': 'webm', @@ -72,7 +72,7 @@ def _real_extract(self, url): 'height': json['height'], 'fps': json['frameRate'], 'filesize': json['webmSize'], - 'preference': 0 + 'preference': 1 }, { 'format_id': 'gif', 'ext': 'gif', @@ -81,7 +81,7 @@ def _real_extract(self, url): 'height': json['height'], 'fps': json['frameRate'], 'filesize': json['gifSize'], - 'preference': 1 + 'preference': 0 }] self._sort_formats(formats) From f52e66505a9f9403d2278d22732ed98b711292fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergey=20M=E2=80=A4?= Date: Sat, 18 Apr 2015 03:50:22 +0600 Subject: [PATCH 4/5] [gfycat] Simplify (Closes #5439, Closes #5394) --- youtube_dl/extractor/gfycat.py | 179 ++++++++++++++------------------- 1 file changed, 77 insertions(+), 102 deletions(-) diff --git a/youtube_dl/extractor/gfycat.py b/youtube_dl/extractor/gfycat.py index 5e70ed3f6..397f1d42e 100644 --- a/youtube_dl/extractor/gfycat.py +++ b/youtube_dl/extractor/gfycat.py @@ -1,115 +1,90 @@ # coding: utf-8 - from __future__ import unicode_literals -import datetime - from .common import InfoExtractor +from ..utils import ( + int_or_none, + float_or_none, + qualities, +) + class GfycatIE(InfoExtractor): _VALID_URL = r'https?://(?:www\.)?gfycat\.com/(?P[^/?#]+)' - _TESTS = [ - { - 'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher', - 'info_dict': { - 'id': 'DeadlyDecisiveGermanpinscher', - 'title': 'Ghost in the Shell', - 'ext': 'mp4', - 'upload_date': '20140913' - } - },{ - 'url': 'http://gfycat.com/pleasinghilariouskusimanse', - 'info_dict': { - 'id': 'pleasinghilariouskusimanse', - 'title': 'PleasingHilariousKusimanse', - 'ext': 'webm', - 'upload_date': '20150412' - }, - 'params': { - 'format': 'webm', - }, - },{ - 'url': 'http://gfycat.com/requiredunkemptbuzzard', - 'info_dict': { - 'id': 'requiredunkemptbuzzard', - 'title': 'Headshot!', - 'ext': 'gif', - 'upload_date': '20150129' - }, - 'params': { - 'format': 'gif', - }, - }, - ] + _TEST = { + 'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher', + 'info_dict': { + 'id': 'DeadlyDecisiveGermanpinscher', + 'ext': 'mp4', + 'title': 'Ghost in the Shell', + 'timestamp': 1410656006, + 'upload_date': '20140914', + 'uploader': 'anonymous', + 'duration': 10.4, + 'view_count': int, + 'like_count': int, + 'dislike_count': int, + 'categories': list, + 'age_limit': 0, + } + } def _real_extract(self, url): video_id = self._match_id(url) - json = self._download_json("http://gfycat.com/cajax/get/" + video_id, video_id, 'Downloading video info')['gfyItem'] - - # Title - # Use user title first, else fallback to url formated name - if json['title']: - video_title = json['title'] - else: - video_title = json['gfyName'] - - # Formats - # Pref: mp4, webm, gif - formats = [{ - 'format_id': 'mp4', - 'ext': 'mp4', - 'url': json['mp4Url'], - 'width': json['width'], - 'height': json['height'], - 'fps': json['frameRate'], - 'filesize': json['mp4Size'], - 'preference': 2 - }, { - 'format_id': 'webm', - 'ext': 'webm', - 'url': json['webmUrl'], - 'width': json['width'], - 'height': json['height'], - 'fps': json['frameRate'], - 'filesize': json['webmSize'], - 'preference': 1 - }, { - 'format_id': 'gif', - 'ext': 'gif', - 'url': json['gifUrl'], - 'width': json['width'], - 'height': json['height'], - 'fps': json['frameRate'], - 'filesize': json['gifSize'], - 'preference': 0 - }] - + + gfy = self._download_json( + 'http://gfycat.com/cajax/get/%s' % video_id, + video_id, 'Downloading video info')['gfyItem'] + + title = gfy.get('title') or gfy['gfyName'] + description = gfy.get('description') + timestamp = int_or_none(gfy.get('createDate')) + uploader = gfy.get('userName') + view_count = int_or_none(gfy.get('views')) + like_count = int_or_none(gfy.get('likes')) + dislike_count = int_or_none(gfy.get('dislikes')) + age_limit = 18 if gfy.get('nsfw') == '1' else 0 + + width = int_or_none(gfy.get('width')) + height = int_or_none(gfy.get('height')) + fps = int_or_none(gfy.get('frameRate')) + num_frames = int_or_none(gfy.get('numFrames')) + + duration = float_or_none(num_frames, fps) if num_frames and fps else None + + categories = gfy.get('tags') or gfy.get('extraLemmas') or [] + + FORMATS = ('gif', 'webm', 'mp4') + quality = qualities(FORMATS) + + formats = [] + for format_id in FORMATS: + video_url = gfy.get('%sUrl' % format_id) + if not video_url: + continue + filesize = gfy.get('%sSize' % format_id) + formats.append({ + 'url': video_url, + 'format_id': format_id, + 'width': width, + 'height': height, + 'fps': fps, + 'filesize': filesize, + 'quality': quality(format_id), + }) self._sort_formats(formats) - - # Date - date = datetime.datetime.fromtimestamp( - int(json['createDate']) - ).strftime('%Y%m%d') - - # Length - duration = json['numFrames'] / json['frameRate'] - - # Age limit - # 1 = nsfw / 0 = sfw - if json['nsfw'] == 1: - age_limit = 18 - else: - age_limit = 0 - + return { - 'id': video_id, - 'title': video_title, - 'formats': formats, - 'creator': json['userName'], - 'description': json['description'], - 'upload_date': date, - 'categories': json['tags'], - 'age_limit': age_limit, - 'duration': duration, - 'view_count': json['views'] + 'id': video_id, + 'title': title, + 'description': description, + 'timestamp': timestamp, + 'uploader': uploader, + 'duration': duration, + 'view_count': view_count, + 'like_count': like_count, + 'dislike_count': dislike_count, + 'categories': categories, + 'age_limit': age_limit, + 'formats': formats, } From bf12cbe07c3a22deb46848df3bd3242da645eab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergey=20M=E2=80=A4?= Date: Sat, 18 Apr 2015 03:51:21 +0600 Subject: [PATCH 5/5] Credit @julianrichen for gfycat (#5440) --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index db3f42b26..267b8da1e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -123,3 +123,4 @@ Will W. Mohammad Teimori Pabandi Roman Le Négrate Matthias Küch +Julian Richen