Merge remote-tracking branch 'SyxbEaEQ2/rate-limit'
[youtube-dl] / youtube_dl / extractor / viddler.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5     float_or_none,
6     int_or_none,
7 )
8
9
10 class ViddlerIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?viddler\.com/(?:v|embed|player)/(?P<id>[a-z0-9]+)'
12     _TEST = {
13         "url": "http://www.viddler.com/v/43903784",
14         'md5': 'ae43ad7cb59431ce043f0ff7fa13cbf4',
15         'info_dict': {
16             'id': '43903784',
17             'ext': 'mp4',
18             "title": "Video Made Easy",
19             'description': 'You don\'t need to be a professional to make high-quality video content. Viddler provides some quick and easy tips on how to produce great video content with limited resources. ',
20             "uploader": "viddler",
21             'timestamp': 1335371429,
22             'upload_date': '20120425',
23             "duration": 100.89,
24             'thumbnail': 're:^https?://.*\.jpg$',
25             'view_count': int,
26             'categories': ['video content', 'high quality video', 'video made easy', 'how to produce video with limited resources', 'viddler'],
27         }
28     }
29
30     def _real_extract(self, url):
31         video_id = self._match_id(url)
32
33         json_url = (
34             'http://api.viddler.com/api/v2/viddler.videos.getPlaybackDetails.json?video_id=%s&key=v0vhrt7bg2xq1vyxhkct' %
35             video_id)
36         data = self._download_json(json_url, video_id)['video']
37
38         formats = []
39         for filed in data['files']:
40             if filed.get('status', 'ready') != 'ready':
41                 continue
42             f = {
43                 'format_id': filed['profile_id'],
44                 'format_note': filed['profile_name'],
45                 'url': self._proto_relative_url(filed['url']),
46                 'width': int_or_none(filed.get('width')),
47                 'height': int_or_none(filed.get('height')),
48                 'filesize': int_or_none(filed.get('size')),
49                 'ext': filed.get('ext'),
50                 'source_preference': -1,
51             }
52             formats.append(f)
53
54             if filed.get('cdn_url'):
55                 f = f.copy()
56                 f['url'] = self._proto_relative_url(filed['cdn_url'])
57                 f['format_id'] = filed['profile_id'] + '-cdn'
58                 f['source_preference'] = 1
59                 formats.append(f)
60
61             if filed.get('html5_video_source'):
62                 f = f.copy()
63                 f['url'] = self._proto_relative_url(
64                     filed['html5_video_source'])
65                 f['format_id'] = filed['profile_id'] + '-html5'
66                 f['source_preference'] = 0
67                 formats.append(f)
68         self._sort_formats(formats)
69
70         categories = [
71             t.get('text') for t in data.get('tags', []) if 'text' in t]
72
73         return {
74             '_type': 'video',
75             'id': video_id,
76             'title': data['title'],
77             'formats': formats,
78             'description': data.get('description'),
79             'timestamp': int_or_none(data.get('upload_time')),
80             'thumbnail': self._proto_relative_url(data.get('thumbnail_url')),
81             'uploader': data.get('author'),
82             'duration': float_or_none(data.get('length')),
83             'view_count': int_or_none(data.get('view_count')),
84             'categories': categories,
85         }