]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/gfycat.py
New upstream version 2019.09.28
[youtubedl] / youtube_dl / extractor / gfycat.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6 int_or_none,
7 float_or_none,
8 qualities,
9 ExtractorError,
10 )
11
12
13 class GfycatIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?gfycat\.com/(?:ru/|ifr/|gifs/detail/)?(?P<id>[^-/?#]+)'
15 _TESTS = [{
16 'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher',
17 'info_dict': {
18 'id': 'DeadlyDecisiveGermanpinscher',
19 'ext': 'mp4',
20 'title': 'Ghost in the Shell',
21 'timestamp': 1410656006,
22 'upload_date': '20140914',
23 'uploader': 'anonymous',
24 'duration': 10.4,
25 'view_count': int,
26 'like_count': int,
27 'dislike_count': int,
28 'categories': list,
29 'age_limit': 0,
30 }
31 }, {
32 'url': 'http://gfycat.com/ifr/JauntyTimelyAmazontreeboa',
33 'info_dict': {
34 'id': 'JauntyTimelyAmazontreeboa',
35 'ext': 'mp4',
36 'title': 'JauntyTimelyAmazontreeboa',
37 'timestamp': 1411720126,
38 'upload_date': '20140926',
39 'uploader': 'anonymous',
40 'duration': 3.52,
41 'view_count': int,
42 'like_count': int,
43 'dislike_count': int,
44 'categories': list,
45 'age_limit': 0,
46 }
47 }, {
48 'url': 'https://gfycat.com/ru/RemarkableDrearyAmurstarfish',
49 'only_matching': True
50 }, {
51 'url': 'https://gfycat.com/gifs/detail/UnconsciousLankyIvorygull',
52 'only_matching': True
53 }, {
54 'url': 'https://gfycat.com/acceptablehappygoluckyharborporpoise-baseball',
55 'only_matching': True
56 }]
57
58 def _real_extract(self, url):
59 video_id = self._match_id(url)
60
61 gfy = self._download_json(
62 'https://api.gfycat.com/v1/gfycats/%s' % video_id,
63 video_id, 'Downloading video info')
64 if 'error' in gfy:
65 raise ExtractorError('Gfycat said: ' + gfy['error'], expected=True)
66 gfy = gfy['gfyItem']
67
68 title = gfy.get('title') or gfy['gfyName']
69 description = gfy.get('description')
70 timestamp = int_or_none(gfy.get('createDate'))
71 uploader = gfy.get('userName')
72 view_count = int_or_none(gfy.get('views'))
73 like_count = int_or_none(gfy.get('likes'))
74 dislike_count = int_or_none(gfy.get('dislikes'))
75 age_limit = 18 if gfy.get('nsfw') == '1' else 0
76
77 width = int_or_none(gfy.get('width'))
78 height = int_or_none(gfy.get('height'))
79 fps = int_or_none(gfy.get('frameRate'))
80 num_frames = int_or_none(gfy.get('numFrames'))
81
82 duration = float_or_none(num_frames, fps) if num_frames and fps else None
83
84 categories = gfy.get('tags') or gfy.get('extraLemmas') or []
85
86 FORMATS = ('gif', 'webm', 'mp4')
87 quality = qualities(FORMATS)
88
89 formats = []
90 for format_id in FORMATS:
91 video_url = gfy.get('%sUrl' % format_id)
92 if not video_url:
93 continue
94 filesize = int_or_none(gfy.get('%sSize' % format_id))
95 formats.append({
96 'url': video_url,
97 'format_id': format_id,
98 'width': width,
99 'height': height,
100 'fps': fps,
101 'filesize': filesize,
102 'quality': quality(format_id),
103 })
104 self._sort_formats(formats)
105
106 return {
107 'id': video_id,
108 'title': title,
109 'description': description,
110 'timestamp': timestamp,
111 'uploader': uploader,
112 'duration': duration,
113 'view_count': view_count,
114 'like_count': like_count,
115 'dislike_count': dislike_count,
116 'categories': categories,
117 'age_limit': age_limit,
118 'formats': formats,
119 }