]>
Commit | Line | Data |
---|---|---|
afe4a8c7 | 1 | # coding: utf-8 |
afe4a8c7 F |
2 | from __future__ import unicode_literals |
3 | ||
afe4a8c7 | 4 | from .common import InfoExtractor |
f52e6650 S |
5 | from ..utils import ( |
6 | int_or_none, | |
7 | float_or_none, | |
8 | qualities, | |
267dc07e | 9 | ExtractorError, |
f52e6650 S |
10 | ) |
11 | ||
afe4a8c7 F |
12 | |
13 | class GfycatIE(InfoExtractor): | |
b14b2283 | 14 | _VALID_URL = r'https?://(?:www\.)?gfycat\.com/(?:ifr/|gifs/detail/)?(?P<id>[^/?#]+)' |
d7b4d5dd | 15 | _TESTS = [{ |
f52e6650 S |
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 | } | |
d7b4d5dd YCH |
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 | } | |
b14b2283 T |
47 | }, { |
48 | 'url': 'https://gfycat.com/gifs/detail/UnconsciousLankyIvorygull', | |
49 | 'only_matching': True | |
d7b4d5dd | 50 | }] |
afe4a8c7 F |
51 | |
52 | def _real_extract(self, url): | |
53 | video_id = self._match_id(url) | |
f52e6650 S |
54 | |
55 | gfy = self._download_json( | |
16597c2f | 56 | 'https://api.gfycat.com/v1/gfycats/%s' % video_id, |
267dc07e YCH |
57 | video_id, 'Downloading video info') |
58 | if 'error' in gfy: | |
59 | raise ExtractorError('Gfycat said: ' + gfy['error'], expected=True) | |
60 | gfy = gfy['gfyItem'] | |
f52e6650 S |
61 | |
62 | title = gfy.get('title') or gfy['gfyName'] | |
63 | description = gfy.get('description') | |
64 | timestamp = int_or_none(gfy.get('createDate')) | |
65 | uploader = gfy.get('userName') | |
66 | view_count = int_or_none(gfy.get('views')) | |
67 | like_count = int_or_none(gfy.get('likes')) | |
68 | dislike_count = int_or_none(gfy.get('dislikes')) | |
69 | age_limit = 18 if gfy.get('nsfw') == '1' else 0 | |
70 | ||
71 | width = int_or_none(gfy.get('width')) | |
72 | height = int_or_none(gfy.get('height')) | |
73 | fps = int_or_none(gfy.get('frameRate')) | |
74 | num_frames = int_or_none(gfy.get('numFrames')) | |
75 | ||
76 | duration = float_or_none(num_frames, fps) if num_frames and fps else None | |
77 | ||
78 | categories = gfy.get('tags') or gfy.get('extraLemmas') or [] | |
79 | ||
80 | FORMATS = ('gif', 'webm', 'mp4') | |
81 | quality = qualities(FORMATS) | |
82 | ||
83 | formats = [] | |
84 | for format_id in FORMATS: | |
85 | video_url = gfy.get('%sUrl' % format_id) | |
86 | if not video_url: | |
87 | continue | |
87f56469 | 88 | filesize = int_or_none(gfy.get('%sSize' % format_id)) |
f52e6650 S |
89 | formats.append({ |
90 | 'url': video_url, | |
91 | 'format_id': format_id, | |
92 | 'width': width, | |
93 | 'height': height, | |
94 | 'fps': fps, | |
95 | 'filesize': filesize, | |
96 | 'quality': quality(format_id), | |
97 | }) | |
afe4a8c7 | 98 | self._sort_formats(formats) |
f52e6650 | 99 | |
afe4a8c7 | 100 | return { |
f52e6650 S |
101 | 'id': video_id, |
102 | 'title': title, | |
103 | 'description': description, | |
104 | 'timestamp': timestamp, | |
105 | 'uploader': uploader, | |
106 | 'duration': duration, | |
107 | 'view_count': view_count, | |
108 | 'like_count': like_count, | |
109 | 'dislike_count': dislike_count, | |
110 | 'categories': categories, | |
111 | 'age_limit': age_limit, | |
112 | 'formats': formats, | |
afe4a8c7 | 113 | } |