]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/kaltura.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / kaltura.py
1 import base64
2 import json
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7 compat_parse_qs,
8 compat_urlparse,
9 )
10 from ..utils import (
11 ExtractorError,
12 clean_html,
13 format_field,
14 int_or_none,
15 remove_start,
16 smuggle_url,
17 traverse_obj,
18 unsmuggle_url,
19 )
20
21
22 class KalturaIE(InfoExtractor):
23 _VALID_URL = r'''(?x)
24 (?:
25 kaltura:(?P<partner_id>\w+):(?P<id>\w+)(?::(?P<player_type>\w+))?|
26 https?://
27 (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com(?::\d+)?/
28 (?:
29 (?:
30 # flash player
31 index\.php/(?:kwidget|extwidget/preview)|
32 # html5 player
33 html5/html5lib/[^/]+/mwEmbedFrame\.php
34 )
35 )(?:/(?P<path>[^?]+))?(?:\?(?P<query>.*))?
36 )
37 '''
38 _SERVICE_URL = 'http://cdnapi.kaltura.com'
39 _SERVICE_BASE = '/api_v3/service/multirequest'
40 # See https://github.com/kaltura/server/blob/master/plugins/content/caption/base/lib/model/enums/CaptionType.php
41 _CAPTION_TYPES = {
42 1: 'srt',
43 2: 'ttml',
44 3: 'vtt',
45 }
46 _TESTS = [
47 {
48 'url': 'kaltura:269692:1_1jc2y3e4',
49 'md5': '3adcbdb3dcc02d647539e53f284ba171',
50 'info_dict': {
51 'id': '1_1jc2y3e4',
52 'ext': 'mp4',
53 'title': 'Straight from the Heart',
54 'upload_date': '20131219',
55 'uploader_id': 'mlundberg@wolfgangsvault.com',
56 'description': 'The Allman Brothers Band, 12/16/1981',
57 'thumbnail': 're:^https?://.*/thumbnail/.*',
58 'timestamp': int,
59 },
60 'skip': 'The access to this service is forbidden since the specified partner is blocked'
61 },
62 {
63 'url': 'http://www.kaltura.com/index.php/kwidget/cache_st/1300318621/wid/_269692/uiconf_id/3873291/entry_id/1_1jc2y3e4',
64 'only_matching': True,
65 },
66 {
67 'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3',
68 'only_matching': True,
69 },
70 {
71 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.30.2/mwEmbedFrame.php/p/1337/uiconf_id/20540612/entry_id/1_sf5ovm7u?wid=_243342',
72 'only_matching': True,
73 },
74 {
75 # video with subtitles
76 'url': 'kaltura:111032:1_cw786r8q',
77 'only_matching': True,
78 },
79 {
80 # video with ttml subtitles (no fileExt)
81 'url': 'kaltura:1926081:0_l5ye1133',
82 'info_dict': {
83 'id': '0_l5ye1133',
84 'ext': 'mp4',
85 'title': 'What Can You Do With Python?',
86 'upload_date': '20160221',
87 'uploader_id': 'stork',
88 'thumbnail': 're:^https?://.*/thumbnail/.*',
89 'timestamp': int,
90 'subtitles': {
91 'en': [{
92 'ext': 'ttml',
93 }],
94 },
95 },
96 'skip': 'Gone. Maybe https://www.safaribooksonline.com/library/tutorials/introduction-to-python-anon/3469/',
97 'params': {
98 'skip_download': True,
99 },
100 },
101 {
102 'url': 'https://www.kaltura.com/index.php/extwidget/preview/partner_id/1770401/uiconf_id/37307382/entry_id/0_58u8kme7/embed/iframe?&flashvars[streamerType]=auto',
103 'only_matching': True,
104 },
105 {
106 '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',
107 'only_matching': True,
108 },
109 {
110 # unavailable source format
111 'url': 'kaltura:513551:1_66x4rg7o',
112 'only_matching': True,
113 },
114 {
115 # html5lib URL using kwidget player
116 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.46/mwEmbedFrame.php/p/691292/uiconf_id/20499062/entry_id/0_c076mna6?wid=_691292&iframeembed=true&playerId=kaltura_player_1420508608&entry_id=0_c076mna6&flashvars%5BakamaiHD.loadingPolicy%5D=preInitialize&flashvars%5BakamaiHD.asyncInit%5D=true&flashvars%5BstreamerType%5D=hdnetwork',
117 'info_dict': {
118 'id': '0_c076mna6',
119 'ext': 'mp4',
120 'title': 'md5:4883e7acbcbf42583a2dddc97dee4855',
121 'duration': 3608,
122 'uploader_id': 'commons@swinburne.edu.au',
123 'timestamp': 1408086874,
124 'view_count': int,
125 'upload_date': '20140815',
126 'thumbnail': 'http://cfvod.kaltura.com/p/691292/sp/69129200/thumbnail/entry_id/0_c076mna6/version/100022',
127 }
128 },
129 {
130 # html5lib playlist URL using kwidget player
131 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.89/mwEmbedFrame.php/p/2019031/uiconf_id/40436601?wid=1_4j3m32cv&iframeembed=true&playerId=kaltura_player_&flashvars[playlistAPI.kpl0Id]=1_jovey5nu&flashvars[ks]=&&flashvars[imageDefaultDuration]=30&flashvars[localizationCode]=en&flashvars[leadWithHTML5]=true&flashvars[forceMobileHTML5]=true&flashvars[nextPrevBtn.plugin]=true&flashvars[hotspots.plugin]=true&flashvars[sideBarContainer.plugin]=true&flashvars[sideBarContainer.position]=left&flashvars[sideBarContainer.clickToClose]=true&flashvars[chapters.plugin]=true&flashvars[chapters.layout]=vertical&flashvars[chapters.thumbnailRotator]=false&flashvars[streamSelector.plugin]=true&flashvars[EmbedPlayer.SpinnerTarget]=videoHolder&flashvars[dualScreen.plugin]=true&flashvars[playlistAPI.playlistUrl]=https://canvasgatechtest.kaf.kaltura.com/playlist/details/{playlistAPI.kpl0Id}/categoryid/126428551',
132 'info_dict': {
133 'id': '1_jovey5nu',
134 'title': '00-00 Introduction'
135 },
136 'playlist': [
137 {
138 'info_dict': {
139 'id': '1_b1y5hlvx',
140 'ext': 'mp4',
141 'title': 'CS7646_00-00 Introductio_Introduction',
142 'duration': 91,
143 'thumbnail': 'http://cfvod.kaltura.com/p/2019031/sp/201903100/thumbnail/entry_id/1_b1y5hlvx/version/100001',
144 'view_count': int,
145 'timestamp': 1533154447,
146 'upload_date': '20180801',
147 'uploader_id': 'djoyner3',
148 }
149 }, {
150 'info_dict': {
151 'id': '1_jfb7mdpn',
152 'ext': 'mp4',
153 'title': 'CS7646_00-00 Introductio_Three parts to the course',
154 'duration': 63,
155 'thumbnail': 'http://cfvod.kaltura.com/p/2019031/sp/201903100/thumbnail/entry_id/1_jfb7mdpn/version/100001',
156 'view_count': int,
157 'timestamp': 1533154489,
158 'upload_date': '20180801',
159 'uploader_id': 'djoyner3',
160 }
161 }, {
162 'info_dict': {
163 'id': '1_8xflxdp7',
164 'ext': 'mp4',
165 'title': 'CS7646_00-00 Introductio_Textbooks',
166 'duration': 37,
167 'thumbnail': 'http://cfvod.kaltura.com/p/2019031/sp/201903100/thumbnail/entry_id/1_8xflxdp7/version/100001',
168 'view_count': int,
169 'timestamp': 1533154512,
170 'upload_date': '20180801',
171 'uploader_id': 'djoyner3',
172 }
173 }, {
174 'info_dict': {
175 'id': '1_3hqew8kn',
176 'ext': 'mp4',
177 'title': 'CS7646_00-00 Introductio_Prerequisites',
178 'duration': 49,
179 'thumbnail': 'http://cfvod.kaltura.com/p/2019031/sp/201903100/thumbnail/entry_id/1_3hqew8kn/version/100001',
180 'view_count': int,
181 'timestamp': 1533154536,
182 'upload_date': '20180801',
183 'uploader_id': 'djoyner3',
184 }
185 }
186 ]
187 }
188 ]
189
190 @classmethod
191 def _extract_embed_urls(cls, url, webpage):
192 # Embed codes: https://knowledge.kaltura.com/embedding-kaltura-media-players-your-site
193 finditer = (
194 list(re.finditer(
195 r"""(?xs)
196 kWidget\.(?:thumb)?[Ee]mbed\(
197 \{.*?
198 (?P<q1>['"])wid(?P=q1)\s*:\s*
199 (?P<q2>['"])_?(?P<partner_id>(?:(?!(?P=q2)).)+)(?P=q2),.*?
200 (?P<q3>['"])entry_?[Ii]d(?P=q3)\s*:\s*
201 (?P<q4>['"])(?P<id>(?:(?!(?P=q4)).)+)(?P=q4)(?:,|\s*\})
202 """, webpage))
203 or list(re.finditer(
204 r'''(?xs)
205 (?P<q1>["'])
206 (?:https?:)?//cdnapi(?:sec)?\.kaltura\.com(?::\d+)?/(?:(?!(?P=q1)).)*\b(?:p|partner_id)/(?P<partner_id>\d+)(?:(?!(?P=q1)).)*
207 (?P=q1).*?
208 (?:
209 (?:
210 entry_?[Ii]d|
211 (?P<q2>["'])entry_?[Ii]d(?P=q2)
212 )\s*:\s*|
213 \[\s*(?P<q2_1>["'])entry_?[Ii]d(?P=q2_1)\s*\]\s*=\s*
214 )
215 (?P<q3>["'])(?P<id>(?:(?!(?P=q3)).)+)(?P=q3)
216 ''', webpage))
217 or list(re.finditer(
218 r'''(?xs)
219 <(?:iframe[^>]+src|meta[^>]+\bcontent)=(?P<q1>["'])\s*
220 (?:https?:)?//(?:(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/(?:(?!(?P=q1)).)*\b(?:p|partner_id)/(?P<partner_id>\d+)
221 (?:(?!(?P=q1)).)*
222 [?&;]entry_id=(?P<id>(?:(?!(?P=q1))[^&])+)
223 (?:(?!(?P=q1)).)*
224 (?P=q1)
225 ''', webpage))
226 )
227 urls = []
228 for mobj in finditer:
229 embed_info = mobj.groupdict()
230 for k, v in embed_info.items():
231 if v:
232 embed_info[k] = v.strip()
233 embed_url = 'kaltura:%(partner_id)s:%(id)s' % embed_info
234 escaped_pid = re.escape(embed_info['partner_id'])
235 service_mobj = re.search(
236 r'<script[^>]+src=(["\'])(?P<id>(?:https?:)?//(?:(?!\1).)+)/p/%s/sp/%s00/embedIframeJs' % (escaped_pid, escaped_pid),
237 webpage)
238 if service_mobj:
239 embed_url = smuggle_url(embed_url, {'service_url': service_mobj.group('id')})
240 urls.append(embed_url)
241 return urls
242
243 def _kaltura_api_call(self, video_id, actions, service_url=None, *args, **kwargs):
244 params = actions[0]
245 params.update({i: a for i, a in enumerate(actions[1:], start=1)})
246
247 data = self._download_json(
248 (service_url or self._SERVICE_URL) + self._SERVICE_BASE,
249 video_id, data=json.dumps(params).encode('utf-8'),
250 headers={
251 'Content-Type': 'application/json',
252 'Accept-Encoding': 'gzip, deflate, br',
253 }, *args, **kwargs)
254
255 for idx, status in enumerate(data):
256 if not isinstance(status, dict):
257 continue
258 if status.get('objectType') == 'KalturaAPIException':
259 raise ExtractorError(
260 '%s said: %s (%d)' % (self.IE_NAME, status['message'], idx))
261
262 data[1] = traverse_obj(data, (1, 'objects', 0))
263
264 return data
265
266 def _get_video_info(self, video_id, partner_id, service_url=None, player_type='html5'):
267 assert player_type in ('html5', 'kwidget')
268 if player_type == 'kwidget':
269 return self._get_video_info_kwidget(video_id, partner_id, service_url)
270
271 return self._get_video_info_html5(video_id, partner_id, service_url)
272
273 def _get_video_info_html5(self, video_id, partner_id, service_url=None):
274 actions = [
275 {
276 'apiVersion': '3.3.0',
277 'clientTag': 'html5:v3.1.0',
278 'format': 1, # JSON, 2 = XML, 3 = PHP
279 'ks': '',
280 'partnerId': partner_id,
281 },
282 {
283 'expiry': 86400,
284 'service': 'session',
285 'action': 'startWidgetSession',
286 'widgetId': self._build_widget_id(partner_id),
287 },
288 # info
289 {
290 'action': 'list',
291 'filter': {'redirectFromEntryId': video_id},
292 'service': 'baseentry',
293 'ks': '{1:result:ks}',
294 'responseProfile': {
295 'type': 1,
296 'fields': 'createdAt,dataUrl,duration,name,plays,thumbnailUrl,userId',
297 },
298 },
299 # flavor_assets
300 {
301 'action': 'getbyentryid',
302 'entryId': video_id,
303 'service': 'flavorAsset',
304 'ks': '{1:result:ks}',
305 },
306 # captions
307 {
308 'action': 'list',
309 'filter:entryIdEqual': video_id,
310 'service': 'caption_captionasset',
311 'ks': '{1:result:ks}',
312 },
313 ]
314 return self._kaltura_api_call(
315 video_id, actions, service_url, note='Downloading video info JSON (Kaltura html5 player)')
316
317 def _get_video_info_kwidget(self, video_id, partner_id, service_url=None):
318 actions = [
319 {
320 'service': 'multirequest',
321 'apiVersion': '3.1',
322 'expiry': 86400,
323 'clientTag': 'kwidget:v2.89',
324 'format': 1, # JSON, 2 = XML, 3 = PHP
325 'ignoreNull': 1,
326 'action': 'null',
327 },
328 # header
329 {
330 'expiry': 86400,
331 'service': 'session',
332 'action': 'startWidgetSession',
333 'widgetId': self._build_widget_id(partner_id),
334 },
335 # (empty)
336 {
337 'expiry': 86400,
338 'service': 'session',
339 'action': 'startwidgetsession',
340 'widgetId': self._build_widget_id(partner_id),
341 'format': 9,
342 'apiVersion': '3.1',
343 'clientTag': 'kwidget:v2.89',
344 'ignoreNull': 1,
345 'ks': '{1:result:ks}'
346 },
347 # info
348 {
349 'action': 'list',
350 'filter': {'redirectFromEntryId': video_id},
351 'service': 'baseentry',
352 'ks': '{1:result:ks}',
353 'responseProfile': {
354 'type': 1,
355 'fields': 'createdAt,dataUrl,duration,name,plays,thumbnailUrl,userId',
356 },
357 },
358 # flavor_assets
359 {
360 'action': 'getbyentryid',
361 'entryId': video_id,
362 'service': 'flavorAsset',
363 'ks': '{1:result:ks}',
364 },
365 # captions
366 {
367 'action': 'list',
368 'filter:entryIdEqual': video_id,
369 'service': 'caption_captionasset',
370 'ks': '{1:result:ks}',
371 },
372 ]
373 # second object (representing the second start widget session) is None
374 header, _, _info, flavor_assets, captions = self._kaltura_api_call(
375 video_id, actions, service_url, note='Downloading video info JSON (Kaltura kwidget player)')
376 info = _info['objects'][0]
377 return header, info, flavor_assets, captions
378
379 def _build_widget_id(self, partner_id):
380 return partner_id if '_' in partner_id else f'_{partner_id}'
381
382 IFRAME_PACKAGE_DATA_REGEX = r'window\.kalturaIframePackageData\s*='
383
384 def _real_extract(self, url):
385 url, smuggled_data = unsmuggle_url(url, {})
386
387 mobj = self._match_valid_url(url)
388 partner_id, entry_id, player_type = mobj.group('partner_id', 'id', 'player_type')
389 ks, captions = None, None
390 if not player_type:
391 player_type = 'kwidget' if 'html5lib/v2' in url else 'html5'
392 if partner_id and entry_id:
393 _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id, smuggled_data.get('service_url'), player_type=player_type)
394 else:
395 path, query = mobj.group('path', 'query')
396 if not path and not query:
397 raise ExtractorError('Invalid URL', expected=True)
398 params = {}
399 if query:
400 params = compat_parse_qs(query)
401 if path:
402 splitted_path = path.split('/')
403 params.update(dict((zip(splitted_path[::2], [[v] for v in splitted_path[1::2]]))))
404 if 'wid' in params:
405 partner_id = remove_start(params['wid'][0], '_')
406 elif 'p' in params:
407 partner_id = params['p'][0]
408 elif 'partner_id' in params:
409 partner_id = params['partner_id'][0]
410 else:
411 raise ExtractorError('Invalid URL', expected=True)
412 if 'entry_id' in params:
413 entry_id = params['entry_id'][0]
414 _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id, player_type=player_type)
415 elif 'uiconf_id' in params and 'flashvars[referenceId]' in params:
416 reference_id = params['flashvars[referenceId]'][0]
417 webpage = self._download_webpage(url, reference_id)
418 entry_data = self._search_json(
419 self.IFRAME_PACKAGE_DATA_REGEX, webpage,
420 'kalturaIframePackageData', reference_id)['entryResult']
421 info, flavor_assets = entry_data['meta'], entry_data['contextData']['flavorAssets']
422 entry_id = info['id']
423 # Unfortunately, data returned in kalturaIframePackageData lacks
424 # captions so we will try requesting the complete data using
425 # regular approach since we now know the entry_id
426 try:
427 _, info, flavor_assets, captions = self._get_video_info(
428 entry_id, partner_id, player_type=player_type)
429 except ExtractorError:
430 # Regular scenario failed but we already have everything
431 # extracted apart from captions and can process at least
432 # with this
433 pass
434 elif 'uiconf_id' in params and 'flashvars[playlistAPI.kpl0Id]' in params:
435 playlist_id = params['flashvars[playlistAPI.kpl0Id]'][0]
436 webpage = self._download_webpage(url, playlist_id)
437 playlist_data = self._search_json(
438 self.IFRAME_PACKAGE_DATA_REGEX, webpage,
439 'kalturaIframePackageData', playlist_id)['playlistResult']
440 return self.playlist_from_matches(
441 traverse_obj(playlist_data, (playlist_id, 'items', ..., 'id')),
442 playlist_id, traverse_obj(playlist_data, (playlist_id, 'name')),
443 ie=KalturaIE, getter=lambda x: f'kaltura:{partner_id}:{x}:{player_type}')
444 else:
445 raise ExtractorError('Invalid URL', expected=True)
446 ks = params.get('flashvars[ks]', [None])[0]
447
448 return self._per_video_extract(smuggled_data, entry_id, info, ks, flavor_assets, captions)
449
450 def _per_video_extract(self, smuggled_data, entry_id, info, ks, flavor_assets, captions):
451 source_url = smuggled_data.get('source_url')
452 if source_url:
453 referrer = base64.b64encode(
454 '://'.join(compat_urlparse.urlparse(source_url)[:2])
455 .encode('utf-8')).decode('utf-8')
456 else:
457 referrer = None
458
459 def sign_url(unsigned_url):
460 if ks:
461 unsigned_url += '/ks/%s' % ks
462 if referrer:
463 unsigned_url += '?referrer=%s' % referrer
464 return unsigned_url
465
466 data_url = info['dataUrl']
467 if '/flvclipper/' in data_url:
468 data_url = re.sub(r'/flvclipper/.*', '/serveFlavor', data_url)
469
470 formats = []
471 subtitles = {}
472 for f in flavor_assets:
473 # Continue if asset is not ready
474 if f.get('status') != 2:
475 continue
476 # Original format that's not available (e.g. kaltura:1926081:0_c03e1b5g)
477 # skip for now.
478 if f.get('fileExt') == 'chun':
479 continue
480 # DRM-protected video, cannot be decrypted
481 if not self.get_param('allow_unplayable_formats') and f.get('fileExt') == 'wvm':
482 continue
483 if not f.get('fileExt'):
484 # QT indicates QuickTime; some videos have broken fileExt
485 if f.get('containerFormat') == 'qt':
486 f['fileExt'] = 'mov'
487 else:
488 f['fileExt'] = 'mp4'
489 video_url = sign_url(
490 '%s/flavorId/%s' % (data_url, f['id']))
491 format_id = '%(fileExt)s-%(bitrate)s' % f
492 # Source format may not be available (e.g. kaltura:513551:1_66x4rg7o)
493 if f.get('isOriginal') is True and not self._is_valid_url(
494 video_url, entry_id, format_id):
495 continue
496 # audio-only has no videoCodecId (e.g. kaltura:1926081:0_c03e1b5g
497 # -f mp4-56)
498 vcodec = 'none' if 'videoCodecId' not in f and f.get(
499 'frameRate') == 0 else f.get('videoCodecId')
500 formats.append({
501 'format_id': format_id,
502 'ext': f.get('fileExt'),
503 'tbr': int_or_none(f['bitrate']),
504 'fps': int_or_none(f.get('frameRate')),
505 'filesize_approx': int_or_none(f.get('size'), invscale=1024),
506 'container': f.get('containerFormat'),
507 'vcodec': vcodec,
508 'height': int_or_none(f.get('height')),
509 'width': int_or_none(f.get('width')),
510 'url': video_url,
511 })
512 if '/playManifest/' in data_url:
513 m3u8_url = sign_url(data_url.replace(
514 'format/url', 'format/applehttp'))
515 fmts, subs = self._extract_m3u8_formats_and_subtitles(
516 m3u8_url, entry_id, 'mp4', 'm3u8_native',
517 m3u8_id='hls', fatal=False)
518 formats.extend(fmts)
519 self._merge_subtitles(subs, target=subtitles)
520
521 if captions:
522 for caption in captions.get('objects', []):
523 # Continue if caption is not ready
524 if caption.get('status') != 2:
525 continue
526 if not caption.get('id'):
527 continue
528 caption_format = int_or_none(caption.get('format'))
529 subtitles.setdefault(caption.get('languageCode') or caption.get('language'), []).append({
530 'url': '%s/api_v3/service/caption_captionasset/action/serve/captionAssetId/%s' % (self._SERVICE_URL, caption['id']),
531 'ext': caption.get('fileExt') or self._CAPTION_TYPES.get(caption_format) or 'ttml',
532 })
533
534 return {
535 'id': entry_id,
536 'title': info['name'],
537 'formats': formats,
538 'subtitles': subtitles,
539 'description': clean_html(info.get('description')),
540 'thumbnail': info.get('thumbnailUrl'),
541 'duration': info.get('duration'),
542 'timestamp': info.get('createdAt'),
543 'uploader_id': format_field(info, 'userId', ignore=('None', None)),
544 'view_count': int_or_none(info.get('plays')),
545 }