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.

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