]>
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, | |
11 | ) | |
0d97ef43 | 12 | from ..utils import ( |
bdceea7a | 13 | clean_html, |
0d97ef43 NJ |
14 | ExtractorError, |
15 | int_or_none, | |
01b06aed | 16 | unsmuggle_url, |
0d97ef43 NJ |
17 | ) |
18 | ||
19 | ||
20 | class KalturaIE(InfoExtractor): | |
21 | _VALID_URL = r'''(?x) | |
ee3ec091 S |
22 | (?: |
23 | kaltura:(?P<partner_id_s>\d+):(?P<id_s>[0-9a-z_]+)| | |
24 | https?:// | |
b25f7533 | 25 | (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/ |
ee3ec091 S |
26 | (?: |
27 | (?: | |
28 | # flash player | |
29 | index\.php/kwidget/ | |
30 | (?:[^/]+/)*?wid/_(?P<partner_id>\d+)/ | |
31 | (?:[^/]+/)*?entry_id/(?P<id>[0-9a-z_]+)| | |
b184f944 | 32 | # html5 player |
ee3ec091 S |
33 | html5/html5lib/ |
34 | (?:[^/]+/)*?entry_id/(?P<id_html5>[0-9a-z_]+) | |
35 | .*\?.*\bwid=_(?P<partner_id_html5>\d+) | |
ae6a8021 | 36 | ) |
ee3ec091 | 37 | ) |
ee3ec091 S |
38 | ) |
39 | ''' | |
0d97ef43 NJ |
40 | _API_BASE = 'http://cdnapi.kaltura.com/api_v3/index.php?' |
41 | _TESTS = [ | |
42 | { | |
43 | 'url': 'kaltura:269692:1_1jc2y3e4', | |
44 | 'md5': '3adcbdb3dcc02d647539e53f284ba171', | |
45 | 'info_dict': { | |
46 | 'id': '1_1jc2y3e4', | |
47 | 'ext': 'mp4', | |
bb4b8c57 | 48 | 'title': 'Straight from the Heart', |
0d97ef43 NJ |
49 | 'upload_date': '20131219', |
50 | 'uploader_id': 'mlundberg@wolfgangsvault.com', | |
51 | 'description': 'The Allman Brothers Band, 12/16/1981', | |
52 | 'thumbnail': 're:^https?://.*/thumbnail/.*', | |
53 | 'timestamp': int, | |
54 | }, | |
55 | }, | |
56 | { | |
57 | 'url': 'http://www.kaltura.com/index.php/kwidget/cache_st/1300318621/wid/_269692/uiconf_id/3873291/entry_id/1_1jc2y3e4', | |
58 | 'only_matching': True, | |
59 | }, | |
bd3749ed S |
60 | { |
61 | 'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3', | |
62 | 'only_matching': True, | |
63 | }, | |
ef49b590 | 64 | { |
65 | 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.30.2/mwEmbedFrame.php/p/1337/uiconf_id/20540612/entry_id/1_sf5ovm7u?wid=_243342', | |
66 | 'only_matching': True, | |
67 | } | |
0d97ef43 NJ |
68 | ] |
69 | ||
70 | def _kaltura_api_call(self, video_id, actions, *args, **kwargs): | |
71 | params = actions[0] | |
72 | if len(actions) > 1: | |
73 | for i, a in enumerate(actions[1:], start=1): | |
74 | for k, v in a.items(): | |
75 | params['%d:%s' % (i, k)] = v | |
76 | ||
77 | query = compat_urllib_parse.urlencode(params) | |
78 | url = self._API_BASE + query | |
79 | data = self._download_json(url, video_id, *args, **kwargs) | |
80 | ||
81 | status = data if len(actions) == 1 else data[0] | |
82 | if status.get('objectType') == 'KalturaAPIException': | |
83 | raise ExtractorError( | |
84 | '%s said: %s' % (self.IE_NAME, status['message'])) | |
85 | ||
86 | return data | |
87 | ||
88 | def _get_kaltura_signature(self, video_id, partner_id): | |
89 | actions = [{ | |
90 | 'apiVersion': '3.1', | |
91 | 'expiry': 86400, | |
92 | 'format': 1, | |
93 | 'service': 'session', | |
94 | 'action': 'startWidgetSession', | |
95 | 'widgetId': '_%s' % partner_id, | |
96 | }] | |
97 | return self._kaltura_api_call( | |
98 | video_id, actions, note='Downloading Kaltura signature')['ks'] | |
99 | ||
100 | def _get_video_info(self, video_id, partner_id): | |
101 | signature = self._get_kaltura_signature(video_id, partner_id) | |
102 | actions = [ | |
103 | { | |
104 | 'action': 'null', | |
105 | 'apiVersion': '3.1.5', | |
106 | 'clientTag': 'kdp:v3.8.5', | |
107 | 'format': 1, # JSON, 2 = XML, 3 = PHP | |
108 | 'service': 'multirequest', | |
109 | 'ks': signature, | |
110 | }, | |
111 | { | |
112 | 'action': 'get', | |
113 | 'entryId': video_id, | |
114 | 'service': 'baseentry', | |
115 | 'version': '-1', | |
116 | }, | |
117 | { | |
bb4b8c57 | 118 | 'action': 'getbyentryid', |
0d97ef43 | 119 | 'entryId': video_id, |
bb4b8c57 | 120 | 'service': 'flavorAsset', |
0d97ef43 NJ |
121 | }, |
122 | ] | |
123 | return self._kaltura_api_call( | |
124 | video_id, actions, note='Downloading video info JSON') | |
125 | ||
126 | def _real_extract(self, url): | |
01b06aed S |
127 | url, smuggled_data = unsmuggle_url(url, {}) |
128 | ||
0d97ef43 | 129 | mobj = re.match(self._VALID_URL, url) |
ee3ec091 S |
130 | partner_id = mobj.group('partner_id_s') or mobj.group('partner_id') or mobj.group('partner_id_html5') |
131 | entry_id = mobj.group('id_s') or mobj.group('id') or mobj.group('id_html5') | |
0d97ef43 | 132 | |
bb4b8c57 | 133 | info, flavor_assets = self._get_video_info(entry_id, partner_id) |
0d97ef43 | 134 | |
01b06aed S |
135 | source_url = smuggled_data.get('source_url') |
136 | if source_url: | |
137 | referrer = base64.b64encode( | |
138 | '://'.join(compat_urlparse.urlparse(source_url)[:2]) | |
139 | .encode('utf-8')).decode('utf-8') | |
140 | else: | |
141 | referrer = None | |
142 | ||
143 | formats = [] | |
bb4b8c57 | 144 | for f in flavor_assets: |
145 | # Continue if asset is not ready | |
146 | if f['status'] != 2: | |
147 | continue | |
01b06aed S |
148 | video_url = '%s/flavorId/%s' % (info['dataUrl'], f['id']) |
149 | if referrer: | |
150 | video_url += '?referrer=%s' % referrer | |
151 | formats.append({ | |
152 | 'format_id': '%(fileExt)s-%(bitrate)s' % f, | |
d80a39ce S |
153 | 'ext': f.get('fileExt'), |
154 | 'tbr': int_or_none(f['bitrate']), | |
155 | 'fps': int_or_none(f.get('frameRate')), | |
01b06aed S |
156 | 'filesize_approx': int_or_none(f.get('size'), invscale=1024), |
157 | 'container': f.get('containerFormat'), | |
158 | 'vcodec': f.get('videoCodecId'), | |
d80a39ce S |
159 | 'height': int_or_none(f.get('height')), |
160 | 'width': int_or_none(f.get('width')), | |
01b06aed S |
161 | 'url': video_url, |
162 | }) | |
608cc3b8 | 163 | m3u8_url = info['dataUrl'].replace('format/url', 'format/applehttp') |
164 | if referrer: | |
165 | m3u8_url += '?referrer=%s' % referrer | |
7e5edcfd S |
166 | formats.extend(self._extract_m3u8_formats( |
167 | m3u8_url, entry_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)) | |
bb4b8c57 | 168 | |
01b06aed | 169 | self._check_formats(formats, entry_id) |
0d97ef43 NJ |
170 | self._sort_formats(formats) |
171 | ||
172 | return { | |
ee3ec091 | 173 | 'id': entry_id, |
0d97ef43 NJ |
174 | 'title': info['name'], |
175 | 'formats': formats, | |
bdceea7a | 176 | 'description': clean_html(info.get('description')), |
0d97ef43 NJ |
177 | 'thumbnail': info.get('thumbnailUrl'), |
178 | 'duration': info.get('duration'), | |
179 | 'timestamp': info.get('createdAt'), | |
180 | 'uploader_id': info.get('userId'), | |
181 | 'view_count': info.get('plays'), | |
182 | } |