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.

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