Merge pull request #7045 from remitamine/ign
[youtube-dl] / 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': 'Track 4',
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': 'getContextData',
119                 'contextDataParams:objectType': 'KalturaEntryContextDataParams',
120                 'contextDataParams:referrer': 'http://www.kaltura.com/',
121                 'contextDataParams:streamerType': 'http',
122                 'entryId': video_id,
123                 'service': 'baseentry',
124             },
125         ]
126         return self._kaltura_api_call(
127             video_id, actions, note='Downloading video info JSON')
128
129     def _real_extract(self, url):
130         url, smuggled_data = unsmuggle_url(url, {})
131
132         mobj = re.match(self._VALID_URL, url)
133         partner_id = mobj.group('partner_id_s') or mobj.group('partner_id') or mobj.group('partner_id_html5')
134         entry_id = mobj.group('id_s') or mobj.group('id') or mobj.group('id_html5')
135
136         info, source_data = self._get_video_info(entry_id, partner_id)
137
138         source_url = smuggled_data.get('source_url')
139         if source_url:
140             referrer = base64.b64encode(
141                 '://'.join(compat_urlparse.urlparse(source_url)[:2])
142                 .encode('utf-8')).decode('utf-8')
143         else:
144             referrer = None
145
146         formats = []
147         for f in source_data['flavorAssets']:
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         self._check_formats(formats, entry_id)
164         self._sort_formats(formats)
165
166         return {
167             'id': entry_id,
168             'title': info['name'],
169             'formats': formats,
170             'description': clean_html(info.get('description')),
171             'thumbnail': info.get('thumbnailUrl'),
172             'duration': info.get('duration'),
173             'timestamp': info.get('createdAt'),
174             'uploader_id': info.get('userId'),
175             'view_count': info.get('plays'),
176         }