ooyala.py 9.8 KB

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