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.

134 lines
4.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. (?:kaltura:|
  13. https?://(:?www\.)?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. def _kaltura_api_call(self, video_id, actions, *args, **kwargs):
  40. params = actions[0]
  41. if len(actions) > 1:
  42. for i, a in enumerate(actions[1:], start=1):
  43. for k, v in a.items():
  44. params['%d:%s' % (i, k)] = v
  45. query = compat_urllib_parse.urlencode(params)
  46. url = self._API_BASE + query
  47. data = self._download_json(url, video_id, *args, **kwargs)
  48. status = data if len(actions) == 1 else data[0]
  49. if status.get('objectType') == 'KalturaAPIException':
  50. raise ExtractorError(
  51. '%s said: %s' % (self.IE_NAME, status['message']))
  52. return data
  53. def _get_kaltura_signature(self, video_id, partner_id):
  54. actions = [{
  55. 'apiVersion': '3.1',
  56. 'expiry': 86400,
  57. 'format': 1,
  58. 'service': 'session',
  59. 'action': 'startWidgetSession',
  60. 'widgetId': '_%s' % partner_id,
  61. }]
  62. return self._kaltura_api_call(
  63. video_id, actions, note='Downloading Kaltura signature')['ks']
  64. def _get_video_info(self, video_id, partner_id):
  65. signature = self._get_kaltura_signature(video_id, partner_id)
  66. actions = [
  67. {
  68. 'action': 'null',
  69. 'apiVersion': '3.1.5',
  70. 'clientTag': 'kdp:v3.8.5',
  71. 'format': 1, # JSON, 2 = XML, 3 = PHP
  72. 'service': 'multirequest',
  73. 'ks': signature,
  74. },
  75. {
  76. 'action': 'get',
  77. 'entryId': video_id,
  78. 'service': 'baseentry',
  79. 'version': '-1',
  80. },
  81. {
  82. 'action': 'getContextData',
  83. 'contextDataParams:objectType': 'KalturaEntryContextDataParams',
  84. 'contextDataParams:referrer': 'http://www.kaltura.com/',
  85. 'contextDataParams:streamerType': 'http',
  86. 'entryId': video_id,
  87. 'service': 'baseentry',
  88. },
  89. ]
  90. return self._kaltura_api_call(
  91. video_id, actions, note='Downloading video info JSON')
  92. def _real_extract(self, url):
  93. video_id = self._match_id(url)
  94. mobj = re.match(self._VALID_URL, url)
  95. partner_id, entry_id = mobj.group('partner_id'), mobj.group('id')
  96. info, source_data = self._get_video_info(entry_id, partner_id)
  97. formats = [{
  98. 'format_id': '%(fileExt)s-%(bitrate)s' % f,
  99. 'ext': f['fileExt'],
  100. 'tbr': f['bitrate'],
  101. 'fps': f.get('frameRate'),
  102. 'filesize_approx': int_or_none(f.get('size'), invscale=1024),
  103. 'container': f.get('containerFormat'),
  104. 'vcodec': f.get('videoCodecId'),
  105. 'height': f.get('height'),
  106. 'width': f.get('width'),
  107. 'url': '%s/flavorId/%s' % (info['dataUrl'], f['id']),
  108. } for f in source_data['flavorAssets']]
  109. self._sort_formats(formats)
  110. return {
  111. 'id': video_id,
  112. 'title': info['name'],
  113. 'formats': formats,
  114. 'description': info.get('description'),
  115. 'thumbnail': info.get('thumbnailUrl'),
  116. 'duration': info.get('duration'),
  117. 'timestamp': info.get('createdAt'),
  118. 'uploader_id': info.get('userId'),
  119. 'view_count': info.get('plays'),
  120. }