kaltura.py 5.1 KB

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