ooyala.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. unescapeHTML,
  7. ExtractorError,
  8. determine_ext,
  9. )
  10. class OoyalaIE(InfoExtractor):
  11. _VALID_URL = r'(?:ooyala:|https?://.+?\.ooyala\.com/.*?(?:embedCode|ec)=)(?P<id>.+?)(&|$)'
  12. _TESTS = [
  13. {
  14. # From http://it.slashdot.org/story/13/04/25/178216/recovering-data-from-broken-hard-drives-and-ssds-video
  15. 'url': 'http://player.ooyala.com/player.js?embedCode=pxczE2YjpfHfn1f3M-ykG_AmJRRn0PD8',
  16. 'info_dict': {
  17. 'id': 'pxczE2YjpfHfn1f3M-ykG_AmJRRn0PD8',
  18. 'ext': 'mp4',
  19. 'title': 'Explaining Data Recovery from Hard Drives and SSDs',
  20. 'description': 'How badly damaged does a drive have to be to defeat Russell and his crew? Apparently, smashed to bits.',
  21. },
  22. }, {
  23. # Only available for ipad
  24. 'url': 'http://player.ooyala.com/player.js?embedCode=x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0',
  25. 'info_dict': {
  26. 'id': 'x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0',
  27. 'ext': 'mp4',
  28. 'title': 'Simulation Overview - Levels of Simulation',
  29. 'description': '',
  30. },
  31. },
  32. ]
  33. @staticmethod
  34. def _url_for_embed_code(embed_code):
  35. return 'http://player.ooyala.com/player.js?embedCode=%s' % embed_code
  36. @classmethod
  37. def _build_url_result(cls, embed_code):
  38. return cls.url_result(cls._url_for_embed_code(embed_code),
  39. ie=cls.ie_key())
  40. def _extract_result(self, info, more_info):
  41. embedCode = info['embedCode']
  42. video_url = info.get('ipad_url') or info['url']
  43. if determine_ext(video_url) == 'm3u8':
  44. formats = self._extract_m3u8_formats(video_url, embedCode, ext='mp4')
  45. else:
  46. formats = [{
  47. 'url': video_url,
  48. 'ext': 'mp4',
  49. }]
  50. return {
  51. 'id': embedCode,
  52. 'title': unescapeHTML(info['title']),
  53. 'formats': formats,
  54. 'description': unescapeHTML(more_info['description']),
  55. 'thumbnail': more_info['promo'],
  56. }
  57. def _real_extract(self, url):
  58. mobj = re.match(self._VALID_URL, url)
  59. embedCode = mobj.group('id')
  60. player_url = 'http://player.ooyala.com/player.js?embedCode=%s' % embedCode
  61. player = self._download_webpage(player_url, embedCode)
  62. mobile_url = self._search_regex(r'mobile_player_url="(.+?)&device="',
  63. player, 'mobile player url')
  64. # Looks like some videos are only available for particular devices
  65. # (e.g. http://player.ooyala.com/player.js?embedCode=x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0
  66. # is only available for ipad)
  67. # Working around with fetching URLs for all the devices found starting with 'unknown'
  68. # until we succeed or eventually fail for each device.
  69. devices = re.findall(r'device\s*=\s*"([^"]+)";', player)
  70. devices.remove('unknown')
  71. devices.insert(0, 'unknown')
  72. for device in devices:
  73. mobile_player = self._download_webpage(
  74. '%s&device=%s' % (mobile_url, device), embedCode,
  75. 'Downloading mobile player JS for %s device' % device)
  76. videos_info = self._search_regex(
  77. r'var streams=window.oo_testEnv\?\[\]:eval\("\((\[{.*?}\])\)"\);',
  78. mobile_player, 'info', fatal=False, default=None)
  79. if videos_info:
  80. break
  81. if not videos_info:
  82. raise ExtractorError('Unable to extract info')
  83. videos_info = videos_info.replace('\\"', '"')
  84. videos_more_info = self._search_regex(
  85. r'eval\("\(({.*?\\"promo\\".*?})\)"', mobile_player, 'more info').replace('\\"', '"')
  86. videos_info = json.loads(videos_info)
  87. videos_more_info = json.loads(videos_more_info)
  88. if videos_more_info.get('lineup'):
  89. videos = [self._extract_result(info, more_info) for (info, more_info) in zip(videos_info, videos_more_info['lineup'])]
  90. return {
  91. '_type': 'playlist',
  92. 'id': embedCode,
  93. 'title': unescapeHTML(videos_more_info['title']),
  94. 'entries': videos,
  95. }
  96. else:
  97. return self._extract_result(videos_info[0], videos_more_info)