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.

256 lines
9.6 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_urlparse,
  8. compat_parse_qs,
  9. )
  10. from ..utils import (
  11. clean_html,
  12. ExtractorError,
  13. int_or_none,
  14. unsmuggle_url,
  15. smuggle_url,
  16. )
  17. class KalturaIE(InfoExtractor):
  18. _VALID_URL = r'''(?x)
  19. (?:
  20. kaltura:(?P<partner_id>\d+):(?P<id>[0-9a-z_]+)|
  21. https?://
  22. (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/
  23. (?:
  24. (?:
  25. # flash player
  26. index\.php/kwidget|
  27. # html5 player
  28. html5/html5lib/[^/]+/mwEmbedFrame\.php
  29. )
  30. )(?:/(?P<path>[^?]+))?(?:\?(?P<query>.*))?
  31. )
  32. '''
  33. _SERVICE_URL = 'http://cdnapi.kaltura.com'
  34. _SERVICE_BASE = '/api_v3/index.php'
  35. _TESTS = [
  36. {
  37. 'url': 'kaltura:269692:1_1jc2y3e4',
  38. 'md5': '3adcbdb3dcc02d647539e53f284ba171',
  39. 'info_dict': {
  40. 'id': '1_1jc2y3e4',
  41. 'ext': 'mp4',
  42. 'title': 'Straight from the Heart',
  43. 'upload_date': '20131219',
  44. 'uploader_id': 'mlundberg@wolfgangsvault.com',
  45. 'description': 'The Allman Brothers Band, 12/16/1981',
  46. 'thumbnail': 're:^https?://.*/thumbnail/.*',
  47. 'timestamp': int,
  48. },
  49. },
  50. {
  51. 'url': 'http://www.kaltura.com/index.php/kwidget/cache_st/1300318621/wid/_269692/uiconf_id/3873291/entry_id/1_1jc2y3e4',
  52. 'only_matching': True,
  53. },
  54. {
  55. 'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3',
  56. 'only_matching': True,
  57. },
  58. {
  59. 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.30.2/mwEmbedFrame.php/p/1337/uiconf_id/20540612/entry_id/1_sf5ovm7u?wid=_243342',
  60. 'only_matching': True,
  61. }
  62. ]
  63. @staticmethod
  64. def _extract_url(webpage):
  65. mobj = (
  66. re.search(
  67. r"""(?xs)
  68. kWidget\.(?:thumb)?[Ee]mbed\(
  69. \{.*?
  70. (?P<q1>['\"])wid(?P=q1)\s*:\s*
  71. (?P<q2>['\"])_?(?P<partner_id>[^'\"]+)(?P=q2),.*?
  72. (?P<q3>['\"])entry_?[Ii]d(?P=q3)\s*:\s*
  73. (?P<q4>['\"])(?P<id>[^'\"]+)(?P=q4),
  74. """, webpage) or
  75. re.search(
  76. r'''(?xs)
  77. (?P<q1>["\'])
  78. (?:https?:)?//cdnapi(?:sec)?\.kaltura\.com/.*?(?:p|partner_id)/(?P<partner_id>\d+).*?
  79. (?P=q1).*?
  80. (?:
  81. entry_?[Ii]d|
  82. (?P<q2>["\'])entry_?[Ii]d(?P=q2)
  83. )\s*:\s*
  84. (?P<q3>["\'])(?P<id>.+?)(?P=q3)
  85. ''', webpage))
  86. if mobj:
  87. embed_info = mobj.groupdict()
  88. url = 'kaltura:%(partner_id)s:%(id)s' % embed_info
  89. escaped_pid = re.escape(embed_info['partner_id'])
  90. service_url = re.search(
  91. r'<script[^>]+src=["\']((?:https?:)?//.+?)/p/%s/sp/%s00/embedIframeJs' % (escaped_pid, escaped_pid),
  92. webpage)
  93. if service_url:
  94. url = smuggle_url(url, {'service_url': service_url.group(1)})
  95. return url
  96. def _kaltura_api_call(self, video_id, actions, service_url=None, *args, **kwargs):
  97. params = actions[0]
  98. if len(actions) > 1:
  99. for i, a in enumerate(actions[1:], start=1):
  100. for k, v in a.items():
  101. params['%d:%s' % (i, k)] = v
  102. data = self._download_json(
  103. (service_url or self._SERVICE_URL) + self._SERVICE_BASE,
  104. video_id, query=params, *args, **kwargs)
  105. status = data if len(actions) == 1 else data[0]
  106. if status.get('objectType') == 'KalturaAPIException':
  107. raise ExtractorError(
  108. '%s said: %s' % (self.IE_NAME, status['message']))
  109. return data
  110. def _get_kaltura_signature(self, video_id, partner_id, service_url=None):
  111. actions = [{
  112. 'apiVersion': '3.1',
  113. 'expiry': 86400,
  114. 'format': 1,
  115. 'service': 'session',
  116. 'action': 'startWidgetSession',
  117. 'widgetId': '_%s' % partner_id,
  118. }]
  119. return self._kaltura_api_call(
  120. video_id, actions, service_url, note='Downloading Kaltura signature')['ks']
  121. def _get_video_info(self, video_id, partner_id, service_url=None):
  122. signature = self._get_kaltura_signature(video_id, partner_id, service_url)
  123. actions = [
  124. {
  125. 'action': 'null',
  126. 'apiVersion': '3.1.5',
  127. 'clientTag': 'kdp:v3.8.5',
  128. 'format': 1, # JSON, 2 = XML, 3 = PHP
  129. 'service': 'multirequest',
  130. 'ks': signature,
  131. },
  132. {
  133. 'action': 'get',
  134. 'entryId': video_id,
  135. 'service': 'baseentry',
  136. 'version': '-1',
  137. },
  138. {
  139. 'action': 'getbyentryid',
  140. 'entryId': video_id,
  141. 'service': 'flavorAsset',
  142. },
  143. ]
  144. return self._kaltura_api_call(
  145. video_id, actions, service_url, note='Downloading video info JSON')
  146. def _real_extract(self, url):
  147. url, smuggled_data = unsmuggle_url(url, {})
  148. mobj = re.match(self._VALID_URL, url)
  149. partner_id, entry_id = mobj.group('partner_id', 'id')
  150. ks = None
  151. if partner_id and entry_id:
  152. info, flavor_assets = self._get_video_info(entry_id, partner_id, smuggled_data.get('service_url'))
  153. else:
  154. path, query = mobj.group('path', 'query')
  155. if not path and not query:
  156. raise ExtractorError('Invalid URL', expected=True)
  157. params = {}
  158. if query:
  159. params = compat_parse_qs(query)
  160. if path:
  161. splitted_path = path.split('/')
  162. params.update(dict((zip(splitted_path[::2], [[v] for v in splitted_path[1::2]]))))
  163. if 'wid' in params:
  164. partner_id = params['wid'][0][1:]
  165. elif 'p' in params:
  166. partner_id = params['p'][0]
  167. else:
  168. raise ExtractorError('Invalid URL', expected=True)
  169. if 'entry_id' in params:
  170. entry_id = params['entry_id'][0]
  171. info, flavor_assets = self._get_video_info(entry_id, partner_id)
  172. elif 'uiconf_id' in params and 'flashvars[referenceId]' in params:
  173. reference_id = params['flashvars[referenceId]'][0]
  174. webpage = self._download_webpage(url, reference_id)
  175. entry_data = self._parse_json(self._search_regex(
  176. r'window\.kalturaIframePackageData\s*=\s*({.*});',
  177. webpage, 'kalturaIframePackageData'),
  178. reference_id)['entryResult']
  179. info, flavor_assets = entry_data['meta'], entry_data['contextData']['flavorAssets']
  180. entry_id = info['id']
  181. else:
  182. raise ExtractorError('Invalid URL', expected=True)
  183. ks = params.get('flashvars[ks]', [None])[0]
  184. source_url = smuggled_data.get('source_url')
  185. if source_url:
  186. referrer = base64.b64encode(
  187. '://'.join(compat_urlparse.urlparse(source_url)[:2])
  188. .encode('utf-8')).decode('utf-8')
  189. else:
  190. referrer = None
  191. def sign_url(unsigned_url):
  192. if ks:
  193. unsigned_url += '/ks/%s' % ks
  194. if referrer:
  195. unsigned_url += '?referrer=%s' % referrer
  196. return unsigned_url
  197. data_url = info['dataUrl']
  198. if '/flvclipper/' in data_url:
  199. data_url = re.sub(r'/flvclipper/.*', '/serveFlavor', data_url)
  200. formats = []
  201. for f in flavor_assets:
  202. # Continue if asset is not ready
  203. if f['status'] != 2:
  204. continue
  205. video_url = sign_url(
  206. '%s/flavorId/%s' % (data_url, f['id']))
  207. formats.append({
  208. 'format_id': '%(fileExt)s-%(bitrate)s' % f,
  209. 'ext': f.get('fileExt'),
  210. 'tbr': int_or_none(f['bitrate']),
  211. 'fps': int_or_none(f.get('frameRate')),
  212. 'filesize_approx': int_or_none(f.get('size'), invscale=1024),
  213. 'container': f.get('containerFormat'),
  214. 'vcodec': f.get('videoCodecId'),
  215. 'height': int_or_none(f.get('height')),
  216. 'width': int_or_none(f.get('width')),
  217. 'url': video_url,
  218. })
  219. if '/playManifest/' in data_url:
  220. m3u8_url = sign_url(data_url.replace(
  221. 'format/url', 'format/applehttp'))
  222. formats.extend(self._extract_m3u8_formats(
  223. m3u8_url, entry_id, 'mp4', 'm3u8_native',
  224. m3u8_id='hls', fatal=False))
  225. self._check_formats(formats, entry_id)
  226. self._sort_formats(formats)
  227. return {
  228. 'id': entry_id,
  229. 'title': info['name'],
  230. 'formats': formats,
  231. 'description': clean_html(info.get('description')),
  232. 'thumbnail': info.get('thumbnailUrl'),
  233. 'duration': info.get('duration'),
  234. 'timestamp': info.get('createdAt'),
  235. 'uploader_id': info.get('userId'),
  236. 'view_count': info.get('plays'),
  237. }