[kaltura] add html5 player urls
[youtube-dl] / youtube_dl / extractor / kaltura.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_urllib_parse
8 from ..utils import (
9     ExtractorError,
10     int_or_none,
11 )
12
13
14 class KalturaIE(InfoExtractor):
15     _VALID_URL = r'''(?x)
16     (?:kaltura:|
17         https?://(:?(?:www|cdnapisec)\.)?kaltura\.com/(?:
18             (?:index\.php/kwidget/(?:[^/]+/)*?wid/_)|
19             (?:html5/html5lib/v(?:[\d.]+)/mwEmbedFrame.php/p/\d+)
20         )
21     )(?P<partner_id>\d+)?(?::|/(?:[^/]+/)*?entry_id/)(?P<id>[0-9a-z_]+)
22     (?:\?wid=_(?P<partner_id_html5>\d+))?'''
23     _API_BASE = 'http://cdnapi.kaltura.com/api_v3/index.php?'
24     _TESTS = [
25         {
26             'url': 'kaltura:269692:1_1jc2y3e4',
27             'md5': '3adcbdb3dcc02d647539e53f284ba171',
28             'info_dict': {
29                 'id': '1_1jc2y3e4',
30                 'ext': 'mp4',
31                 'title': 'Track 4',
32                 'upload_date': '20131219',
33                 'uploader_id': 'mlundberg@wolfgangsvault.com',
34                 'description': 'The Allman Brothers Band, 12/16/1981',
35                 'thumbnail': 're:^https?://.*/thumbnail/.*',
36                 'timestamp': int,
37             },
38         },
39         {
40             'url': 'http://www.kaltura.com/index.php/kwidget/cache_st/1300318621/wid/_269692/uiconf_id/3873291/entry_id/1_1jc2y3e4',
41             'only_matching': True,
42         },
43         {
44             'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3',
45             'only_matching': True,
46         },
47         {
48             'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.30.2/mwEmbedFrame.php/p/1337/uiconf_id/20540612/entry_id/1_sf5ovm7u?wid=_243342',
49             'only_matching': True,
50         }
51     ]
52
53     def _kaltura_api_call(self, video_id, actions, *args, **kwargs):
54         params = actions[0]
55         if len(actions) > 1:
56             for i, a in enumerate(actions[1:], start=1):
57                 for k, v in a.items():
58                     params['%d:%s' % (i, k)] = v
59
60         query = compat_urllib_parse.urlencode(params)
61         url = self._API_BASE + query
62         data = self._download_json(url, video_id, *args, **kwargs)
63
64         status = data if len(actions) == 1 else data[0]
65         if status.get('objectType') == 'KalturaAPIException':
66             raise ExtractorError(
67                 '%s said: %s' % (self.IE_NAME, status['message']))
68
69         return data
70
71     def _get_kaltura_signature(self, video_id, partner_id):
72         actions = [{
73             'apiVersion': '3.1',
74             'expiry': 86400,
75             'format': 1,
76             'service': 'session',
77             'action': 'startWidgetSession',
78             'widgetId': '_%s' % partner_id,
79         }]
80         return self._kaltura_api_call(
81             video_id, actions, note='Downloading Kaltura signature')['ks']
82
83     def _get_video_info(self, video_id, partner_id):
84         signature = self._get_kaltura_signature(video_id, partner_id)
85         actions = [
86             {
87                 'action': 'null',
88                 'apiVersion': '3.1.5',
89                 'clientTag': 'kdp:v3.8.5',
90                 'format': 1,  # JSON, 2 = XML, 3 = PHP
91                 'service': 'multirequest',
92                 'ks': signature,
93             },
94             {
95                 'action': 'get',
96                 'entryId': video_id,
97                 'service': 'baseentry',
98                 'version': '-1',
99             },
100             {
101                 'action': 'getContextData',
102                 'contextDataParams:objectType': 'KalturaEntryContextDataParams',
103                 'contextDataParams:referrer': 'http://www.kaltura.com/',
104                 'contextDataParams:streamerType': 'http',
105                 'entryId': video_id,
106                 'service': 'baseentry',
107             },
108         ]
109         return self._kaltura_api_call(
110             video_id, actions, note='Downloading video info JSON')
111
112     def _real_extract(self, url):
113         video_id = self._match_id(url)
114         mobj = re.match(self._VALID_URL, url)
115         partner_id, entry_id = mobj.group('partner_id') or mobj.group('partner_id_html5'), mobj.group('id')
116
117         info, source_data = self._get_video_info(entry_id, partner_id)
118
119         formats = [{
120             'format_id': '%(fileExt)s-%(bitrate)s' % f,
121             'ext': f['fileExt'],
122             'tbr': f['bitrate'],
123             'fps': f.get('frameRate'),
124             'filesize_approx': int_or_none(f.get('size'), invscale=1024),
125             'container': f.get('containerFormat'),
126             'vcodec': f.get('videoCodecId'),
127             'height': f.get('height'),
128             'width': f.get('width'),
129             'url': '%s/flavorId/%s' % (info['dataUrl'], f['id']),
130         } for f in source_data['flavorAssets']]
131         self._sort_formats(formats)
132
133         return {
134             'id': video_id,
135             'title': info['name'],
136             'formats': formats,
137             'description': info.get('description'),
138             'thumbnail': info.get('thumbnailUrl'),
139             'duration': info.get('duration'),
140             'timestamp': info.get('createdAt'),
141             'uploader_id': info.get('userId'),
142             'view_count': info.get('plays'),
143         }