[gfycat] Fixed preferences.
[youtube-dl] / youtube_dl / extractor / gfycat.py
1 # coding: utf-8
2
3 from __future__ import unicode_literals
4
5 import datetime
6
7 from .common import InfoExtractor
8
9 class GfycatIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?gfycat\.com/(?P<id>[^/?#]+)'
11     _TESTS = [
12         {
13             'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher',
14             'info_dict': {
15                 'id':          'DeadlyDecisiveGermanpinscher',
16                 'title':       'Ghost in the Shell',
17                 'ext':         'mp4',
18                 'upload_date': '20140913'
19             }
20         },{
21             'url': 'http://gfycat.com/pleasinghilariouskusimanse',
22             'info_dict': {
23                 'id':          'pleasinghilariouskusimanse',
24                 'title':       'PleasingHilariousKusimanse',
25                 'ext':         'webm',
26                 'upload_date': '20150412'
27             },
28             'params': {
29                 'format': 'webm',
30             },
31         },{
32             'url': 'http://gfycat.com/requiredunkemptbuzzard',
33             'info_dict': {
34                 'id':          'requiredunkemptbuzzard',
35                 'title':       'Headshot!',
36                 'ext':         'gif',
37                 'upload_date': '20150129'
38             },
39             'params': {
40                 'format': 'gif',
41             },
42         },
43     ]
44
45     def _real_extract(self, url):
46         video_id = self._match_id(url)
47         json     = self._download_json("http://gfycat.com/cajax/get/" + video_id, video_id, 'Downloading video info')['gfyItem']
48         
49         # Title
50         # Use user title first, else fallback to url formated name
51         if json['title']:
52             video_title = json['title']
53         else:
54             video_title = json['gfyName']
55         
56         # Formats
57         # Pref: mp4, webm, gif
58         formats = [{
59             'format_id':  'mp4',
60             'ext':        'mp4',
61             'url':        json['mp4Url'],
62             'width':      json['width'],
63             'height':     json['height'],
64             'fps':        json['frameRate'],
65             'filesize':   json['mp4Size'],
66             'preference': 2
67         }, {
68             'format_id': 'webm',
69             'ext':       'webm',
70             'url':        json['webmUrl'],
71             'width':      json['width'],
72             'height':     json['height'],
73             'fps':        json['frameRate'],
74             'filesize':   json['webmSize'],
75             'preference': 1
76         }, {
77             'format_id':  'gif',
78             'ext':        'gif',
79             'url':        json['gifUrl'],
80             'width':      json['width'],
81             'height':     json['height'],
82             'fps':        json['frameRate'],
83             'filesize':   json['gifSize'],
84             'preference': 0
85         }]
86         
87         self._sort_formats(formats)
88         
89         # Date
90         date = datetime.datetime.fromtimestamp(
91             int(json['createDate'])
92         ).strftime('%Y%m%d')
93         
94         # Length
95         duration = json['numFrames'] / json['frameRate']
96         
97         # Age limit
98         # 1 = nsfw / 0 = sfw
99         if json['nsfw'] == 1:
100             age_limit = 18
101         else:
102             age_limit = 0
103         
104         return {
105             'id':          video_id,
106             'title':       video_title,
107             'formats':     formats,
108             'creator':     json['userName'],
109             'description': json['description'],
110             'upload_date': date,
111             'categories':  json['tags'],
112             'age_limit':   age_limit,
113             'duration':    duration,
114             'view_count':  json['views']
115         }