You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
6.5 KiB

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