]>
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): | |
2adedc47 | 14 | _VALID_URL = r'https?://(?:www\.)?gfycat\.com/(?:ru/|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 | } | |
2adedc47 G |
47 | }, { |
48 | 'url': 'https://gfycat.com/ru/RemarkableDrearyAmurstarfish', | |
49 | 'only_matching': True | |
b14b2283 T |
50 | }, { |
51 | 'url': 'https://gfycat.com/gifs/detail/UnconsciousLankyIvorygull', | |
52 | 'only_matching': True | |
68b92aa1 | 53 | }, { |
54 | 'url': 'https://gfycat.com/acceptablehappygoluckyharborporpoise-baseball', | |
55 | 'only_matching': True | |
d7b4d5dd | 56 | }] |
afe4a8c7 F |
57 | |
58 | def _real_extract(self, url): | |
59 | video_id = self._match_id(url) | |
f52e6650 S |
60 | |
61 | gfy = self._download_json( | |
16597c2f | 62 | 'https://api.gfycat.com/v1/gfycats/%s' % video_id, |
267dc07e YCH |
63 | video_id, 'Downloading video info') |
64 | if 'error' in gfy: | |
65 | raise ExtractorError('Gfycat said: ' + gfy['error'], expected=True) | |
66 | gfy = gfy['gfyItem'] | |
f52e6650 S |
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 | |
87f56469 | 94 | filesize = int_or_none(gfy.get('%sSize' % format_id)) |
f52e6650 S |
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 | }) | |
afe4a8c7 | 104 | self._sort_formats(formats) |
f52e6650 | 105 | |
afe4a8c7 | 106 | return { |
f52e6650 S |
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, | |
afe4a8c7 | 119 | } |