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.

154 lines
5.6 KiB

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