]>
Commit | Line | Data |
---|---|---|
0d97ef43 NJ |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import re | |
01b06aed | 5 | import base64 |
0d97ef43 NJ |
6 | |
7 | from .common import InfoExtractor | |
01b06aed | 8 | from ..compat import ( |
01b06aed | 9 | compat_urlparse, |
fac7e792 | 10 | compat_parse_qs, |
01b06aed | 11 | ) |
0d97ef43 | 12 | from ..utils import ( |
bdceea7a | 13 | clean_html, |
0d97ef43 NJ |
14 | ExtractorError, |
15 | int_or_none, | |
01b06aed | 16 | unsmuggle_url, |
81953d1a | 17 | smuggle_url, |
0d97ef43 NJ |
18 | ) |
19 | ||
20 | ||
21 | class KalturaIE(InfoExtractor): | |
22 | _VALID_URL = r'''(?x) | |
ee3ec091 | 23 | (?: |
fac7e792 | 24 | kaltura:(?P<partner_id>\d+):(?P<id>[0-9a-z_]+)| |
ee3ec091 | 25 | https?:// |
ff24261b | 26 | (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com(?::\d+)?/ |
ee3ec091 S |
27 | (?: |
28 | (?: | |
29 | # flash player | |
37084f66 | 30 | index\.php/(?:kwidget|extwidget/preview)| |
b184f944 | 31 | # html5 player |
fac7e792 | 32 | html5/html5lib/[^/]+/mwEmbedFrame\.php |
ae6a8021 | 33 | ) |
fac7e792 | 34 | )(?:/(?P<path>[^?]+))?(?:\?(?P<query>.*))? |
ee3ec091 S |
35 | ) |
36 | ''' | |
81953d1a RA |
37 | _SERVICE_URL = 'http://cdnapi.kaltura.com' |
38 | _SERVICE_BASE = '/api_v3/index.php' | |
92d4cfa3 S |
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 | } | |
0d97ef43 NJ |
45 | _TESTS = [ |
46 | { | |
47 | 'url': 'kaltura:269692:1_1jc2y3e4', | |
48 | 'md5': '3adcbdb3dcc02d647539e53f284ba171', | |
49 | 'info_dict': { | |
50 | 'id': '1_1jc2y3e4', | |
51 | 'ext': 'mp4', | |
bb4b8c57 | 52 | 'title': 'Straight from the Heart', |
0d97ef43 NJ |
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 | }, | |
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, | |
ef49b590 | 106 | } |
0d97ef43 NJ |
107 | ] |
108 | ||
9ea5c04c S |
109 | @staticmethod |
110 | def _extract_url(webpage): | |
78280352 | 111 | # Embed codes: https://knowledge.kaltura.com/embedding-kaltura-media-players-your-site |
9ea5c04c S |
112 | mobj = ( |
113 | re.search( | |
114 | r"""(?xs) | |
115 | kWidget\.(?:thumb)?[Ee]mbed\( | |
116 | \{.*? | |
78280352 S |
117 | (?P<q1>['"])wid(?P=q1)\s*:\s* |
118 | (?P<q2>['"])_?(?P<partner_id>(?:(?!(?P=q2)).)+)(?P=q2),.*? | |
119 | (?P<q3>['"])entry_?[Ii]d(?P=q3)\s*:\s* | |
120 | (?P<q4>['"])(?P<id>(?:(?!(?P=q4)).)+)(?P=q4)(?:,|\s*\}) | |
9ea5c04c S |
121 | """, webpage) or |
122 | re.search( | |
123 | r'''(?xs) | |
78280352 | 124 | (?P<q1>["']) |
ff24261b | 125 | (?:https?:)?//cdnapi(?:sec)?\.kaltura\.com(?::\d+)?/(?:(?!(?P=q1)).)*\b(?:p|partner_id)/(?P<partner_id>\d+)(?:(?!(?P=q1)).)* |
9ea5c04c S |
126 | (?P=q1).*? |
127 | (?: | |
128 | entry_?[Ii]d| | |
78280352 | 129 | (?P<q2>["'])entry_?[Ii]d(?P=q2) |
9ea5c04c | 130 | )\s*:\s* |
78280352 | 131 | (?P<q3>["'])(?P<id>(?:(?!(?P=q3)).)+)(?P=q3) |
a01825a5 JH |
132 | ''', webpage) or |
133 | re.search( | |
134 | r'''(?xs) | |
78280352 S |
135 | <iframe[^>]+src=(?P<q1>["']) |
136 | (?:https?:)?//(?:www\.)?kaltura\.com/(?:(?!(?P=q1)).)*\b(?:p|partner_id)/(?P<partner_id>\d+) | |
a01825a5 | 137 | (?:(?!(?P=q1)).)* |
78280352 | 138 | [?&]entry_id=(?P<id>(?:(?!(?P=q1))[^&])+) |
a01825a5 JH |
139 | (?P=q1) |
140 | ''', webpage) | |
141 | ) | |
9ea5c04c | 142 | if mobj: |
81953d1a RA |
143 | embed_info = mobj.groupdict() |
144 | url = 'kaltura:%(partner_id)s:%(id)s' % embed_info | |
d9163ae3 | 145 | escaped_pid = re.escape(embed_info['partner_id']) |
81953d1a | 146 | service_url = re.search( |
d9163ae3 | 147 | r'<script[^>]+src=["\']((?:https?:)?//.+?)/p/%s/sp/%s00/embedIframeJs' % (escaped_pid, escaped_pid), |
81953d1a RA |
148 | webpage) |
149 | if service_url: | |
150 | url = smuggle_url(url, {'service_url': service_url.group(1)}) | |
151 | return url | |
9ea5c04c | 152 | |
d9163ae3 | 153 | def _kaltura_api_call(self, video_id, actions, service_url=None, *args, **kwargs): |
0d97ef43 NJ |
154 | params = actions[0] |
155 | if len(actions) > 1: | |
156 | for i, a in enumerate(actions[1:], start=1): | |
157 | for k, v in a.items(): | |
158 | params['%d:%s' % (i, k)] = v | |
159 | ||
81953d1a | 160 | data = self._download_json( |
d9163ae3 | 161 | (service_url or self._SERVICE_URL) + self._SERVICE_BASE, |
81953d1a | 162 | video_id, query=params, *args, **kwargs) |
0d97ef43 NJ |
163 | |
164 | status = data if len(actions) == 1 else data[0] | |
165 | if status.get('objectType') == 'KalturaAPIException': | |
166 | raise ExtractorError( | |
167 | '%s said: %s' % (self.IE_NAME, status['message'])) | |
168 | ||
169 | return data | |
170 | ||
d9163ae3 | 171 | def _get_video_info(self, video_id, partner_id, service_url=None): |
0d97ef43 NJ |
172 | actions = [ |
173 | { | |
174 | 'action': 'null', | |
175 | 'apiVersion': '3.1.5', | |
176 | 'clientTag': 'kdp:v3.8.5', | |
177 | 'format': 1, # JSON, 2 = XML, 3 = PHP | |
178 | 'service': 'multirequest', | |
1094074c RA |
179 | }, |
180 | { | |
181 | 'expiry': 86400, | |
182 | 'service': 'session', | |
183 | 'action': 'startWidgetSession', | |
184 | 'widgetId': '_%s' % partner_id, | |
0d97ef43 NJ |
185 | }, |
186 | { | |
187 | 'action': 'get', | |
188 | 'entryId': video_id, | |
189 | 'service': 'baseentry', | |
1094074c | 190 | 'ks': '{1:result:ks}', |
0d97ef43 NJ |
191 | }, |
192 | { | |
bb4b8c57 | 193 | 'action': 'getbyentryid', |
0d97ef43 | 194 | 'entryId': video_id, |
bb4b8c57 | 195 | 'service': 'flavorAsset', |
1094074c RA |
196 | 'ks': '{1:result:ks}', |
197 | }, | |
198 | { | |
199 | 'action': 'list', | |
200 | 'filter:entryIdEqual': video_id, | |
201 | 'service': 'caption_captionasset', | |
202 | 'ks': '{1:result:ks}', | |
0d97ef43 NJ |
203 | }, |
204 | ] | |
205 | return self._kaltura_api_call( | |
d9163ae3 | 206 | video_id, actions, service_url, note='Downloading video info JSON') |
0d97ef43 NJ |
207 | |
208 | def _real_extract(self, url): | |
01b06aed S |
209 | url, smuggled_data = unsmuggle_url(url, {}) |
210 | ||
0d97ef43 | 211 | mobj = re.match(self._VALID_URL, url) |
fac7e792 | 212 | partner_id, entry_id = mobj.group('partner_id', 'id') |
9dce3c09 | 213 | ks = None |
1094074c | 214 | captions = None |
fac7e792 | 215 | if partner_id and entry_id: |
1094074c | 216 | _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id, smuggled_data.get('service_url')) |
fac7e792 | 217 | else: |
218 | path, query = mobj.group('path', 'query') | |
219 | if not path and not query: | |
220 | raise ExtractorError('Invalid URL', expected=True) | |
221 | params = {} | |
222 | if query: | |
223 | params = compat_parse_qs(query) | |
224 | if path: | |
225 | splitted_path = path.split('/') | |
e5a2e17a | 226 | params.update(dict((zip(splitted_path[::2], [[v] for v in splitted_path[1::2]])))) |
fac7e792 | 227 | if 'wid' in params: |
228 | partner_id = params['wid'][0][1:] | |
229 | elif 'p' in params: | |
230 | partner_id = params['p'][0] | |
37084f66 RA |
231 | elif 'partner_id' in params: |
232 | partner_id = params['partner_id'][0] | |
fac7e792 | 233 | else: |
234 | raise ExtractorError('Invalid URL', expected=True) | |
235 | if 'entry_id' in params: | |
236 | entry_id = params['entry_id'][0] | |
1094074c | 237 | _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id) |
fac7e792 | 238 | elif 'uiconf_id' in params and 'flashvars[referenceId]' in params: |
239 | reference_id = params['flashvars[referenceId]'][0] | |
240 | webpage = self._download_webpage(url, reference_id) | |
241 | entry_data = self._parse_json(self._search_regex( | |
242 | r'window\.kalturaIframePackageData\s*=\s*({.*});', | |
243 | webpage, 'kalturaIframePackageData'), | |
244 | reference_id)['entryResult'] | |
245 | info, flavor_assets = entry_data['meta'], entry_data['contextData']['flavorAssets'] | |
246 | entry_id = info['id'] | |
a8094467 S |
247 | # Unfortunately, data returned in kalturaIframePackageData lacks |
248 | # captions so we will try requesting the complete data using | |
249 | # regular approach since we now know the entry_id | |
250 | try: | |
251 | _, info, flavor_assets, captions = self._get_video_info( | |
252 | entry_id, partner_id) | |
253 | except ExtractorError: | |
254 | # Regular scenario failed but we already have everything | |
255 | # extracted apart from captions and can process at least | |
256 | # with this | |
257 | pass | |
fac7e792 | 258 | else: |
259 | raise ExtractorError('Invalid URL', expected=True) | |
9dce3c09 | 260 | ks = params.get('flashvars[ks]', [None])[0] |
0d97ef43 | 261 | |
01b06aed S |
262 | source_url = smuggled_data.get('source_url') |
263 | if source_url: | |
264 | referrer = base64.b64encode( | |
265 | '://'.join(compat_urlparse.urlparse(source_url)[:2]) | |
266 | .encode('utf-8')).decode('utf-8') | |
267 | else: | |
268 | referrer = None | |
269 | ||
9dce3c09 S |
270 | def sign_url(unsigned_url): |
271 | if ks: | |
272 | unsigned_url += '/ks/%s' % ks | |
273 | if referrer: | |
274 | unsigned_url += '?referrer=%s' % referrer | |
275 | return unsigned_url | |
276 | ||
81953d1a RA |
277 | data_url = info['dataUrl'] |
278 | if '/flvclipper/' in data_url: | |
279 | data_url = re.sub(r'/flvclipper/.*', '/serveFlavor', data_url) | |
280 | ||
01b06aed | 281 | formats = [] |
bb4b8c57 | 282 | for f in flavor_assets: |
283 | # Continue if asset is not ready | |
1094074c | 284 | if f.get('status') != 2: |
bb4b8c57 | 285 | continue |
e8bcd982 S |
286 | # Original format that's not available (e.g. kaltura:1926081:0_c03e1b5g) |
287 | # skip for now. | |
288 | if f.get('fileExt') == 'chun': | |
289 | continue | |
c0bddd6d | 290 | # DRM-protected video, cannot be decrypted |
291 | if f.get('fileExt') == 'wvm': | |
292 | continue | |
ab6f6aee | 293 | if not f.get('fileExt'): |
8ab7e6c4 | 294 | # QT indicates QuickTime; some videos have broken fileExt |
ab6f6aee S |
295 | if f.get('containerFormat') == 'qt': |
296 | f['fileExt'] = 'mov' | |
297 | else: | |
298 | f['fileExt'] = 'mp4' | |
81953d1a RA |
299 | video_url = sign_url( |
300 | '%s/flavorId/%s' % (data_url, f['id'])) | |
1d16035b S |
301 | # audio-only has no videoCodecId (e.g. kaltura:1926081:0_c03e1b5g |
302 | # -f mp4-56) | |
303 | vcodec = 'none' if 'videoCodecId' not in f and f.get( | |
304 | 'frameRate') == 0 else f.get('videoCodecId') | |
01b06aed S |
305 | formats.append({ |
306 | 'format_id': '%(fileExt)s-%(bitrate)s' % f, | |
d80a39ce S |
307 | 'ext': f.get('fileExt'), |
308 | 'tbr': int_or_none(f['bitrate']), | |
309 | 'fps': int_or_none(f.get('frameRate')), | |
01b06aed S |
310 | 'filesize_approx': int_or_none(f.get('size'), invscale=1024), |
311 | 'container': f.get('containerFormat'), | |
1d16035b | 312 | 'vcodec': vcodec, |
d80a39ce S |
313 | 'height': int_or_none(f.get('height')), |
314 | 'width': int_or_none(f.get('width')), | |
01b06aed S |
315 | 'url': video_url, |
316 | }) | |
81953d1a RA |
317 | if '/playManifest/' in data_url: |
318 | m3u8_url = sign_url(data_url.replace( | |
319 | 'format/url', 'format/applehttp')) | |
320 | formats.extend(self._extract_m3u8_formats( | |
321 | m3u8_url, entry_id, 'mp4', 'm3u8_native', | |
322 | m3u8_id='hls', fatal=False)) | |
bb4b8c57 | 323 | |
0d97ef43 NJ |
324 | self._sort_formats(formats) |
325 | ||
1094074c RA |
326 | subtitles = {} |
327 | if captions: | |
328 | for caption in captions.get('objects', []): | |
1094074c | 329 | # Continue if caption is not ready |
65547082 | 330 | if caption.get('status') != 2: |
1094074c | 331 | continue |
92d4cfa3 S |
332 | if not caption.get('id'): |
333 | continue | |
334 | caption_format = int_or_none(caption.get('format')) | |
1094074c RA |
335 | subtitles.setdefault(caption.get('languageCode') or caption.get('language'), []).append({ |
336 | 'url': '%s/api_v3/service/caption_captionasset/action/serve/captionAssetId/%s' % (self._SERVICE_URL, caption['id']), | |
92d4cfa3 | 337 | 'ext': caption.get('fileExt') or self._CAPTION_TYPES.get(caption_format) or 'ttml', |
1094074c RA |
338 | }) |
339 | ||
0d97ef43 | 340 | return { |
ee3ec091 | 341 | 'id': entry_id, |
0d97ef43 NJ |
342 | 'title': info['name'], |
343 | 'formats': formats, | |
1094074c | 344 | 'subtitles': subtitles, |
bdceea7a | 345 | 'description': clean_html(info.get('description')), |
0d97ef43 NJ |
346 | 'thumbnail': info.get('thumbnailUrl'), |
347 | 'duration': info.get('duration'), | |
348 | 'timestamp': info.get('createdAt'), | |
26e40542 | 349 | 'uploader_id': info.get('userId') if info.get('userId') != 'None' else None, |
0d97ef43 NJ |
350 | 'view_count': info.get('plays'), |
351 | } |