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