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.

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