]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/kaltura.py
Update upstream source from tag 'upstream/2020.06.16'
[youtubedl] / 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_urlparse,
10 compat_parse_qs,
11 )
12 from ..utils import (
13 clean_html,
14 ExtractorError,
15 int_or_none,
16 unsmuggle_url,
17 smuggle_url,
18 )
19
20
21 class KalturaIE(InfoExtractor):
22 _VALID_URL = r'''(?x)
23 (?:
24 kaltura:(?P<partner_id>\d+):(?P<id>[0-9a-z_]+)|
25 https?://
26 (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com(?::\d+)?/
27 (?:
28 (?:
29 # flash player
30 index\.php/(?:kwidget|extwidget/preview)|
31 # html5 player
32 html5/html5lib/[^/]+/mwEmbedFrame\.php
33 )
34 )(?:/(?P<path>[^?]+))?(?:\?(?P<query>.*))?
35 )
36 '''
37 _SERVICE_URL = 'http://cdnapi.kaltura.com'
38 _SERVICE_BASE = '/api_v3/index.php'
39 # See https://github.com/kaltura/server/blob/master/plugins/content/caption/base/lib/model/enums/CaptionType.php
40 _CAPTION_TYPES = {
41 1: 'srt',
42 2: 'ttml',
43 3: 'vtt',
44 }
45 _TESTS = [
46 {
47 'url': 'kaltura:269692:1_1jc2y3e4',
48 'md5': '3adcbdb3dcc02d647539e53f284ba171',
49 'info_dict': {
50 'id': '1_1jc2y3e4',
51 'ext': 'mp4',
52 'title': 'Straight from the Heart',
53 'upload_date': '20131219',
54 'uploader_id': 'mlundberg@wolfgangsvault.com',
55 'description': 'The Allman Brothers Band, 12/16/1981',
56 'thumbnail': 're:^https?://.*/thumbnail/.*',
57 'timestamp': int,
58 },
59 },
60 {
61 'url': 'http://www.kaltura.com/index.php/kwidget/cache_st/1300318621/wid/_269692/uiconf_id/3873291/entry_id/1_1jc2y3e4',
62 'only_matching': True,
63 },
64 {
65 'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3',
66 'only_matching': True,
67 },
68 {
69 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.30.2/mwEmbedFrame.php/p/1337/uiconf_id/20540612/entry_id/1_sf5ovm7u?wid=_243342',
70 'only_matching': True,
71 },
72 {
73 # video with subtitles
74 'url': 'kaltura:111032:1_cw786r8q',
75 'only_matching': True,
76 },
77 {
78 # video with ttml subtitles (no fileExt)
79 'url': 'kaltura:1926081:0_l5ye1133',
80 'info_dict': {
81 'id': '0_l5ye1133',
82 'ext': 'mp4',
83 'title': 'What Can You Do With Python?',
84 'upload_date': '20160221',
85 'uploader_id': 'stork',
86 'thumbnail': 're:^https?://.*/thumbnail/.*',
87 'timestamp': int,
88 'subtitles': {
89 'en': [{
90 'ext': 'ttml',
91 }],
92 },
93 },
94 'skip': 'Gone. Maybe https://www.safaribooksonline.com/library/tutorials/introduction-to-python-anon/3469/',
95 'params': {
96 'skip_download': True,
97 },
98 },
99 {
100 'url': 'https://www.kaltura.com/index.php/extwidget/preview/partner_id/1770401/uiconf_id/37307382/entry_id/0_58u8kme7/embed/iframe?&flashvars[streamerType]=auto',
101 'only_matching': True,
102 },
103 {
104 'url': 'https://www.kaltura.com:443/index.php/extwidget/preview/partner_id/1770401/uiconf_id/37307382/entry_id/0_58u8kme7/embed/iframe?&flashvars[streamerType]=auto',
105 'only_matching': True,
106 },
107 {
108 # unavailable source format
109 'url': 'kaltura:513551:1_66x4rg7o',
110 'only_matching': True,
111 }
112 ]
113
114 @staticmethod
115 def _extract_url(webpage):
116 urls = KalturaIE._extract_urls(webpage)
117 return urls[0] if urls else None
118
119 @staticmethod
120 def _extract_urls(webpage):
121 # Embed codes: https://knowledge.kaltura.com/embedding-kaltura-media-players-your-site
122 finditer = (
123 re.finditer(
124 r"""(?xs)
125 kWidget\.(?:thumb)?[Ee]mbed\(
126 \{.*?
127 (?P<q1>['"])wid(?P=q1)\s*:\s*
128 (?P<q2>['"])_?(?P<partner_id>(?:(?!(?P=q2)).)+)(?P=q2),.*?
129 (?P<q3>['"])entry_?[Ii]d(?P=q3)\s*:\s*
130 (?P<q4>['"])(?P<id>(?:(?!(?P=q4)).)+)(?P=q4)(?:,|\s*\})
131 """, webpage)
132 or re.finditer(
133 r'''(?xs)
134 (?P<q1>["'])
135 (?:https?:)?//cdnapi(?:sec)?\.kaltura\.com(?::\d+)?/(?:(?!(?P=q1)).)*\b(?:p|partner_id)/(?P<partner_id>\d+)(?:(?!(?P=q1)).)*
136 (?P=q1).*?
137 (?:
138 (?:
139 entry_?[Ii]d|
140 (?P<q2>["'])entry_?[Ii]d(?P=q2)
141 )\s*:\s*|
142 \[\s*(?P<q2_1>["'])entry_?[Ii]d(?P=q2_1)\s*\]\s*=\s*
143 )
144 (?P<q3>["'])(?P<id>(?:(?!(?P=q3)).)+)(?P=q3)
145 ''', webpage)
146 or re.finditer(
147 r'''(?xs)
148 <(?:iframe[^>]+src|meta[^>]+\bcontent)=(?P<q1>["'])
149 (?:https?:)?//(?:(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/(?:(?!(?P=q1)).)*\b(?:p|partner_id)/(?P<partner_id>\d+)
150 (?:(?!(?P=q1)).)*
151 [?&;]entry_id=(?P<id>(?:(?!(?P=q1))[^&])+)
152 (?:(?!(?P=q1)).)*
153 (?P=q1)
154 ''', webpage)
155 )
156 urls = []
157 for mobj in finditer:
158 embed_info = mobj.groupdict()
159 for k, v in embed_info.items():
160 if v:
161 embed_info[k] = v.strip()
162 url = 'kaltura:%(partner_id)s:%(id)s' % embed_info
163 escaped_pid = re.escape(embed_info['partner_id'])
164 service_mobj = re.search(
165 r'<script[^>]+src=(["\'])(?P<id>(?:https?:)?//(?:(?!\1).)+)/p/%s/sp/%s00/embedIframeJs' % (escaped_pid, escaped_pid),
166 webpage)
167 if service_mobj:
168 url = smuggle_url(url, {'service_url': service_mobj.group('id')})
169 urls.append(url)
170 return urls
171
172 def _kaltura_api_call(self, video_id, actions, service_url=None, *args, **kwargs):
173 params = actions[0]
174 if len(actions) > 1:
175 for i, a in enumerate(actions[1:], start=1):
176 for k, v in a.items():
177 params['%d:%s' % (i, k)] = v
178
179 data = self._download_json(
180 (service_url or self._SERVICE_URL) + self._SERVICE_BASE,
181 video_id, query=params, *args, **kwargs)
182
183 status = data if len(actions) == 1 else data[0]
184 if status.get('objectType') == 'KalturaAPIException':
185 raise ExtractorError(
186 '%s said: %s' % (self.IE_NAME, status['message']))
187
188 return data
189
190 def _get_video_info(self, video_id, partner_id, service_url=None):
191 actions = [
192 {
193 'action': 'null',
194 'apiVersion': '3.1.5',
195 'clientTag': 'kdp:v3.8.5',
196 'format': 1, # JSON, 2 = XML, 3 = PHP
197 'service': 'multirequest',
198 },
199 {
200 'expiry': 86400,
201 'service': 'session',
202 'action': 'startWidgetSession',
203 'widgetId': '_%s' % partner_id,
204 },
205 {
206 'action': 'get',
207 'entryId': video_id,
208 'service': 'baseentry',
209 'ks': '{1:result:ks}',
210 'responseProfile:fields': 'createdAt,dataUrl,duration,name,plays,thumbnailUrl,userId',
211 'responseProfile:type': 1,
212 },
213 {
214 'action': 'getbyentryid',
215 'entryId': video_id,
216 'service': 'flavorAsset',
217 'ks': '{1:result:ks}',
218 },
219 {
220 'action': 'list',
221 'filter:entryIdEqual': video_id,
222 'service': 'caption_captionasset',
223 'ks': '{1:result:ks}',
224 },
225 ]
226 return self._kaltura_api_call(
227 video_id, actions, service_url, note='Downloading video info JSON')
228
229 def _real_extract(self, url):
230 url, smuggled_data = unsmuggle_url(url, {})
231
232 mobj = re.match(self._VALID_URL, url)
233 partner_id, entry_id = mobj.group('partner_id', 'id')
234 ks = None
235 captions = None
236 if partner_id and entry_id:
237 _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id, smuggled_data.get('service_url'))
238 else:
239 path, query = mobj.group('path', 'query')
240 if not path and not query:
241 raise ExtractorError('Invalid URL', expected=True)
242 params = {}
243 if query:
244 params = compat_parse_qs(query)
245 if path:
246 splitted_path = path.split('/')
247 params.update(dict((zip(splitted_path[::2], [[v] for v in splitted_path[1::2]]))))
248 if 'wid' in params:
249 partner_id = params['wid'][0][1:]
250 elif 'p' in params:
251 partner_id = params['p'][0]
252 elif 'partner_id' in params:
253 partner_id = params['partner_id'][0]
254 else:
255 raise ExtractorError('Invalid URL', expected=True)
256 if 'entry_id' in params:
257 entry_id = params['entry_id'][0]
258 _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id)
259 elif 'uiconf_id' in params and 'flashvars[referenceId]' in params:
260 reference_id = params['flashvars[referenceId]'][0]
261 webpage = self._download_webpage(url, reference_id)
262 entry_data = self._parse_json(self._search_regex(
263 r'window\.kalturaIframePackageData\s*=\s*({.*});',
264 webpage, 'kalturaIframePackageData'),
265 reference_id)['entryResult']
266 info, flavor_assets = entry_data['meta'], entry_data['contextData']['flavorAssets']
267 entry_id = info['id']
268 # Unfortunately, data returned in kalturaIframePackageData lacks
269 # captions so we will try requesting the complete data using
270 # regular approach since we now know the entry_id
271 try:
272 _, info, flavor_assets, captions = self._get_video_info(
273 entry_id, partner_id)
274 except ExtractorError:
275 # Regular scenario failed but we already have everything
276 # extracted apart from captions and can process at least
277 # with this
278 pass
279 else:
280 raise ExtractorError('Invalid URL', expected=True)
281 ks = params.get('flashvars[ks]', [None])[0]
282
283 source_url = smuggled_data.get('source_url')
284 if source_url:
285 referrer = base64.b64encode(
286 '://'.join(compat_urlparse.urlparse(source_url)[:2])
287 .encode('utf-8')).decode('utf-8')
288 else:
289 referrer = None
290
291 def sign_url(unsigned_url):
292 if ks:
293 unsigned_url += '/ks/%s' % ks
294 if referrer:
295 unsigned_url += '?referrer=%s' % referrer
296 return unsigned_url
297
298 data_url = info['dataUrl']
299 if '/flvclipper/' in data_url:
300 data_url = re.sub(r'/flvclipper/.*', '/serveFlavor', data_url)
301
302 formats = []
303 for f in flavor_assets:
304 # Continue if asset is not ready
305 if f.get('status') != 2:
306 continue
307 # Original format that's not available (e.g. kaltura:1926081:0_c03e1b5g)
308 # skip for now.
309 if f.get('fileExt') == 'chun':
310 continue
311 # DRM-protected video, cannot be decrypted
312 if f.get('fileExt') == 'wvm':
313 continue
314 if not f.get('fileExt'):
315 # QT indicates QuickTime; some videos have broken fileExt
316 if f.get('containerFormat') == 'qt':
317 f['fileExt'] = 'mov'
318 else:
319 f['fileExt'] = 'mp4'
320 video_url = sign_url(
321 '%s/flavorId/%s' % (data_url, f['id']))
322 format_id = '%(fileExt)s-%(bitrate)s' % f
323 # Source format may not be available (e.g. kaltura:513551:1_66x4rg7o)
324 if f.get('isOriginal') is True and not self._is_valid_url(
325 video_url, entry_id, format_id):
326 continue
327 # audio-only has no videoCodecId (e.g. kaltura:1926081:0_c03e1b5g
328 # -f mp4-56)
329 vcodec = 'none' if 'videoCodecId' not in f and f.get(
330 'frameRate') == 0 else f.get('videoCodecId')
331 formats.append({
332 'format_id': format_id,
333 'ext': f.get('fileExt'),
334 'tbr': int_or_none(f['bitrate']),
335 'fps': int_or_none(f.get('frameRate')),
336 'filesize_approx': int_or_none(f.get('size'), invscale=1024),
337 'container': f.get('containerFormat'),
338 'vcodec': vcodec,
339 'height': int_or_none(f.get('height')),
340 'width': int_or_none(f.get('width')),
341 'url': video_url,
342 })
343 if '/playManifest/' in data_url:
344 m3u8_url = sign_url(data_url.replace(
345 'format/url', 'format/applehttp'))
346 formats.extend(self._extract_m3u8_formats(
347 m3u8_url, entry_id, 'mp4', 'm3u8_native',
348 m3u8_id='hls', fatal=False))
349
350 self._sort_formats(formats)
351
352 subtitles = {}
353 if captions:
354 for caption in captions.get('objects', []):
355 # Continue if caption is not ready
356 if caption.get('status') != 2:
357 continue
358 if not caption.get('id'):
359 continue
360 caption_format = int_or_none(caption.get('format'))
361 subtitles.setdefault(caption.get('languageCode') or caption.get('language'), []).append({
362 'url': '%s/api_v3/service/caption_captionasset/action/serve/captionAssetId/%s' % (self._SERVICE_URL, caption['id']),
363 'ext': caption.get('fileExt') or self._CAPTION_TYPES.get(caption_format) or 'ttml',
364 })
365
366 return {
367 'id': entry_id,
368 'title': info['name'],
369 'formats': formats,
370 'subtitles': subtitles,
371 'description': clean_html(info.get('description')),
372 'thumbnail': info.get('thumbnailUrl'),
373 'duration': info.get('duration'),
374 'timestamp': info.get('createdAt'),
375 'uploader_id': info.get('userId') if info.get('userId') != 'None' else None,
376 'view_count': info.get('plays'),
377 }