2 from __future__ 
import unicode_literals
 
   7 from .common 
import InfoExtractor
 
   9     compat_urllib_parse_urlencode
, 
  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     _API_BASE 
= 'http://cdnapi.kaltura.com/api_v3/index.php?' 
  40             'url': 'kaltura:269692:1_1jc2y3e4', 
  41             'md5': '3adcbdb3dcc02d647539e53f284ba171', 
  45                 'title': 'Straight from the Heart', 
  46                 'upload_date': '20131219', 
  47                 'uploader_id': 'mlundberg@wolfgangsvault.com', 
  48                 'description': 'The Allman Brothers Band, 12/16/1981', 
  49                 'thumbnail': 're:^https?://.*/thumbnail/.*', 
  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, 
  58             'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3', 
  59             'only_matching': True, 
  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, 
  67     def _kaltura_api_call(self
, video_id
, actions
, *args
, **kwargs
): 
  70             for i
, a 
in enumerate(actions
[1:], start
=1): 
  71                 for k
, v 
in a
.items(): 
  72                     params
['%d:%s' % (i
, k
)] = v
 
  74         query 
= compat_urllib_parse_urlencode(params
) 
  75         url 
= self
._API
_BASE 
+ query
 
  76         data 
= self
._download
_json
(url
, video_id
, *args
, **kwargs
) 
  78         status 
= data 
if len(actions
) == 1 else data
[0] 
  79         if status
.get('objectType') == 'KalturaAPIException': 
  81                 '%s said: %s' % (self
.IE_NAME
, status
['message'])) 
  85     def _get_kaltura_signature(self
, video_id
, partner_id
): 
  91             'action': 'startWidgetSession', 
  92             'widgetId': '_%s' % partner_id
, 
  94         return self
._kaltura
_api
_call
( 
  95             video_id
, actions
, note
='Downloading Kaltura signature')['ks'] 
  97     def _get_video_info(self
, video_id
, partner_id
): 
  98         signature 
= self
._get
_kaltura
_signature
(video_id
, partner_id
) 
 102                 'apiVersion': '3.1.5', 
 103                 'clientTag': 'kdp:v3.8.5', 
 104                 'format': 1,  # JSON, 2 = XML, 3 = PHP 
 105                 'service': 'multirequest', 
 111                 'service': 'baseentry', 
 115                 'action': 'getbyentryid', 
 117                 'service': 'flavorAsset', 
 120         return self
._kaltura
_api
_call
( 
 121             video_id
, actions
, note
='Downloading video info JSON') 
 123     def _real_extract(self
, url
): 
 124         url
, smuggled_data 
= unsmuggle_url(url
, {}) 
 126         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 127         partner_id
, entry_id 
= mobj
.group('partner_id', 'id') 
 129         if partner_id 
and entry_id
: 
 130             info
, flavor_assets 
= self
._get
_video
_info
(entry_id
, partner_id
) 
 132             path
, query 
= mobj
.group('path', 'query') 
 133             if not path 
and not query
: 
 134                 raise ExtractorError('Invalid URL', expected
=True) 
 137                 params 
= compat_parse_qs(query
) 
 139                 splitted_path 
= path
.split('/') 
 140                 params
.update(dict((zip(splitted_path
[::2], [[v
] for v 
in splitted_path
[1::2]])))) 
 142                 partner_id 
= params
['wid'][0][1:] 
 144                 partner_id 
= params
['p'][0] 
 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'] 
 160                 raise ExtractorError('Invalid URL', expected
=True) 
 161             ks 
= params
.get('flashvars[ks]', [None])[0] 
 163         source_url 
= smuggled_data
.get('source_url') 
 165             referrer 
= base64
.b64encode( 
 166                 '://'.join(compat_urlparse
.urlparse(source_url
)[:2]) 
 167                 .encode('utf-8')).decode('utf-8') 
 171         def sign_url(unsigned_url
): 
 173                 unsigned_url 
+= '/ks/%s' % ks
 
 175                 unsigned_url 
+= '?referrer=%s' % referrer
 
 179         for f 
in flavor_assets
: 
 180             # Continue if asset is not ready 
 183             video_url 
= sign_url('%s/flavorId/%s' % (info
['dataUrl'], f
['id'])) 
 185                 'format_id': '%(fileExt)s-%(bitrate)s' % f
, 
 186                 'ext': f
.get('fileExt'), 
 187                 'tbr': int_or_none(f
['bitrate']), 
 188                 'fps': int_or_none(f
.get('frameRate')), 
 189                 'filesize_approx': int_or_none(f
.get('size'), invscale
=1024), 
 190                 'container': f
.get('containerFormat'), 
 191                 'vcodec': f
.get('videoCodecId'), 
 192                 'height': int_or_none(f
.get('height')), 
 193                 'width': int_or_none(f
.get('width')), 
 196         m3u8_url 
= sign_url(info
['dataUrl'].replace('format/url', 'format/applehttp')) 
 197         formats
.extend(self
._extract
_m
3u8_formats
( 
 198             m3u8_url
, entry_id
, 'mp4', 'm3u8_native', m3u8_id
='hls', fatal
=False)) 
 200         self
._check
_formats
(formats
, entry_id
) 
 201         self
._sort
_formats
(formats
) 
 205             'title': info
['name'], 
 207             'description': clean_html(info
.get('description')), 
 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'),