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.

175 lines
6.4 KiB

10 years ago
10 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. import time
  5. import hmac
  6. import binascii
  7. import hashlib
  8. from .subtitles import SubtitlesInfoExtractor
  9. from ..compat import (
  10. compat_str,
  11. )
  12. from ..utils import (
  13. determine_ext,
  14. ExtractorError,
  15. xpath_with_ns,
  16. unsmuggle_url,
  17. )
  18. _x = lambda p: xpath_with_ns(p, {'smil': 'http://www.w3.org/2005/SMIL21/Language'})
  19. class ThePlatformIE(SubtitlesInfoExtractor):
  20. _VALID_URL = r'''(?x)
  21. (?:https?://(?:link|player)\.theplatform\.com/[sp]/(?P<provider_id>[^/]+)/
  22. (?P<config>(?:[^/\?]+/(?:swf|config)|onsite)/select/)?
  23. |theplatform:)(?P<id>[^/\?&]+)'''
  24. _TEST = {
  25. # from http://www.metacafe.com/watch/cb-e9I_cZgTgIPd/blackberrys_big_bold_z30/
  26. 'url': 'http://link.theplatform.com/s/dJ5BDC/e9I_cZgTgIPd/meta.smil?format=smil&Tracking=true&mbr=true',
  27. 'info_dict': {
  28. 'id': 'e9I_cZgTgIPd',
  29. 'ext': 'flv',
  30. 'title': 'Blackberry\'s big, bold Z30',
  31. 'description': 'The Z30 is Blackberry\'s biggest, baddest mobile messaging device yet.',
  32. 'duration': 247,
  33. },
  34. 'params': {
  35. # rtmp download
  36. 'skip_download': True,
  37. },
  38. }
  39. @staticmethod
  40. def _sign_url(url, sig_key, sig_secret, life=600, include_qs=False):
  41. flags = '10' if include_qs else '00'
  42. expiration_date = '%x' % (int(time.time()) + life)
  43. def str_to_hex(str):
  44. return binascii.b2a_hex(str.encode('ascii')).decode('ascii')
  45. def hex_to_str(hex):
  46. return binascii.a2b_hex(hex)
  47. relative_path = url.split('http://link.theplatform.com/s/')[1].split('?')[0]
  48. clear_text = hex_to_str(flags + expiration_date + str_to_hex(relative_path))
  49. checksum = hmac.new(sig_key.encode('ascii'), clear_text, hashlib.sha1).hexdigest()
  50. sig = flags + expiration_date + checksum + str_to_hex(sig_secret)
  51. return '%s&sig=%s' % (url, sig)
  52. def _real_extract(self, url):
  53. url, smuggled_data = unsmuggle_url(url, {})
  54. mobj = re.match(self._VALID_URL, url)
  55. provider_id = mobj.group('provider_id')
  56. video_id = mobj.group('id')
  57. if not provider_id:
  58. provider_id = 'dJ5BDC'
  59. if smuggled_data.get('force_smil_url', False):
  60. smil_url = url
  61. elif mobj.group('config'):
  62. config_url = url + '&form=json'
  63. config_url = config_url.replace('swf/', 'config/')
  64. config_url = config_url.replace('onsite/', 'onsite/config/')
  65. config = self._download_json(config_url, video_id, 'Downloading config')
  66. smil_url = config['releaseUrl'] + '&format=SMIL&formats=MPEG4&manifest=f4m'
  67. else:
  68. smil_url = ('http://link.theplatform.com/s/{0}/{1}/meta.smil?'
  69. 'format=smil&mbr=true'.format(provider_id, video_id))
  70. sig = smuggled_data.get('sig')
  71. if sig:
  72. smil_url = self._sign_url(smil_url, sig['key'], sig['secret'])
  73. meta = self._download_xml(smil_url, video_id)
  74. try:
  75. error_msg = next(
  76. n.attrib['abstract']
  77. for n in meta.findall(_x('.//smil:ref'))
  78. if n.attrib.get('title') == 'Geographic Restriction')
  79. except StopIteration:
  80. pass
  81. else:
  82. raise ExtractorError(error_msg, expected=True)
  83. info_url = 'http://link.theplatform.com/s/{0}/{1}?format=preview'.format(provider_id, video_id)
  84. info_json = self._download_webpage(info_url, video_id)
  85. info = json.loads(info_json)
  86. subtitles = {}
  87. captions = info.get('captions')
  88. if isinstance(captions, list):
  89. for caption in captions:
  90. lang, src = caption.get('lang'), caption.get('src')
  91. if lang and src:
  92. subtitles[lang] = src
  93. if self._downloader.params.get('listsubtitles', False):
  94. self._list_available_subtitles(video_id, subtitles)
  95. return
  96. subtitles = self.extract_subtitles(video_id, subtitles)
  97. head = meta.find(_x('smil:head'))
  98. body = meta.find(_x('smil:body'))
  99. f4m_node = body.find(_x('smil:seq//smil:video'))
  100. if f4m_node is not None and '.f4m' in f4m_node.attrib['src']:
  101. f4m_url = f4m_node.attrib['src']
  102. if 'manifest.f4m?' not in f4m_url:
  103. f4m_url += '?'
  104. # the parameters are from syfy.com, other sites may use others,
  105. # they also work for nbc.com
  106. f4m_url += '&g=UXWGVKRWHFSP&hdcore=3.0.3'
  107. formats = self._extract_f4m_formats(f4m_url, video_id)
  108. else:
  109. formats = []
  110. switch = body.find(_x('smil:switch'))
  111. if switch is not None:
  112. base_url = head.find(_x('smil:meta')).attrib['base']
  113. for f in switch.findall(_x('smil:video')):
  114. attr = f.attrib
  115. width = int(attr['width'])
  116. height = int(attr['height'])
  117. vbr = int(attr['system-bitrate']) // 1000
  118. format_id = '%dx%d_%dk' % (width, height, vbr)
  119. formats.append({
  120. 'format_id': format_id,
  121. 'url': base_url,
  122. 'play_path': 'mp4:' + attr['src'],
  123. 'ext': 'flv',
  124. 'width': width,
  125. 'height': height,
  126. 'vbr': vbr,
  127. })
  128. else:
  129. switch = body.find(_x('smil:seq//smil:switch'))
  130. for f in switch.findall(_x('smil:video')):
  131. attr = f.attrib
  132. vbr = int(attr['system-bitrate']) // 1000
  133. ext = determine_ext(attr['src'])
  134. if ext == 'once':
  135. ext = 'mp4'
  136. formats.append({
  137. 'format_id': compat_str(vbr),
  138. 'url': attr['src'],
  139. 'vbr': vbr,
  140. 'ext': ext,
  141. })
  142. self._sort_formats(formats)
  143. return {
  144. 'id': video_id,
  145. 'title': info['title'],
  146. 'subtitles': subtitles,
  147. 'formats': formats,
  148. 'description': info['description'],
  149. 'thumbnail': info['defaultThumbnailUrl'],
  150. 'duration': info['duration'] // 1000,
  151. }