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.

207 lines
8.5 KiB

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