Merge branch 'julianrichen-gfycat'
[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/(?P<id>[^/?#]+)'
14     _TEST = {
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
32     def _real_extract(self, url):
33         video_id = self._match_id(url)
34
35         gfy = self._download_json(
36             'http://gfycat.com/cajax/get/%s' % video_id,
37             video_id, 'Downloading video info')['gfyItem']
38
39         title = gfy.get('title') or gfy['gfyName']
40         description = gfy.get('description')
41         timestamp = int_or_none(gfy.get('createDate'))
42         uploader = gfy.get('userName')
43         view_count = int_or_none(gfy.get('views'))
44         like_count = int_or_none(gfy.get('likes'))
45         dislike_count = int_or_none(gfy.get('dislikes'))
46         age_limit = 18 if gfy.get('nsfw') == '1' else 0
47
48         width = int_or_none(gfy.get('width'))
49         height = int_or_none(gfy.get('height'))
50         fps = int_or_none(gfy.get('frameRate'))
51         num_frames = int_or_none(gfy.get('numFrames'))
52
53         duration = float_or_none(num_frames, fps) if num_frames and fps else None
54
55         categories = gfy.get('tags') or gfy.get('extraLemmas') or []
56
57         FORMATS = ('gif', 'webm', 'mp4')
58         quality = qualities(FORMATS)
59
60         formats = []
61         for format_id in FORMATS:
62             video_url = gfy.get('%sUrl' % format_id)
63             if not video_url:
64                 continue
65             filesize = gfy.get('%sSize' % format_id)
66             formats.append({
67                 'url': video_url,
68                 'format_id': format_id,
69                 'width': width,
70                 'height': height,
71                 'fps': fps,
72                 'filesize': filesize,
73                 'quality': quality(format_id),
74             })
75         self._sort_formats(formats)
76
77         return {
78             'id': video_id,
79             'title': title,
80             'description': description,
81             'timestamp': timestamp,
82             'uploader': uploader,
83             'duration': duration,
84             'view_count': view_count,
85             'like_count': like_count,
86             'dislike_count': dislike_count,
87             'categories': categories,
88             'age_limit': age_limit,
89             'formats': formats,
90         }