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