]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/gfycat.py
eb6f8583644b77f0063d7222e9c8ac37f4d0dae2
[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/(?: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/gifs/detail/UnconsciousLankyIvorygull',
49 'only_matching': True
50 }, {
51 'url': 'https://gfycat.com/acceptablehappygoluckyharborporpoise-baseball',
52 'only_matching': True
53 }]
54
55 def _real_extract(self, url):
56 video_id = self._match_id(url)
57
58 gfy = self._download_json(
59 'https://api.gfycat.com/v1/gfycats/%s' % video_id,
60 video_id, 'Downloading video info')
61 if 'error' in gfy:
62 raise ExtractorError('Gfycat said: ' + gfy['error'], expected=True)
63 gfy = gfy['gfyItem']
64
65 title = gfy.get('title') or gfy['gfyName']
66 description = gfy.get('description')
67 timestamp = int_or_none(gfy.get('createDate'))
68 uploader = gfy.get('userName')
69 view_count = int_or_none(gfy.get('views'))
70 like_count = int_or_none(gfy.get('likes'))
71 dislike_count = int_or_none(gfy.get('dislikes'))
72 age_limit = 18 if gfy.get('nsfw') == '1' else 0
73
74 width = int_or_none(gfy.get('width'))
75 height = int_or_none(gfy.get('height'))
76 fps = int_or_none(gfy.get('frameRate'))
77 num_frames = int_or_none(gfy.get('numFrames'))
78
79 duration = float_or_none(num_frames, fps) if num_frames and fps else None
80
81 categories = gfy.get('tags') or gfy.get('extraLemmas') or []
82
83 FORMATS = ('gif', 'webm', 'mp4')
84 quality = qualities(FORMATS)
85
86 formats = []
87 for format_id in FORMATS:
88 video_url = gfy.get('%sUrl' % format_id)
89 if not video_url:
90 continue
91 filesize = int_or_none(gfy.get('%sSize' % format_id))
92 formats.append({
93 'url': video_url,
94 'format_id': format_id,
95 'width': width,
96 'height': height,
97 'fps': fps,
98 'filesize': filesize,
99 'quality': quality(format_id),
100 })
101 self._sort_formats(formats)
102
103 return {
104 'id': video_id,
105 'title': title,
106 'description': description,
107 'timestamp': timestamp,
108 'uploader': uploader,
109 'duration': duration,
110 'view_count': view_count,
111 'like_count': like_count,
112 'dislike_count': dislike_count,
113 'categories': categories,
114 'age_limit': age_limit,
115 'formats': formats,
116 }