]>
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?:// |
b25f7533 | 26 | (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/ |
ee3ec091 S |
27 | (?: |
28 | (?: | |
29 | # flash player | |
fac7e792 | 30 | index\.php/kwidget| |
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' | |
0d97ef43 NJ |
39 | _TESTS = [ |
40 | { | |
41 | 'url': 'kaltura:269692:1_1jc2y3e4', | |
42 | 'md5': '3adcbdb3dcc02d647539e53f284ba171', | |
43 | 'info_dict': { | |
44 | 'id': '1_1jc2y3e4', | |
45 | 'ext': 'mp4', | |
bb4b8c57 | 46 | 'title': 'Straight from the Heart', |
0d97ef43 NJ |
47 | 'upload_date': '20131219', |
48 | 'uploader_id': 'mlundberg@wolfgangsvault.com', | |
49 | 'description': 'The Allman Brothers Band, 12/16/1981', | |
50 | 'thumbnail': 're:^https?://.*/thumbnail/.*', | |
51 | 'timestamp': int, | |
52 | }, | |
53 | }, | |
54 | { | |
55 | 'url': 'http://www.kaltura.com/index.php/kwidget/cache_st/1300318621/wid/_269692/uiconf_id/3873291/entry_id/1_1jc2y3e4', | |
56 | 'only_matching': True, | |
57 | }, | |
bd3749ed S |
58 | { |
59 | 'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3', | |
60 | 'only_matching': True, | |
61 | }, | |
ef49b590 | 62 | { |
63 | 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.30.2/mwEmbedFrame.php/p/1337/uiconf_id/20540612/entry_id/1_sf5ovm7u?wid=_243342', | |
64 | 'only_matching': True, | |
65 | } | |
0d97ef43 NJ |
66 | ] |
67 | ||
9ea5c04c S |
68 | @staticmethod |
69 | def _extract_url(webpage): | |
70 | mobj = ( | |
71 | re.search( | |
72 | r"""(?xs) | |
73 | kWidget\.(?:thumb)?[Ee]mbed\( | |
74 | \{.*? | |
75 | (?P<q1>['\"])wid(?P=q1)\s*:\s* | |
76 | (?P<q2>['\"])_?(?P<partner_id>[^'\"]+)(?P=q2),.*? | |
77 | (?P<q3>['\"])entry_?[Ii]d(?P=q3)\s*:\s* | |
78 | (?P<q4>['\"])(?P<id>[^'\"]+)(?P=q4), | |
79 | """, webpage) or | |
80 | re.search( | |
81 | r'''(?xs) | |
82 | (?P<q1>["\']) | |
83 | (?:https?:)?//cdnapi(?:sec)?\.kaltura\.com/.*?(?:p|partner_id)/(?P<partner_id>\d+).*? | |
84 | (?P=q1).*? | |
85 | (?: | |
86 | entry_?[Ii]d| | |
87 | (?P<q2>["\'])entry_?[Ii]d(?P=q2) | |
88 | )\s*:\s* | |
89 | (?P<q3>["\'])(?P<id>.+?)(?P=q3) | |
90 | ''', webpage)) | |
91 | if mobj: | |
81953d1a RA |
92 | embed_info = mobj.groupdict() |
93 | url = 'kaltura:%(partner_id)s:%(id)s' % embed_info | |
d9163ae3 | 94 | escaped_pid = re.escape(embed_info['partner_id']) |
81953d1a | 95 | service_url = re.search( |
d9163ae3 | 96 | r'<script[^>]+src=["\']((?:https?:)?//.+?)/p/%s/sp/%s00/embedIframeJs' % (escaped_pid, escaped_pid), |
81953d1a RA |
97 | webpage) |
98 | if service_url: | |
99 | url = smuggle_url(url, {'service_url': service_url.group(1)}) | |
100 | return url | |
9ea5c04c | 101 | |
d9163ae3 | 102 | def _kaltura_api_call(self, video_id, actions, service_url=None, *args, **kwargs): |
0d97ef43 NJ |
103 | params = actions[0] |
104 | if len(actions) > 1: | |
105 | for i, a in enumerate(actions[1:], start=1): | |
106 | for k, v in a.items(): | |
107 | params['%d:%s' % (i, k)] = v | |
108 | ||
81953d1a | 109 | data = self._download_json( |
d9163ae3 | 110 | (service_url or self._SERVICE_URL) + self._SERVICE_BASE, |
81953d1a | 111 | video_id, query=params, *args, **kwargs) |
0d97ef43 NJ |
112 | |
113 | status = data if len(actions) == 1 else data[0] | |
114 | if status.get('objectType') == 'KalturaAPIException': | |
115 | raise ExtractorError( | |
116 | '%s said: %s' % (self.IE_NAME, status['message'])) | |
117 | ||
118 | return data | |
119 | ||
d9163ae3 | 120 | def _get_kaltura_signature(self, video_id, partner_id, service_url=None): |
0d97ef43 NJ |
121 | actions = [{ |
122 | 'apiVersion': '3.1', | |
123 | 'expiry': 86400, | |
124 | 'format': 1, | |
125 | 'service': 'session', | |
126 | 'action': 'startWidgetSession', | |
127 | 'widgetId': '_%s' % partner_id, | |
128 | }] | |
129 | return self._kaltura_api_call( | |
d9163ae3 | 130 | video_id, actions, service_url, note='Downloading Kaltura signature')['ks'] |
0d97ef43 | 131 | |
d9163ae3 RA |
132 | def _get_video_info(self, video_id, partner_id, service_url=None): |
133 | signature = self._get_kaltura_signature(video_id, partner_id, service_url) | |
0d97ef43 NJ |
134 | actions = [ |
135 | { | |
136 | 'action': 'null', | |
137 | 'apiVersion': '3.1.5', | |
138 | 'clientTag': 'kdp:v3.8.5', | |
139 | 'format': 1, # JSON, 2 = XML, 3 = PHP | |
140 | 'service': 'multirequest', | |
141 | 'ks': signature, | |
142 | }, | |
143 | { | |
144 | 'action': 'get', | |
145 | 'entryId': video_id, | |
146 | 'service': 'baseentry', | |
147 | 'version': '-1', | |
148 | }, | |
149 | { | |
bb4b8c57 | 150 | 'action': 'getbyentryid', |
0d97ef43 | 151 | 'entryId': video_id, |
bb4b8c57 | 152 | 'service': 'flavorAsset', |
0d97ef43 NJ |
153 | }, |
154 | ] | |
155 | return self._kaltura_api_call( | |
d9163ae3 | 156 | video_id, actions, service_url, note='Downloading video info JSON') |
0d97ef43 NJ |
157 | |
158 | def _real_extract(self, url): | |
01b06aed S |
159 | url, smuggled_data = unsmuggle_url(url, {}) |
160 | ||
0d97ef43 | 161 | mobj = re.match(self._VALID_URL, url) |
fac7e792 | 162 | partner_id, entry_id = mobj.group('partner_id', 'id') |
9dce3c09 | 163 | ks = None |
fac7e792 | 164 | if partner_id and entry_id: |
d9163ae3 | 165 | info, flavor_assets = self._get_video_info(entry_id, partner_id, smuggled_data.get('service_url')) |
fac7e792 | 166 | else: |
167 | path, query = mobj.group('path', 'query') | |
168 | if not path and not query: | |
169 | raise ExtractorError('Invalid URL', expected=True) | |
170 | params = {} | |
171 | if query: | |
172 | params = compat_parse_qs(query) | |
173 | if path: | |
174 | splitted_path = path.split('/') | |
e5a2e17a | 175 | params.update(dict((zip(splitted_path[::2], [[v] for v in splitted_path[1::2]])))) |
fac7e792 | 176 | if 'wid' in params: |
177 | partner_id = params['wid'][0][1:] | |
178 | elif 'p' in params: | |
179 | partner_id = params['p'][0] | |
180 | else: | |
181 | raise ExtractorError('Invalid URL', expected=True) | |
182 | if 'entry_id' in params: | |
183 | entry_id = params['entry_id'][0] | |
184 | info, flavor_assets = self._get_video_info(entry_id, partner_id) | |
185 | elif 'uiconf_id' in params and 'flashvars[referenceId]' in params: | |
186 | reference_id = params['flashvars[referenceId]'][0] | |
187 | webpage = self._download_webpage(url, reference_id) | |
188 | entry_data = self._parse_json(self._search_regex( | |
189 | r'window\.kalturaIframePackageData\s*=\s*({.*});', | |
190 | webpage, 'kalturaIframePackageData'), | |
191 | reference_id)['entryResult'] | |
192 | info, flavor_assets = entry_data['meta'], entry_data['contextData']['flavorAssets'] | |
193 | entry_id = info['id'] | |
194 | else: | |
195 | raise ExtractorError('Invalid URL', expected=True) | |
9dce3c09 | 196 | ks = params.get('flashvars[ks]', [None])[0] |
0d97ef43 | 197 | |
01b06aed S |
198 | source_url = smuggled_data.get('source_url') |
199 | if source_url: | |
200 | referrer = base64.b64encode( | |
201 | '://'.join(compat_urlparse.urlparse(source_url)[:2]) | |
202 | .encode('utf-8')).decode('utf-8') | |
203 | else: | |
204 | referrer = None | |
205 | ||
9dce3c09 S |
206 | def sign_url(unsigned_url): |
207 | if ks: | |
208 | unsigned_url += '/ks/%s' % ks | |
209 | if referrer: | |
210 | unsigned_url += '?referrer=%s' % referrer | |
211 | return unsigned_url | |
212 | ||
81953d1a RA |
213 | data_url = info['dataUrl'] |
214 | if '/flvclipper/' in data_url: | |
215 | data_url = re.sub(r'/flvclipper/.*', '/serveFlavor', data_url) | |
216 | ||
01b06aed | 217 | formats = [] |
bb4b8c57 | 218 | for f in flavor_assets: |
219 | # Continue if asset is not ready | |
220 | if f['status'] != 2: | |
221 | continue | |
81953d1a RA |
222 | video_url = sign_url( |
223 | '%s/flavorId/%s' % (data_url, f['id'])) | |
01b06aed S |
224 | formats.append({ |
225 | 'format_id': '%(fileExt)s-%(bitrate)s' % f, | |
d80a39ce S |
226 | 'ext': f.get('fileExt'), |
227 | 'tbr': int_or_none(f['bitrate']), | |
228 | 'fps': int_or_none(f.get('frameRate')), | |
01b06aed S |
229 | 'filesize_approx': int_or_none(f.get('size'), invscale=1024), |
230 | 'container': f.get('containerFormat'), | |
231 | 'vcodec': f.get('videoCodecId'), | |
d80a39ce S |
232 | 'height': int_or_none(f.get('height')), |
233 | 'width': int_or_none(f.get('width')), | |
01b06aed S |
234 | 'url': video_url, |
235 | }) | |
81953d1a RA |
236 | if '/playManifest/' in data_url: |
237 | m3u8_url = sign_url(data_url.replace( | |
238 | 'format/url', 'format/applehttp')) | |
239 | formats.extend(self._extract_m3u8_formats( | |
240 | m3u8_url, entry_id, 'mp4', 'm3u8_native', | |
241 | m3u8_id='hls', fatal=False)) | |
bb4b8c57 | 242 | |
01b06aed | 243 | self._check_formats(formats, entry_id) |
0d97ef43 NJ |
244 | self._sort_formats(formats) |
245 | ||
246 | return { | |
ee3ec091 | 247 | 'id': entry_id, |
0d97ef43 NJ |
248 | 'title': info['name'], |
249 | 'formats': formats, | |
bdceea7a | 250 | 'description': clean_html(info.get('description')), |
0d97ef43 NJ |
251 | 'thumbnail': info.get('thumbnailUrl'), |
252 | 'duration': info.get('duration'), | |
253 | 'timestamp': info.get('createdAt'), | |
254 | 'uploader_id': info.get('userId'), | |
255 | 'view_count': info.get('plays'), | |
256 | } |