[gfycat] Extract id correctly (fixes #6165)
[youtube-dl] / 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 )
10
11
12 class GfycatIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?gfycat\.com/(?:ifr/)?(?P<id>[^/?#]+)'
14     _TESTS = [{
15         'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher',
16         'info_dict': {
17             'id': 'DeadlyDecisiveGermanpinscher',
18             'ext': 'mp4',
19             'title': 'Ghost in the Shell',
20             'timestamp': 1410656006,
21             'upload_date': '20140914',
22             'uploader': 'anonymous',
23             'duration': 10.4,
24             'view_count': int,
25             'like_count': int,
26             'dislike_count': int,
27             'categories': list,
28             'age_limit': 0,
29         }
30     }, {
31         'url': 'http://gfycat.com/ifr/JauntyTimelyAmazontreeboa',
32         'info_dict': {
33             'id': 'JauntyTimelyAmazontreeboa',
34             'ext': 'mp4',
35             'title': 'JauntyTimelyAmazontreeboa',
36             'timestamp': 1411720126,
37             'upload_date': '20140926',
38             'uploader': 'anonymous',
39             'duration': 3.52,
40             'view_count': int,
41             'like_count': int,
42             'dislike_count': int,
43             'categories': list,
44             'age_limit': 0,
45         }
46     }]
47
48     def _real_extract(self, url):
49         video_id = self._match_id(url)
50
51         gfy = self._download_json(
52             'http://gfycat.com/cajax/get/%s' % video_id,
53             video_id, 'Downloading video info')['gfyItem']
54
55         title = gfy.get('title') or gfy['gfyName']
56         description = gfy.get('description')
57         timestamp = int_or_none(gfy.get('createDate'))
58         uploader = gfy.get('userName')
59         view_count = int_or_none(gfy.get('views'))
60         like_count = int_or_none(gfy.get('likes'))
61         dislike_count = int_or_none(gfy.get('dislikes'))
62         age_limit = 18 if gfy.get('nsfw') == '1' else 0
63
64         width = int_or_none(gfy.get('width'))
65         height = int_or_none(gfy.get('height'))
66         fps = int_or_none(gfy.get('frameRate'))
67         num_frames = int_or_none(gfy.get('numFrames'))
68
69         duration = float_or_none(num_frames, fps) if num_frames and fps else None
70
71         categories = gfy.get('tags') or gfy.get('extraLemmas') or []
72
73         FORMATS = ('gif', 'webm', 'mp4')
74         quality = qualities(FORMATS)
75
76         formats = []
77         for format_id in FORMATS:
78             video_url = gfy.get('%sUrl' % format_id)
79             if not video_url:
80                 continue
81             filesize = gfy.get('%sSize' % format_id)
82             formats.append({
83                 'url': video_url,
84                 'format_id': format_id,
85                 'width': width,
86                 'height': height,
87                 'fps': fps,
88                 'filesize': filesize,
89                 'quality': quality(format_id),
90             })
91         self._sort_formats(formats)
92
93         return {
94             'id': video_id,
95             'title': title,
96             'description': description,
97             'timestamp': timestamp,
98             'uploader': uploader,
99             'duration': duration,
100             'view_count': view_count,
101             'like_count': like_count,
102             'dislike_count': dislike_count,
103             'categories': categories,
104             'age_limit': age_limit,
105             'formats': formats,
106         }