2 from __future__ import unicode_literals
7 from .common import InfoExtractor
19 class KalturaIE(InfoExtractor):
22 kaltura:(?P<partner_id_s>\d+):(?P<id_s>[0-9a-z_]+)|
24 (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/
29 (?:[^/]+/)*?wid/_(?P<partner_id>\d+)/
30 (?:[^/]+/)*?entry_id/(?P<id>[0-9a-z_]+)|
33 (?:[^/]+/)*?entry_id/(?P<id_html5>[0-9a-z_]+)
34 .*\?.*\bwid=_(?P<partner_id_html5>\d+)
39 _API_BASE = 'http://cdnapi.kaltura.com/api_v3/index.php?'
42 'url': 'kaltura:269692:1_1jc2y3e4',
43 'md5': '3adcbdb3dcc02d647539e53f284ba171',
48 'upload_date': '20131219',
49 'uploader_id': 'mlundberg@wolfgangsvault.com',
50 'description': 'The Allman Brothers Band, 12/16/1981',
51 'thumbnail': 're:^https?://.*/thumbnail/.*',
56 'url': 'http://www.kaltura.com/index.php/kwidget/cache_st/1300318621/wid/_269692/uiconf_id/3873291/entry_id/1_1jc2y3e4',
57 'only_matching': True,
60 'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3',
61 'only_matching': True,
64 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.30.2/mwEmbedFrame.php/p/1337/uiconf_id/20540612/entry_id/1_sf5ovm7u?wid=_243342',
65 'only_matching': True,
69 def _kaltura_api_call(self, video_id, actions, *args, **kwargs):
72 for i, a in enumerate(actions[1:], start=1):
73 for k, v in a.items():
74 params['%d:%s' % (i, k)] = v
76 query = compat_urllib_parse.urlencode(params)
77 url = self._API_BASE + query
78 data = self._download_json(url, video_id, *args, **kwargs)
80 status = data if len(actions) == 1 else data[0]
81 if status.get('objectType') == 'KalturaAPIException':
83 '%s said: %s' % (self.IE_NAME, status['message']))
87 def _get_kaltura_signature(self, video_id, partner_id):
93 'action': 'startWidgetSession',
94 'widgetId': '_%s' % partner_id,
96 return self._kaltura_api_call(
97 video_id, actions, note='Downloading Kaltura signature')['ks']
99 def _get_video_info(self, video_id, partner_id):
100 signature = self._get_kaltura_signature(video_id, partner_id)
104 'apiVersion': '3.1.5',
105 'clientTag': 'kdp:v3.8.5',
106 'format': 1, # JSON, 2 = XML, 3 = PHP
107 'service': 'multirequest',
113 'service': 'baseentry',
117 'action': 'getContextData',
118 'contextDataParams:objectType': 'KalturaEntryContextDataParams',
119 'contextDataParams:referrer': 'http://www.kaltura.com/',
120 'contextDataParams:streamerType': 'http',
122 'service': 'baseentry',
125 return self._kaltura_api_call(
126 video_id, actions, note='Downloading video info JSON')
128 def _real_extract(self, url):
129 url, smuggled_data = unsmuggle_url(url, {})
131 mobj = re.match(self._VALID_URL, url)
132 partner_id = mobj.group('partner_id_s') or mobj.group('partner_id') or mobj.group('partner_id_html5')
133 entry_id = mobj.group('id_s') or mobj.group('id') or mobj.group('id_html5')
135 info, source_data = self._get_video_info(entry_id, partner_id)
137 source_url = smuggled_data.get('source_url')
139 referrer = base64.b64encode(
140 '://'.join(compat_urlparse.urlparse(source_url)[:2])
141 .encode('utf-8')).decode('utf-8')
146 for f in source_data['flavorAssets']:
147 video_url = '%s/flavorId/%s' % (info['dataUrl'], f['id'])
149 video_url += '?referrer=%s' % referrer
151 'format_id': '%(fileExt)s-%(bitrate)s' % f,
152 'ext': f.get('fileExt'),
153 'tbr': int_or_none(f['bitrate']),
154 'fps': int_or_none(f.get('frameRate')),
155 'filesize_approx': int_or_none(f.get('size'), invscale=1024),
156 'container': f.get('containerFormat'),
157 'vcodec': f.get('videoCodecId'),
158 'height': int_or_none(f.get('height')),
159 'width': int_or_none(f.get('width')),
162 self._check_formats(formats, entry_id)
163 self._sort_formats(formats)
167 'title': info['name'],
169 'description': info.get('description'),
170 'thumbnail': info.get('thumbnailUrl'),
171 'duration': info.get('duration'),
172 'timestamp': info.get('createdAt'),
173 'uploader_id': info.get('userId'),
174 'view_count': info.get('plays'),