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.

151 lines
6.2 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. import base64
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. unescapeHTML,
  8. ExtractorError,
  9. determine_ext,
  10. int_or_none,
  11. )
  12. class OoyalaIE(InfoExtractor):
  13. _VALID_URL = r'(?:ooyala:|https?://.+?\.ooyala\.com/.*?(?:embedCode|ec)=)(?P<id>.+?)(&|$)'
  14. _TESTS = [
  15. {
  16. # From http://it.slashdot.org/story/13/04/25/178216/recovering-data-from-broken-hard-drives-and-ssds-video
  17. 'url': 'http://player.ooyala.com/player.js?embedCode=pxczE2YjpfHfn1f3M-ykG_AmJRRn0PD8',
  18. 'info_dict': {
  19. 'id': 'pxczE2YjpfHfn1f3M-ykG_AmJRRn0PD8',
  20. 'ext': 'mp4',
  21. 'title': 'Explaining Data Recovery from Hard Drives and SSDs',
  22. 'description': 'How badly damaged does a drive have to be to defeat Russell and his crew? Apparently, smashed to bits.',
  23. },
  24. }, {
  25. # Only available for ipad
  26. 'url': 'http://player.ooyala.com/player.js?embedCode=x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0',
  27. 'info_dict': {
  28. 'id': 'x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0',
  29. 'ext': 'mp4',
  30. 'title': 'Simulation Overview - Levels of Simulation',
  31. 'description': '',
  32. },
  33. },
  34. {
  35. # Information available only through SAS api
  36. # From http://community.plm.automation.siemens.com/t5/News-NX-Manufacturing/Tool-Path-Divide/ba-p/4187
  37. 'url': 'http://player.ooyala.com/player.js?embedCode=FiOG81ZTrvckcchQxmalf4aQj590qTEx',
  38. 'md5': 'a84001441b35ea492bc03736e59e7935',
  39. 'info_dict': {
  40. 'id': 'FiOG81ZTrvckcchQxmalf4aQj590qTEx',
  41. 'ext': 'mp4',
  42. 'title': 'Ooyala video',
  43. }
  44. }
  45. ]
  46. @staticmethod
  47. def _url_for_embed_code(embed_code):
  48. return 'http://player.ooyala.com/player.js?embedCode=%s' % embed_code
  49. @classmethod
  50. def _build_url_result(cls, embed_code):
  51. return cls.url_result(cls._url_for_embed_code(embed_code),
  52. ie=cls.ie_key())
  53. def _extract_result(self, info, more_info):
  54. embedCode = info['embedCode']
  55. video_url = info.get('ipad_url') or info['url']
  56. if determine_ext(video_url) == 'm3u8':
  57. formats = self._extract_m3u8_formats(video_url, embedCode, ext='mp4')
  58. else:
  59. formats = [{
  60. 'url': video_url,
  61. 'ext': 'mp4',
  62. }]
  63. return {
  64. 'id': embedCode,
  65. 'title': unescapeHTML(info['title']),
  66. 'formats': formats,
  67. 'description': unescapeHTML(more_info['description']),
  68. 'thumbnail': more_info['promo'],
  69. }
  70. def _real_extract(self, url):
  71. mobj = re.match(self._VALID_URL, url)
  72. embedCode = mobj.group('id')
  73. player_url = 'http://player.ooyala.com/player.js?embedCode=%s' % embedCode
  74. player = self._download_webpage(player_url, embedCode)
  75. mobile_url = self._search_regex(r'mobile_player_url="(.+?)&device="',
  76. player, 'mobile player url')
  77. # Looks like some videos are only available for particular devices
  78. # (e.g. http://player.ooyala.com/player.js?embedCode=x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0
  79. # is only available for ipad)
  80. # Working around with fetching URLs for all the devices found starting with 'unknown'
  81. # until we succeed or eventually fail for each device.
  82. devices = re.findall(r'device\s*=\s*"([^"]+)";', player)
  83. devices.remove('unknown')
  84. devices.insert(0, 'unknown')
  85. for device in devices:
  86. mobile_player = self._download_webpage(
  87. '%s&device=%s' % (mobile_url, device), embedCode,
  88. 'Downloading mobile player JS for %s device' % device)
  89. videos_info = self._search_regex(
  90. r'var streams=window.oo_testEnv\?\[\]:eval\("\((\[{.*?}\])\)"\);',
  91. mobile_player, 'info', fatal=False, default=None)
  92. if videos_info:
  93. break
  94. if not videos_info:
  95. formats = []
  96. auth_data = self._download_json(
  97. 'http://player.ooyala.com/sas/player_api/v1/authorization/embed_code/%s/%s?domain=www.example.org&supportedFormats=mp4,webm' % (embedCode, embedCode),
  98. embedCode)
  99. cur_auth_data = auth_data['authorization_data'][embedCode]
  100. for stream in cur_auth_data['streams']:
  101. formats.append({
  102. 'url': base64.b64decode(stream['url']['data'].encode('ascii')).decode('utf-8'),
  103. 'ext': stream.get('delivery_type'),
  104. 'format': stream.get('video_codec'),
  105. 'format_id': stream.get('profile'),
  106. 'width': int_or_none(stream.get('width')),
  107. 'height': int_or_none(stream.get('height')),
  108. 'abr': int_or_none(stream.get('audio_bitrate')),
  109. 'vbr': int_or_none(stream.get('video_bitrate')),
  110. })
  111. if formats:
  112. return {
  113. 'id': embedCode,
  114. 'formats': formats,
  115. 'title': 'Ooyala video',
  116. }
  117. if not cur_auth_data['authorized']:
  118. raise ExtractorError(cur_auth_data['message'], expected=True)
  119. if not videos_info:
  120. raise ExtractorError('Unable to extract info')
  121. videos_info = videos_info.replace('\\"', '"')
  122. videos_more_info = self._search_regex(
  123. r'eval\("\(({.*?\\"promo\\".*?})\)"', mobile_player, 'more info').replace('\\"', '"')
  124. videos_info = json.loads(videos_info)
  125. videos_more_info = json.loads(videos_more_info)
  126. if videos_more_info.get('lineup'):
  127. videos = [self._extract_result(info, more_info) for (info, more_info) in zip(videos_info, videos_more_info['lineup'])]
  128. return {
  129. '_type': 'playlist',
  130. 'id': embedCode,
  131. 'title': unescapeHTML(videos_more_info['title']),
  132. 'entries': videos,
  133. }
  134. else:
  135. return self._extract_result(videos_info[0], videos_more_info)