]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/kaltura.py
[fox] Add new extractor(closes #3063)
[youtube-dl.git] / youtube_dl / extractor / kaltura.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import base64
6
7 from .common import InfoExtractor
8 from ..compat import (
9     compat_urllib_parse,
10     compat_urlparse,
11 )
12 from ..utils import (
13     clean_html,
14     ExtractorError,
15     int_or_none,
16     unsmuggle_url,
17 )
18
19
20 class KalturaIE(InfoExtractor):
21     _VALID_URL = r'''(?x)
22                 (?:
23                     kaltura:(?P<partner_id_s>\d+):(?P<id_s>[0-9a-z_]+)|
24                     https?://
25                         (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/
26                         (?:
27                             (?:
28                                 # flash player
29                                 index\.php/kwidget/
30                                 (?:[^/]+/)*?wid/_(?P<partner_id>\d+)/
31                                 (?:[^/]+/)*?entry_id/(?P<id>[0-9a-z_]+)|
32                                 # html5 player
33                                 html5/html5lib/
34                                 (?:[^/]+/)*?entry_id/(?P<id_html5>[0-9a-z_]+)
35                                 .*\?.*\bwid=_(?P<partner_id_html5>\d+)
36                             )
37                         )
38                 )
39                 '''
40     _API_BASE = 'http://cdnapi.kaltura.com/api_v3/index.php?'
41     _TESTS = [
42         {
43             'url': 'kaltura:269692:1_1jc2y3e4',
44             'md5': '3adcbdb3dcc02d647539e53f284ba171',
45             'info_dict': {
46                 'id': '1_1jc2y3e4',
47                 'ext': 'mp4',
48                 'title': 'Straight from the Heart',
49                 'upload_date': '20131219',
50                 'uploader_id': 'mlundberg@wolfgangsvault.com',
51                 'description': 'The Allman Brothers Band, 12/16/1981',
52                 'thumbnail': 're:^https?://.*/thumbnail/.*',
53                 'timestamp': int,
54             },
55         },
56         {
57             'url': 'http://www.kaltura.com/index.php/kwidget/cache_st/1300318621/wid/_269692/uiconf_id/3873291/entry_id/1_1jc2y3e4',
58             'only_matching': True,
59         },
60         {
61             'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3',
62             'only_matching': True,
63         },
64         {
65             'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.30.2/mwEmbedFrame.php/p/1337/uiconf_id/20540612/entry_id/1_sf5ovm7u?wid=_243342',
66             'only_matching': True,
67         }
68     ]
69
70     def _kaltura_api_call(self, video_id, actions, *args, **kwargs):
71         params = actions[0]
72         if len(actions) > 1:
73             for i, a in enumerate(actions[1:], start=1):
74                 for k, v in a.items():
75                     params['%d:%s' % (i, k)] = v
76
77         query = compat_urllib_parse.urlencode(params)
78         url = self._API_BASE + query
79         data = self._download_json(url, video_id, *args, **kwargs)
80
81         status = data if len(actions) == 1 else data[0]
82         if status.get('objectType') == 'KalturaAPIException':
83             raise ExtractorError(
84                 '%s said: %s' % (self.IE_NAME, status['message']))
85
86         return data
87
88     def _get_kaltura_signature(self, video_id, partner_id):
89         actions = [{
90             'apiVersion': '3.1',
91             'expiry': 86400,
92             'format': 1,
93             'service': 'session',
94             'action': 'startWidgetSession',
95             'widgetId': '_%s' % partner_id,
96         }]
97         return self._kaltura_api_call(
98             video_id, actions, note='Downloading Kaltura signature')['ks']
99
100     def _get_video_info(self, video_id, partner_id):
101         signature = self._get_kaltura_signature(video_id, partner_id)
102         actions = [
103             {
104                 'action': 'null',
105                 'apiVersion': '3.1.5',
106                 'clientTag': 'kdp:v3.8.5',
107                 'format': 1,  # JSON, 2 = XML, 3 = PHP
108                 'service': 'multirequest',
109                 'ks': signature,
110             },
111             {
112                 'action': 'get',
113                 'entryId': video_id,
114                 'service': 'baseentry',
115                 'version': '-1',
116             },
117             {
118                 'action': 'getbyentryid',
119                 'entryId': video_id,
120                 'service': 'flavorAsset',
121             },
122         ]
123         return self._kaltura_api_call(
124             video_id, actions, note='Downloading video info JSON')
125
126     def _real_extract(self, url):
127         url, smuggled_data = unsmuggle_url(url, {})
128
129         mobj = re.match(self._VALID_URL, url)
130         partner_id = mobj.group('partner_id_s') or mobj.group('partner_id') or mobj.group('partner_id_html5')
131         entry_id = mobj.group('id_s') or mobj.group('id') or mobj.group('id_html5')
132
133         info, flavor_assets = self._get_video_info(entry_id, partner_id)
134
135         source_url = smuggled_data.get('source_url')
136         if source_url:
137             referrer = base64.b64encode(
138                 '://'.join(compat_urlparse.urlparse(source_url)[:2])
139                 .encode('utf-8')).decode('utf-8')
140         else:
141             referrer = None
142
143         formats = []
144         for f in flavor_assets:
145             # Continue if asset is not ready
146             if f['status'] != 2:
147                 continue
148             video_url = '%s/flavorId/%s' % (info['dataUrl'], f['id'])
149             if referrer:
150                 video_url += '?referrer=%s' % referrer
151             formats.append({
152                 'format_id': '%(fileExt)s-%(bitrate)s' % f,
153                 'ext': f.get('fileExt'),
154                 'tbr': int_or_none(f['bitrate']),
155                 'fps': int_or_none(f.get('frameRate')),
156                 'filesize_approx': int_or_none(f.get('size'), invscale=1024),
157                 'container': f.get('containerFormat'),
158                 'vcodec': f.get('videoCodecId'),
159                 'height': int_or_none(f.get('height')),
160                 'width': int_or_none(f.get('width')),
161                 'url': video_url,
162             })
163         m3u8_url = info['dataUrl'].replace('format/url', 'format/applehttp')
164         if referrer:
165             m3u8_url += '?referrer=%s' % referrer
166         formats.extend(self._extract_m3u8_formats(
167             m3u8_url, entry_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
168
169         self._check_formats(formats, entry_id)
170         self._sort_formats(formats)
171
172         return {
173             'id': entry_id,
174             'title': info['name'],
175             'formats': formats,
176             'description': clean_html(info.get('description')),
177             'thumbnail': info.get('thumbnailUrl'),
178             'duration': info.get('duration'),
179             'timestamp': info.get('createdAt'),
180             'uploader_id': info.get('userId'),
181             'view_count': info.get('plays'),
182         }