Switch codebase to use sanitized_Request instead of
[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     sanitized_Request,
8 )
9
10
11 class ViddlerIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?viddler\.com/(?:v|embed|player)/(?P<id>[a-z0-9]+)'
13     _TESTS = [{
14         'url': 'http://www.viddler.com/v/43903784',
15         'md5': 'ae43ad7cb59431ce043f0ff7fa13cbf4',
16         'info_dict': {
17             'id': '43903784',
18             'ext': 'mp4',
19             'title': 'Video Made Easy',
20             'description': 'md5:6a697ebd844ff3093bd2e82c37b409cd',
21             'uploader': 'viddler',
22             'timestamp': 1335371429,
23             'upload_date': '20120425',
24             'duration': 100.89,
25             'thumbnail': 're:^https?://.*\.jpg$',
26             'view_count': int,
27             'comment_count': int,
28             'categories': ['video content', 'high quality video', 'video made easy', 'how to produce video with limited resources', 'viddler'],
29         }
30     }, {
31         'url': 'http://www.viddler.com/v/4d03aad9/',
32         'md5': 'faa71fbf70c0bee7ab93076fd007f4b0',
33         'info_dict': {
34             'id': '4d03aad9',
35             'ext': 'mp4',
36             'title': 'WALL-TO-GORTAT',
37             'upload_date': '20150126',
38             'uploader': 'deadspin',
39             'timestamp': 1422285291,
40             'view_count': int,
41             'comment_count': int,
42         }
43     }, {
44         'url': 'http://www.viddler.com/player/221ebbbd/0/',
45         'md5': '0defa2bd0ea613d14a6e9bd1db6be326',
46         'info_dict': {
47             'id': '221ebbbd',
48             'ext': 'mp4',
49             'title': 'LETeens-Grammar-snack-third-conditional',
50             'description': ' ',
51             'upload_date': '20140929',
52             'uploader': 'BCLETeens',
53             'timestamp': 1411997190,
54             'view_count': int,
55             'comment_count': int,
56         }
57     }]
58
59     def _real_extract(self, url):
60         video_id = self._match_id(url)
61
62         json_url = (
63             'http://api.viddler.com/api/v2/viddler.videos.getPlaybackDetails.json?video_id=%s&key=v0vhrt7bg2xq1vyxhkct' %
64             video_id)
65         headers = {'Referer': 'http://static.cdn-ec.viddler.com/js/arpeggio/v2/embed.html'}
66         request = sanitized_Request(json_url, None, headers)
67         data = self._download_json(request, video_id)['video']
68
69         formats = []
70         for filed in data['files']:
71             if filed.get('status', 'ready') != 'ready':
72                 continue
73             format_id = filed.get('profile_id') or filed['profile_name']
74             f = {
75                 'format_id': format_id,
76                 'format_note': filed['profile_name'],
77                 'url': self._proto_relative_url(filed['url']),
78                 'width': int_or_none(filed.get('width')),
79                 'height': int_or_none(filed.get('height')),
80                 'filesize': int_or_none(filed.get('size')),
81                 'ext': filed.get('ext'),
82                 'source_preference': -1,
83             }
84             formats.append(f)
85
86             if filed.get('cdn_url'):
87                 f = f.copy()
88                 f['url'] = self._proto_relative_url(filed['cdn_url'], 'http:')
89                 f['format_id'] = format_id + '-cdn'
90                 f['source_preference'] = 1
91                 formats.append(f)
92
93             if filed.get('html5_video_source'):
94                 f = f.copy()
95                 f['url'] = self._proto_relative_url(filed['html5_video_source'])
96                 f['format_id'] = format_id + '-html5'
97                 f['source_preference'] = 0
98                 formats.append(f)
99         self._sort_formats(formats)
100
101         categories = [
102             t.get('text') for t in data.get('tags', []) if 'text' in t]
103
104         return {
105             'id': video_id,
106             'title': data['title'],
107             'formats': formats,
108             'description': data.get('description'),
109             'timestamp': int_or_none(data.get('upload_time')),
110             'thumbnail': self._proto_relative_url(data.get('thumbnail_url')),
111             'uploader': data.get('author'),
112             'duration': float_or_none(data.get('length')),
113             'view_count': int_or_none(data.get('view_count')),
114             'comment_count': int_or_none(data.get('comment_count')),
115             'categories': categories,
116         }