]>
Commit | Line | Data |
---|---|---|
01b06aed | 1 | import base64 |
5747d4f4 M |
2 | import json |
3 | import re | |
0d97ef43 NJ |
4 | |
5 | from .common import InfoExtractor | |
01b06aed | 6 | from ..compat import ( |
01b06aed | 7 | compat_urlparse, |
fac7e792 | 8 | compat_parse_qs, |
01b06aed | 9 | ) |
0d97ef43 | 10 | from ..utils import ( |
bdceea7a | 11 | clean_html, |
0d97ef43 | 12 | ExtractorError, |
e0ddbd02 | 13 | format_field, |
0d97ef43 | 14 | int_or_none, |
01b06aed | 15 | unsmuggle_url, |
81953d1a | 16 | smuggle_url, |
5747d4f4 | 17 | traverse_obj, |
58fb927e | 18 | remove_start |
0d97ef43 NJ |
19 | ) |
20 | ||
21 | ||
22 | class KalturaIE(InfoExtractor): | |
23 | _VALID_URL = r'''(?x) | |
ee3ec091 | 24 | (?: |
58fb927e | 25 | kaltura:(?P<partner_id>\w+):(?P<id>\w+)(?::(?P<player_type>\w+))?| |
ee3ec091 | 26 | https?:// |
ff24261b | 27 | (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com(?::\d+)?/ |
ee3ec091 S |
28 | (?: |
29 | (?: | |
30 | # flash player | |
37084f66 | 31 | index\.php/(?:kwidget|extwidget/preview)| |
b184f944 | 32 | # html5 player |
fac7e792 | 33 | html5/html5lib/[^/]+/mwEmbedFrame\.php |
ae6a8021 | 34 | ) |
fac7e792 | 35 | )(?:/(?P<path>[^?]+))?(?:\?(?P<query>.*))? |
ee3ec091 S |
36 | ) |
37 | ''' | |
81953d1a | 38 | _SERVICE_URL = 'http://cdnapi.kaltura.com' |
5747d4f4 | 39 | _SERVICE_BASE = '/api_v3/service/multirequest' |
92d4cfa3 S |
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 | } | |
0d97ef43 NJ |
46 | _TESTS = [ |
47 | { | |
48 | 'url': 'kaltura:269692:1_1jc2y3e4', | |
49 | 'md5': '3adcbdb3dcc02d647539e53f284ba171', | |
50 | 'info_dict': { | |
51 | 'id': '1_1jc2y3e4', | |
52 | 'ext': 'mp4', | |
bb4b8c57 | 53 | 'title': 'Straight from the Heart', |
0d97ef43 NJ |
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 | }, | |
58fb927e | 60 | 'skip': 'The access to this service is forbidden since the specified partner is blocked' |
0d97ef43 NJ |
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 | }, | |
bd3749ed S |
66 | { |
67 | 'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3', | |
68 | 'only_matching': True, | |
69 | }, | |
ef49b590 | 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, | |
1094074c RA |
73 | }, |
74 | { | |
75 | # video with subtitles | |
76 | 'url': 'kaltura:111032:1_cw786r8q', | |
77 | 'only_matching': True, | |
2c6acdfd S |
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 | }, | |
a01825a5 | 96 | 'skip': 'Gone. Maybe https://www.safaribooksonline.com/library/tutorials/introduction-to-python-anon/3469/', |
2c6acdfd S |
97 | 'params': { |
98 | 'skip_download': True, | |
99 | }, | |
37084f66 RA |
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, | |
ff24261b S |
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, | |
799756a3 S |
108 | }, |
109 | { | |
110 | # unavailable source format | |
111 | 'url': 'kaltura:513551:1_66x4rg7o', | |
112 | 'only_matching': True, | |
58fb927e JW |
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 | ] | |
ef49b590 | 187 | } |
0d97ef43 NJ |
188 | ] |
189 | ||
bfd973ec | 190 | @classmethod |
191 | def _extract_embed_urls(cls, url, webpage): | |
78280352 | 192 | # Embed codes: https://knowledge.kaltura.com/embedding-kaltura-media-players-your-site |
562de77f | 193 | finditer = ( |
41d1cca3 | 194 | list(re.finditer( |
9ea5c04c S |
195 | r"""(?xs) |
196 | kWidget\.(?:thumb)?[Ee]mbed\( | |
197 | \{.*? | |
78280352 S |
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*\}) | |
41d1cca3 | 202 | """, webpage)) |
203 | or list(re.finditer( | |
9ea5c04c | 204 | r'''(?xs) |
78280352 | 205 | (?P<q1>["']) |
ff24261b | 206 | (?:https?:)?//cdnapi(?:sec)?\.kaltura\.com(?::\d+)?/(?:(?!(?P=q1)).)*\b(?:p|partner_id)/(?P<partner_id>\d+)(?:(?!(?P=q1)).)* |
9ea5c04c S |
207 | (?P=q1).*? |
208 | (?: | |
69d69da9 S |
209 | (?: |
210 | entry_?[Ii]d| | |
211 | (?P<q2>["'])entry_?[Ii]d(?P=q2) | |
212 | )\s*:\s*| | |
c2f2f8b1 | 213 | \[\s*(?P<q2_1>["'])entry_?[Ii]d(?P=q2_1)\s*\]\s*=\s* |
69d69da9 | 214 | ) |
78280352 | 215 | (?P<q3>["'])(?P<id>(?:(?!(?P=q3)).)+)(?P=q3) |
41d1cca3 | 216 | ''', webpage)) |
217 | or list(re.finditer( | |
a01825a5 | 218 | r'''(?xs) |
41d1cca3 | 219 | <(?:iframe[^>]+src|meta[^>]+\bcontent)=(?P<q1>["'])\s* |
c21692fa | 220 | (?:https?:)?//(?:(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/(?:(?!(?P=q1)).)*\b(?:p|partner_id)/(?P<partner_id>\d+) |
a01825a5 | 221 | (?:(?!(?P=q1)).)* |
e30991f9 | 222 | [?&;]entry_id=(?P<id>(?:(?!(?P=q1))[^&])+) |
c21692fa | 223 | (?:(?!(?P=q1)).)* |
a01825a5 | 224 | (?P=q1) |
41d1cca3 | 225 | ''', webpage)) |
a01825a5 | 226 | ) |
562de77f S |
227 | urls = [] |
228 | for mobj in finditer: | |
81953d1a | 229 | embed_info = mobj.groupdict() |
5ca34598 | 230 | for k, v in embed_info.items(): |
1907f06e S |
231 | if v: |
232 | embed_info[k] = v.strip() | |
bfd973ec | 233 | embed_url = 'kaltura:%(partner_id)s:%(id)s' % embed_info |
d9163ae3 | 234 | escaped_pid = re.escape(embed_info['partner_id']) |
07b50f61 S |
235 | service_mobj = re.search( |
236 | r'<script[^>]+src=(["\'])(?P<id>(?:https?:)?//(?:(?!\1).)+)/p/%s/sp/%s00/embedIframeJs' % (escaped_pid, escaped_pid), | |
81953d1a | 237 | webpage) |
07b50f61 | 238 | if service_mobj: |
bfd973ec | 239 | embed_url = smuggle_url(embed_url, {'service_url': service_mobj.group('id')}) |
240 | urls.append(embed_url) | |
562de77f | 241 | return urls |
9ea5c04c | 242 | |
d9163ae3 | 243 | def _kaltura_api_call(self, video_id, actions, service_url=None, *args, **kwargs): |
0d97ef43 | 244 | params = actions[0] |
5747d4f4 | 245 | params.update({i: a for i, a in enumerate(actions[1:], start=1)}) |
0d97ef43 | 246 | |
81953d1a | 247 | data = self._download_json( |
d9163ae3 | 248 | (service_url or self._SERVICE_URL) + self._SERVICE_BASE, |
5747d4f4 M |
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)) | |
0d97ef43 | 261 | |
5747d4f4 | 262 | data[1] = traverse_obj(data, (1, 'objects', 0)) |
0d97ef43 NJ |
263 | |
264 | return data | |
265 | ||
58fb927e JW |
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): | |
0d97ef43 NJ |
274 | actions = [ |
275 | { | |
5747d4f4 M |
276 | 'apiVersion': '3.3.0', |
277 | 'clientTag': 'html5:v3.1.0', | |
0d97ef43 | 278 | 'format': 1, # JSON, 2 = XML, 3 = PHP |
5747d4f4 M |
279 | 'ks': '', |
280 | 'partnerId': partner_id, | |
1094074c RA |
281 | }, |
282 | { | |
283 | 'expiry': 86400, | |
284 | 'service': 'session', | |
285 | 'action': 'startWidgetSession', | |
58fb927e | 286 | 'widgetId': self._build_widget_id(partner_id), |
0d97ef43 | 287 | }, |
58fb927e | 288 | # info |
0d97ef43 | 289 | { |
5747d4f4 M |
290 | 'action': 'list', |
291 | 'filter': {'redirectFromEntryId': video_id}, | |
0d97ef43 | 292 | 'service': 'baseentry', |
1094074c | 293 | 'ks': '{1:result:ks}', |
5747d4f4 M |
294 | 'responseProfile': { |
295 | 'type': 1, | |
296 | 'fields': 'createdAt,dataUrl,duration,name,plays,thumbnailUrl,userId', | |
297 | }, | |
0d97ef43 | 298 | }, |
58fb927e | 299 | # flavor_assets |
0d97ef43 | 300 | { |
bb4b8c57 | 301 | 'action': 'getbyentryid', |
0d97ef43 | 302 | 'entryId': video_id, |
bb4b8c57 | 303 | 'service': 'flavorAsset', |
1094074c RA |
304 | 'ks': '{1:result:ks}', |
305 | }, | |
58fb927e | 306 | # captions |
1094074c RA |
307 | { |
308 | 'action': 'list', | |
309 | 'filter:entryIdEqual': video_id, | |
310 | 'service': 'caption_captionasset', | |
311 | 'ks': '{1:result:ks}', | |
0d97ef43 NJ |
312 | }, |
313 | ] | |
314 | return self._kaltura_api_call( | |
58fb927e JW |
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*=' | |
0d97ef43 NJ |
383 | |
384 | def _real_extract(self, url): | |
01b06aed S |
385 | url, smuggled_data = unsmuggle_url(url, {}) |
386 | ||
5ad28e7f | 387 | mobj = self._match_valid_url(url) |
58fb927e JW |
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' | |
fac7e792 | 392 | if partner_id and entry_id: |
58fb927e | 393 | _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id, smuggled_data.get('service_url'), player_type=player_type) |
fac7e792 | 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('/') | |
e5a2e17a | 403 | params.update(dict((zip(splitted_path[::2], [[v] for v in splitted_path[1::2]])))) |
fac7e792 | 404 | if 'wid' in params: |
58fb927e | 405 | partner_id = remove_start(params['wid'][0], '_') |
fac7e792 | 406 | elif 'p' in params: |
407 | partner_id = params['p'][0] | |
37084f66 RA |
408 | elif 'partner_id' in params: |
409 | partner_id = params['partner_id'][0] | |
fac7e792 | 410 | else: |
411 | raise ExtractorError('Invalid URL', expected=True) | |
412 | if 'entry_id' in params: | |
413 | entry_id = params['entry_id'][0] | |
58fb927e | 414 | _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id, player_type=player_type) |
fac7e792 | 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) | |
58fb927e JW |
418 | entry_data = self._search_json( |
419 | self.IFRAME_PACKAGE_DATA_REGEX, webpage, | |
420 | 'kalturaIframePackageData', reference_id)['entryResult'] | |
fac7e792 | 421 | info, flavor_assets = entry_data['meta'], entry_data['contextData']['flavorAssets'] |
422 | entry_id = info['id'] | |
a8094467 S |
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( | |
58fb927e | 428 | entry_id, partner_id, player_type=player_type) |
a8094467 S |
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 | |
58fb927e JW |
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}') | |
fac7e792 | 444 | else: |
445 | raise ExtractorError('Invalid URL', expected=True) | |
9dce3c09 | 446 | ks = params.get('flashvars[ks]', [None])[0] |
0d97ef43 | 447 | |
58fb927e JW |
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): | |
01b06aed S |
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 | ||
9dce3c09 S |
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 | ||
81953d1a RA |
466 | data_url = info['dataUrl'] |
467 | if '/flvclipper/' in data_url: | |
468 | data_url = re.sub(r'/flvclipper/.*', '/serveFlavor', data_url) | |
469 | ||
01b06aed | 470 | formats = [] |
3f047fc4 | 471 | subtitles = {} |
bb4b8c57 | 472 | for f in flavor_assets: |
473 | # Continue if asset is not ready | |
1094074c | 474 | if f.get('status') != 2: |
bb4b8c57 | 475 | continue |
e8bcd982 S |
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 | |
c0bddd6d | 480 | # DRM-protected video, cannot be decrypted |
a06916d9 | 481 | if not self.get_param('allow_unplayable_formats') and f.get('fileExt') == 'wvm': |
c0bddd6d | 482 | continue |
ab6f6aee | 483 | if not f.get('fileExt'): |
8ab7e6c4 | 484 | # QT indicates QuickTime; some videos have broken fileExt |
ab6f6aee S |
485 | if f.get('containerFormat') == 'qt': |
486 | f['fileExt'] = 'mov' | |
487 | else: | |
488 | f['fileExt'] = 'mp4' | |
81953d1a RA |
489 | video_url = sign_url( |
490 | '%s/flavorId/%s' % (data_url, f['id'])) | |
799756a3 S |
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 | |
1d16035b S |
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') | |
01b06aed | 500 | formats.append({ |
799756a3 | 501 | 'format_id': format_id, |
d80a39ce S |
502 | 'ext': f.get('fileExt'), |
503 | 'tbr': int_or_none(f['bitrate']), | |
504 | 'fps': int_or_none(f.get('frameRate')), | |
01b06aed S |
505 | 'filesize_approx': int_or_none(f.get('size'), invscale=1024), |
506 | 'container': f.get('containerFormat'), | |
1d16035b | 507 | 'vcodec': vcodec, |
d80a39ce S |
508 | 'height': int_or_none(f.get('height')), |
509 | 'width': int_or_none(f.get('width')), | |
01b06aed S |
510 | 'url': video_url, |
511 | }) | |
81953d1a RA |
512 | if '/playManifest/' in data_url: |
513 | m3u8_url = sign_url(data_url.replace( | |
514 | 'format/url', 'format/applehttp')) | |
3f047fc4 | 515 | fmts, subs = self._extract_m3u8_formats_and_subtitles( |
81953d1a | 516 | m3u8_url, entry_id, 'mp4', 'm3u8_native', |
3f047fc4 F |
517 | m3u8_id='hls', fatal=False) |
518 | formats.extend(fmts) | |
519 | self._merge_subtitles(subs, target=subtitles) | |
bb4b8c57 | 520 | |
1094074c RA |
521 | if captions: |
522 | for caption in captions.get('objects', []): | |
1094074c | 523 | # Continue if caption is not ready |
65547082 | 524 | if caption.get('status') != 2: |
1094074c | 525 | continue |
92d4cfa3 S |
526 | if not caption.get('id'): |
527 | continue | |
528 | caption_format = int_or_none(caption.get('format')) | |
1094074c RA |
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']), | |
92d4cfa3 | 531 | 'ext': caption.get('fileExt') or self._CAPTION_TYPES.get(caption_format) or 'ttml', |
1094074c RA |
532 | }) |
533 | ||
0d97ef43 | 534 | return { |
ee3ec091 | 535 | 'id': entry_id, |
0d97ef43 NJ |
536 | 'title': info['name'], |
537 | 'formats': formats, | |
1094074c | 538 | 'subtitles': subtitles, |
bdceea7a | 539 | 'description': clean_html(info.get('description')), |
0d97ef43 NJ |
540 | 'thumbnail': info.get('thumbnailUrl'), |
541 | 'duration': info.get('duration'), | |
542 | 'timestamp': info.get('createdAt'), | |
e0ddbd02 | 543 | 'uploader_id': format_field(info, 'userId', ignore=('None', None)), |
6d1b3489 | 544 | 'view_count': int_or_none(info.get('plays')), |
0d97ef43 | 545 | } |