]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/kaltura.py
[limelight] Fix typo
[youtube-dl] / youtube_dl / extractor / kaltura.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import base64
6
7 from .common import InfoExtractor
8 from ..compat import (
9     compat_urllib_parse_urlencode,
10     compat_urlparse,
11     compat_parse_qs,
12 )
13 from ..utils import (
14     clean_html,
15     ExtractorError,
16     int_or_none,
17     unsmuggle_url,
18 )
19
20
21 class KalturaIE(InfoExtractor):
22     _VALID_URL = r'''(?x)
23                 (?:
24                     kaltura:(?P<partner_id>\d+):(?P<id>[0-9a-z_]+)|
25                     https?://
26                         (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/
27                         (?:
28                             (?:
29                                 # flash player
30                                 index\.php/kwidget|
31                                 # html5 player
32                                 html5/html5lib/[^/]+/mwEmbedFrame\.php
33                             )
34                         )(?:/(?P<path>[^?]+))?(?:\?(?P<query>.*))?
35                 )
36                 '''
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',
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/.*',
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         },
57         {
58             'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3',
59             'only_matching': True,
60         },
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         }
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             {
115                 'action': 'getbyentryid',
116                 'entryId': video_id,
117                 'service': 'flavorAsset',
118             },
119         ]
120         return self._kaltura_api_call(
121             video_id, actions, note='Downloading video info JSON')
122
123     def _real_extract(self, url):
124         url, smuggled_data = unsmuggle_url(url, {})
125
126         mobj = re.match(self._VALID_URL, url)
127         partner_id, entry_id = mobj.group('partner_id', 'id')
128         ks = None
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('/')
140                 params.update(dict((zip(splitted_path[::2], [[v] for v in splitted_path[1::2]]))))
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)
161             ks = params.get('flashvars[ks]', [None])[0]
162
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
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
178         formats = []
179         for f in flavor_assets:
180             # Continue if asset is not ready
181             if f['status'] != 2:
182                 continue
183             video_url = sign_url('%s/flavorId/%s' % (info['dataUrl'], f['id']))
184             formats.append({
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')),
194                 'url': video_url,
195             })
196         m3u8_url = sign_url(info['dataUrl'].replace('format/url', 'format/applehttp'))
197         formats.extend(self._extract_m3u8_formats(
198             m3u8_url, entry_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
199
200         self._check_formats(formats, entry_id)
201         self._sort_formats(formats)
202
203         return {
204             'id': entry_id,
205             'title': info['name'],
206             'formats': formats,
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'),
213         }