]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/kaltura.py
ddf1165ffb021005119622d3f162cbce9c637b55
2 from __future__
import unicode_literals
7 from .common
import InfoExtractor
21 class KalturaIE(InfoExtractor
):
24 kaltura:(?P<partner_id>\d+):(?P<id>[0-9a-z_]+)|
26 (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/
32 html5/html5lib/[^/]+/mwEmbedFrame\.php
34 )(?:/(?P<path>[^?]+))?(?:\?(?P<query>.*))?
37 _SERVICE_URL
= 'http://cdnapi.kaltura.com'
38 _SERVICE_BASE
= '/api_v3/index.php'
41 'url': 'kaltura:269692:1_1jc2y3e4',
42 'md5': '3adcbdb3dcc02d647539e53f284ba171',
46 'title': 'Straight from the Heart',
47 'upload_date': '20131219',
48 'uploader_id': 'mlundberg@wolfgangsvault.com',
49 'description': 'The Allman Brothers Band, 12/16/1981',
50 'thumbnail': 're:^https?://.*/thumbnail/.*',
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,
59 'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3',
60 'only_matching': True,
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,
67 # video with subtitles
68 'url': 'kaltura:111032:1_cw786r8q',
69 'only_matching': True,
74 def _extract_url(webpage
):
78 kWidget\.(?:thumb)?[Ee]mbed\(
80 (?P<q1>['\"])wid(?P
=q1
)\s
*:\s
*
81 (?P
<q2
>['\"])_?(?P<partner_id>[^'\"]+)(?P
=q2
),.*?
82 (?P
<q3
>['\"])entry_?[Ii]d(?P=q3)\s*:\s*
83 (?P<q4>['\"])(?P
<id>[^
'\"]+)(?P=q4),
88 (?:https?:)?//cdnapi(?:sec)?\.kaltura\.com/.*?(?:p|partner_id)/(?P<partner_id>\d+).*?
92 (?P<q2>["\'])entry_?[Ii]d(?P=q2)
94 (?P<q3>["\'])(?P<id>.+?)(?P=q3)
97 embed_info = mobj.groupdict()
98 url = 'kaltura
:%(partner_id)s:%(id)s' % embed_info
99 escaped_pid = re.escape(embed_info['partner_id
'])
100 service_url = re.search(
101 r'<script
[^
>]+src
=["\']((?:https?:)?//.+?)/p/%s/sp/%s00/embedIframeJs' % (escaped_pid, escaped_pid),
104 url = smuggle_url(url, {'service_url': service_url.group(1)})
107 def _kaltura_api_call(self, video_id, actions, service_url=None, *args, **kwargs):
110 for i, a in enumerate(actions[1:], start=1):
111 for k, v in a.items():
112 params['%d:%s' % (i, k)] = v
114 data = self._download_json(
115 (service_url or self._SERVICE_URL) + self._SERVICE_BASE,
116 video_id, query=params, *args, **kwargs)
118 status = data if len(actions) == 1 else data[0]
119 if status.get('objectType') == 'KalturaAPIException':
120 raise ExtractorError(
121 '%s said: %s' % (self.IE_NAME, status['message']))
125 def _get_kaltura_signature(self, video_id, partner_id, service_url=None):
130 'service': 'session',
131 'action': 'startWidgetSession',
132 'widgetId': '_%s' % partner_id,
134 return self._kaltura_api_call(
135 video_id, actions, service_url, note='Downloading Kaltura signature')['ks']
137 def _get_video_info(self, video_id, partner_id, service_url=None):
141 'apiVersion': '3.1.5',
142 'clientTag': 'kdp:v3.8.5',
143 'format': 1, # JSON, 2 = XML, 3 = PHP
144 'service': 'multirequest',
148 'service': 'session',
149 'action': 'startWidgetSession',
150 'widgetId': '_%s' % partner_id,
155 'service': 'baseentry',
156 'ks': '{1:result:ks}',
159 'action': 'getbyentryid',
161 'service': 'flavorAsset',
162 'ks': '{1:result:ks}',
166 'filter:entryIdEqual': video_id,
167 'service': 'caption_captionasset',
168 'ks': '{1:result:ks}',
171 return self._kaltura_api_call(
172 video_id, actions, service_url, note='Downloading video info JSON')
174 def _real_extract(self, url):
175 url, smuggled_data = unsmuggle_url(url, {})
177 mobj = re.match(self._VALID_URL, url)
178 partner_id, entry_id = mobj.group('partner_id', 'id')
181 if partner_id and entry_id:
182 _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id, smuggled_data.get('service_url'))
184 path, query = mobj.group('path', 'query')
185 if not path and not query:
186 raise ExtractorError('Invalid URL', expected=True)
189 params = compat_parse_qs(query)
191 splitted_path = path.split('/')
192 params.update(dict((zip(splitted_path[::2], [[v] for v in splitted_path[1::2]]))))
194 partner_id = params['wid'][0][1:]
196 partner_id = params['p'][0]
198 raise ExtractorError('Invalid URL', expected=True)
199 if 'entry_id' in params:
200 entry_id = params['entry_id'][0]
201 _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id)
202 elif 'uiconf_id' in params and 'flashvars[referenceId]' in params:
203 reference_id = params['flashvars[referenceId]'][0]
204 webpage = self._download_webpage(url, reference_id)
205 entry_data = self._parse_json(self._search_regex(
206 r'window\.kalturaIframePackageData\s*=\s*({.*});',
207 webpage, 'kalturaIframePackageData'),
208 reference_id)['entryResult']
209 info, flavor_assets = entry_data['meta'], entry_data['contextData']['flavorAssets']
210 entry_id = info['id']
212 raise ExtractorError('Invalid URL', expected=True)
213 ks = params.get('flashvars[ks]', [None])[0]
215 source_url = smuggled_data.get('source_url')
217 referrer = base64.b64encode(
218 '://'.join(compat_urlparse.urlparse(source_url)[:2])
219 .encode('utf-8')).decode('utf-8')
223 def sign_url(unsigned_url):
225 unsigned_url += '/ks/%s' % ks
227 unsigned_url += '?referrer=%s' % referrer
230 data_url = info['dataUrl']
231 if '/flvclipper/' in data_url:
232 data_url = re.sub(r'/flvclipper/.*', '/serveFlavor', data_url)
235 for f in flavor_assets:
236 # Continue if asset is not ready
237 if f.get('status') != 2:
239 video_url = sign_url(
240 '%s/flavorId/%s' % (data_url, f['id']))
242 'format_id': '%(fileExt)s-%(bitrate)s' % f,
243 'ext': f.get('fileExt'),
244 'tbr': int_or_none(f['bitrate']),
245 'fps': int_or_none(f.get('frameRate')),
246 'filesize_approx': int_or_none(f.get('size'), invscale=1024),
247 'container': f.get('containerFormat'),
248 'vcodec': f.get('videoCodecId'),
249 'height': int_or_none(f.get('height')),
250 'width': int_or_none(f.get('width')),
253 if '/playManifest/' in data_url:
254 m3u8_url = sign_url(data_url.replace(
255 'format/url', 'format/applehttp'))
256 formats.extend(self._extract_m3u8_formats(
257 m3u8_url, entry_id, 'mp4', 'm3u8_native',
258 m3u8_id='hls', fatal=False))
260 self._sort_formats(formats)
264 for caption in captions.get('objects', []):
265 # Continue if caption is not ready
266 if f.get('status') != 2:
268 subtitles.setdefault(caption.get('languageCode') or caption.get('language'), []).append({
269 'url': '%s/api_v3/service/caption_captionasset/action/serve/captionAssetId/%s' % (self._SERVICE_URL, caption['id']),
270 'ext': caption.get('fileExt'),
275 'title': info['name'],
277 'subtitles': subtitles,
278 'description': clean_html(info.get('description')),
279 'thumbnail': info.get('thumbnailUrl'),
280 'duration': info.get('duration'),
281 'timestamp': info.get('createdAt'),
282 'uploader_id': info.get('userId'),
283 'view_count': info.get('plays'),