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