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.

187 lines
7.7 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. import base64
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. int_or_none,
  7. float_or_none,
  8. ExtractorError,
  9. unsmuggle_url,
  10. determine_ext,
  11. )
  12. from ..compat import compat_urllib_parse_urlencode
  13. class OoyalaBaseIE(InfoExtractor):
  14. _PLAYER_BASE = 'http://player.ooyala.com/'
  15. _CONTENT_TREE_BASE = _PLAYER_BASE + 'player_api/v1/content_tree/'
  16. _AUTHORIZATION_URL_TEMPLATE = _PLAYER_BASE + 'sas/player_api/v2/authorization/embed_code/%s/%s?'
  17. def _extract(self, content_tree_url, video_id, domain='example.org', supportedformats=None):
  18. content_tree = self._download_json(content_tree_url, video_id)['content_tree']
  19. metadata = content_tree[list(content_tree)[0]]
  20. embed_code = metadata['embed_code']
  21. pcode = metadata.get('asset_pcode') or embed_code
  22. title = metadata['title']
  23. auth_data = self._download_json(
  24. self._AUTHORIZATION_URL_TEMPLATE % (pcode, embed_code) +
  25. compat_urllib_parse_urlencode({
  26. 'domain': domain,
  27. 'supportedFormats': supportedformats or 'mp4,rtmp,m3u8,hds',
  28. }), video_id)
  29. cur_auth_data = auth_data['authorization_data'][embed_code]
  30. urls = []
  31. formats = []
  32. if cur_auth_data['authorized']:
  33. for stream in cur_auth_data['streams']:
  34. s_url = base64.b64decode(
  35. stream['url']['data'].encode('ascii')).decode('utf-8')
  36. if s_url in urls:
  37. continue
  38. urls.append(s_url)
  39. ext = determine_ext(s_url, None)
  40. delivery_type = stream['delivery_type']
  41. if delivery_type == 'hls' or ext == 'm3u8':
  42. formats.extend(self._extract_m3u8_formats(
  43. re.sub(r'/ip(?:ad|hone)/', '/all/', s_url), embed_code, 'mp4', 'm3u8_native',
  44. m3u8_id='hls', fatal=False))
  45. elif delivery_type == 'hds' or ext == 'f4m':
  46. formats.extend(self._extract_f4m_formats(
  47. s_url + '?hdcore=3.7.0', embed_code, f4m_id='hds', fatal=False))
  48. elif ext == 'smil':
  49. formats.extend(self._extract_smil_formats(
  50. s_url, embed_code, fatal=False))
  51. else:
  52. formats.append({
  53. 'url': s_url,
  54. 'ext': ext or stream.get('delivery_type'),
  55. 'vcodec': stream.get('video_codec'),
  56. 'format_id': delivery_type,
  57. 'width': int_or_none(stream.get('width')),
  58. 'height': int_or_none(stream.get('height')),
  59. 'abr': int_or_none(stream.get('audio_bitrate')),
  60. 'vbr': int_or_none(stream.get('video_bitrate')),
  61. 'fps': float_or_none(stream.get('framerate')),
  62. })
  63. else:
  64. raise ExtractorError('%s said: %s' % (
  65. self.IE_NAME, cur_auth_data['message']), expected=True)
  66. self._sort_formats(formats)
  67. subtitles = {}
  68. for lang, sub in metadata.get('closed_captions_vtt', {}).get('captions', {}).items():
  69. sub_url = sub.get('url')
  70. if not sub_url:
  71. continue
  72. subtitles[lang] = [{
  73. 'url': sub_url,
  74. }]
  75. return {
  76. 'id': embed_code,
  77. 'title': title,
  78. 'description': metadata.get('description'),
  79. 'thumbnail': metadata.get('thumbnail_image') or metadata.get('promo_image'),
  80. 'duration': float_or_none(metadata.get('duration'), 1000),
  81. 'subtitles': subtitles,
  82. 'formats': formats,
  83. }
  84. class OoyalaIE(OoyalaBaseIE):
  85. _VALID_URL = r'(?:ooyala:|https?://.+?\.ooyala\.com/.*?(?:embedCode|ec)=)(?P<id>.+?)(&|$)'
  86. _TESTS = [
  87. {
  88. # From http://it.slashdot.org/story/13/04/25/178216/recovering-data-from-broken-hard-drives-and-ssds-video
  89. 'url': 'http://player.ooyala.com/player.js?embedCode=pxczE2YjpfHfn1f3M-ykG_AmJRRn0PD8',
  90. 'info_dict': {
  91. 'id': 'pxczE2YjpfHfn1f3M-ykG_AmJRRn0PD8',
  92. 'ext': 'mp4',
  93. 'title': 'Explaining Data Recovery from Hard Drives and SSDs',
  94. 'description': 'How badly damaged does a drive have to be to defeat Russell and his crew? Apparently, smashed to bits.',
  95. 'duration': 853.386,
  96. },
  97. # The video in the original webpage now uses PlayWire
  98. 'skip': 'Ooyala said: movie expired',
  99. }, {
  100. # Only available for ipad
  101. 'url': 'http://player.ooyala.com/player.js?embedCode=x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0',
  102. 'info_dict': {
  103. 'id': 'x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0',
  104. 'ext': 'mp4',
  105. 'title': 'Simulation Overview - Levels of Simulation',
  106. 'duration': 194.948,
  107. },
  108. },
  109. {
  110. # Information available only through SAS api
  111. # From http://community.plm.automation.siemens.com/t5/News-NX-Manufacturing/Tool-Path-Divide/ba-p/4187
  112. 'url': 'http://player.ooyala.com/player.js?embedCode=FiOG81ZTrvckcchQxmalf4aQj590qTEx',
  113. 'md5': 'a84001441b35ea492bc03736e59e7935',
  114. 'info_dict': {
  115. 'id': 'FiOG81ZTrvckcchQxmalf4aQj590qTEx',
  116. 'ext': 'mp4',
  117. 'title': 'Divide Tool Path.mp4',
  118. 'duration': 204.405,
  119. }
  120. }
  121. ]
  122. @staticmethod
  123. def _url_for_embed_code(embed_code):
  124. return 'http://player.ooyala.com/player.js?embedCode=%s' % embed_code
  125. @classmethod
  126. def _build_url_result(cls, embed_code):
  127. return cls.url_result(cls._url_for_embed_code(embed_code),
  128. ie=cls.ie_key())
  129. def _real_extract(self, url):
  130. url, smuggled_data = unsmuggle_url(url, {})
  131. embed_code = self._match_id(url)
  132. domain = smuggled_data.get('domain')
  133. supportedformats = smuggled_data.get('supportedformats')
  134. content_tree_url = self._CONTENT_TREE_BASE + 'embed_code/%s/%s' % (embed_code, embed_code)
  135. return self._extract(content_tree_url, embed_code, domain, supportedformats)
  136. class OoyalaExternalIE(OoyalaBaseIE):
  137. _VALID_URL = r'''(?x)
  138. (?:
  139. ooyalaexternal:|
  140. https?://.+?\.ooyala\.com/.*?\bexternalId=
  141. )
  142. (?P<partner_id>[^:]+)
  143. :
  144. (?P<id>.+)
  145. (?:
  146. :|
  147. .*?&pcode=
  148. )
  149. (?P<pcode>.+?)
  150. (?:&|$)
  151. '''
  152. _TEST = {
  153. 'url': 'https://player.ooyala.com/player.js?externalId=espn:10365079&pcode=1kNG061cgaoolOncv54OAO1ceO-I&adSetCode=91cDU6NuXTGKz3OdjOxFdAgJVtQcKJnI&callback=handleEvents&hasModuleParams=1&height=968&playerBrandingId=7af3bd04449c444c964f347f11873075&targetReplaceId=videoPlayer&width=1656&wmode=opaque&allowScriptAccess=always',
  154. 'info_dict': {
  155. 'id': 'FkYWtmazr6Ed8xmvILvKLWjd4QvYZpzG',
  156. 'ext': 'mp4',
  157. 'title': 'dm_140128_30for30Shorts___JudgingJewellv2',
  158. 'duration': 1302.0,
  159. },
  160. 'params': {
  161. # m3u8 download
  162. 'skip_download': True,
  163. },
  164. }
  165. def _real_extract(self, url):
  166. partner_id, video_id, pcode = re.match(self._VALID_URL, url).groups()
  167. content_tree_url = self._CONTENT_TREE_BASE + 'external_id/%s/%s:%s' % (pcode, partner_id, video_id)
  168. return self._extract(content_tree_url, video_id)