From 70ad513a1c511dca5f00d2de99211822211af287 Mon Sep 17 00:00:00 2001 From: Jakub Wilk Date: Wed, 10 Apr 2019 20:24:17 +0200 Subject: [PATCH] [giphy] Add extractor --- youtube_dl/extractor/extractors.py | 1 + youtube_dl/extractor/giphy.py | 62 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 youtube_dl/extractor/giphy.py diff --git a/youtube_dl/extractor/extractors.py b/youtube_dl/extractor/extractors.py index 392b1f92b..9a8582b80 100644 --- a/youtube_dl/extractor/extractors.py +++ b/youtube_dl/extractor/extractors.py @@ -423,6 +423,7 @@ from .gaskrank import GaskrankIE from .gazeta import GazetaIE from .gdcvault import GDCVaultIE from .generic import GenericIE +from .giphy import GiphyIE from .gfycat import GfycatIE from .giantbomb import GiantBombIE from .giga import GigaIE diff --git a/youtube_dl/extractor/giphy.py b/youtube_dl/extractor/giphy.py new file mode 100644 index 000000000..263291481 --- /dev/null +++ b/youtube_dl/extractor/giphy.py @@ -0,0 +1,62 @@ +# coding: utf-8 +from __future__ import unicode_literals + +import re + +from .common import InfoExtractor +from ..utils import ( + int_or_none, + remove_end, + remove_start, +) + + +class GiphyIE(InfoExtractor): + _VALID_URL = r'https://(?:i|media)\.giphy\.com/media/(\w+)/|https://giphy\.com/gifs/[^/#?]+-(\w+)(?:[#?]|$)' + _TESTS = [{ + 'url': 'https://media.giphy.com/media/2rj8VysAig8QE/giphy.gif', + 'md5': 'faa04a4e3134cb74005b78b28c34cc29', + 'info_dict': { + 'id': '2rj8VysAig8QE', + 'ext': 'mp4', + 'title': 'Css GIF', + } + }, { + 'url': 'https://i.giphy.com/media/oY7zuBsNna7CM/giphy.mp4', + 'only_matching': True + }, { + 'url': 'https://giphy.com/gifs/quit-playin-MZAvNpYCrFUJi', + 'only_matching': True + }] + + def _real_extract(self, url): + [video_id] = filter(None, re.match(self._VALID_URL, url).groups()) + page_url = 'https://media.giphy.com/media/{id}/giphy.gif'.format(id=video_id) + webpage = self._download_webpage(page_url, video_id) + title = self._og_search_title(webpage) + title = remove_end(title, ' - Find & Share on GIPHY') + formats = [] + for prop in 'image', 'video': + url = self._og_search_property(prop, webpage) + mime_type = self._og_search_property(prop + ':type', webpage) + if mime_type == 'image/gif': + format_id = 'gif' + quality = 0 + else: + format_id = remove_start(mime_type, 'video/') + quality = 1 + fmt = { + 'url': url, + 'format_id': format_id, + 'quality': quality, + } + for dim in 'width', 'height': + fmt[dim] = int_or_none( + self._og_search_property(prop + ':' + dim, webpage, fatal=False)) + formats.append(fmt) + self._sort_formats(formats) + return { + 'id': video_id, + 'title': title, + 'formats': formats, + }