[gfycat] Add new extractor
[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         'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher',
13         'info_dict': {
14             'id':          'DeadlyDecisiveGermanpinscher',
15             'title':       'Ghost in the Shell',
16             'ext':         'mp4',
17             'upload_date': '20140913'
18         }
19     },{
20         'url': 'http://gfycat.com/pleasinghilariouskusimanse',
21         'info_dict': {
22             'id':          'pleasinghilariouskusimanse',
23             'title':       'PleasingHilariousKusimanse',
24             'ext':         'webm',
25             'upload_date': '20150412'
26         }
27     },{
28         'url': 'http://gfycat.com/requiredunkemptbuzzard',
29         'info_dict': {
30             'id':          'requiredunkemptbuzzard',
31             'title':       'Headshot!',
32             'ext':         'gif',
33             'upload_date': '20150130'
34         }
35     }]
36
37     def _real_extract(self, url):
38         video_id = self._match_id(url)
39         json     = self._download_json("http://gfycat.com/cajax/get/" + video_id, video_id, 'Downloading video info')['gfyItem']
40         
41         # Title
42         # Use user title first, else fallback to url formated name
43         if json['title']:
44             video_title = json['title']
45         else:
46             video_title = json['gfyName']
47         
48         # Formats
49         # Pref: mp4, webm, gif
50         formats = [{
51             'format_id':  'mp4',
52             'ext':        'mp4',
53             'url':        json['mp4Url'],
54             'width':      json['width'],
55             'height':     json['height'],
56             'fps':        json['frameRate'],
57             'filesize':   json['mp4Size'],
58             'preference': '-1'
59         }, {
60             'format_id': 'webm',
61             'ext':       'webm',
62             'url':        json['webmUrl'],
63             'width':      json['width'],
64             'height':     json['height'],
65             'fps':        json['frameRate'],
66             'filesize':   json['webmSize'],
67             'preference': 0
68         }, {
69             'format_id':  'gif',
70             'ext':        'gif',
71             'url':        json['gifUrl'],
72             'width':      json['width'],
73             'height':     json['height'],
74             'fps':        json['frameRate'],
75             'filesize':   json['gifSize'],
76             'preference': 1
77         }]
78         
79         self._sort_formats(formats)
80         
81         # Date
82         date = datetime.datetime.fromtimestamp(
83             int(json['createDate'])
84         ).strftime('%Y%m%d')
85         
86         # Length
87         duration = json['numFrames'] / json['frameRate']
88         
89         # Age limit
90         # 1 = nsfw / 0 = sfw
91         if json['nsfw'] == 1:
92             age_limit = 18
93         else:
94             age_limit = 0
95         
96         return {
97             'id':          video_id,
98             'title':       video_title,
99             'formats':     formats,
100             'creator':     json['userName'],
101             'description': json['description'],
102             'upload_date': date,
103             'categories':  json['tags'],
104             'age_limit':   age_limit,
105             'duration':    duration,
106             'view_count':  json['views']
107         }