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