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.

171 lines
6.3 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 .common import InfoExtractor
  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(InfoExtractor):
  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' or n.attrib.get('title') == 'Expired')
  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, mime = caption.get('lang', 'en'), caption.get('src'), caption.get('type')
  91. subtitles[lang] = [{
  92. 'ext': 'srt' if mime == 'text/srt' else 'ttml',
  93. 'url': src,
  94. }]
  95. head = meta.find(_x('smil:head'))
  96. body = meta.find(_x('smil:body'))
  97. f4m_node = body.find(_x('smil:seq//smil:video'))
  98. if f4m_node is not None and '.f4m' in f4m_node.attrib['src']:
  99. f4m_url = f4m_node.attrib['src']
  100. if 'manifest.f4m?' not in f4m_url:
  101. f4m_url += '?'
  102. # the parameters are from syfy.com, other sites may use others,
  103. # they also work for nbc.com
  104. f4m_url += '&g=UXWGVKRWHFSP&hdcore=3.0.3'
  105. formats = self._extract_f4m_formats(f4m_url, video_id)
  106. else:
  107. formats = []
  108. switch = body.find(_x('smil:switch'))
  109. if switch is not None:
  110. base_url = head.find(_x('smil:meta')).attrib['base']
  111. for f in switch.findall(_x('smil:video')):
  112. attr = f.attrib
  113. width = int(attr['width'])
  114. height = int(attr['height'])
  115. vbr = int(attr['system-bitrate']) // 1000
  116. format_id = '%dx%d_%dk' % (width, height, vbr)
  117. formats.append({
  118. 'format_id': format_id,
  119. 'url': base_url,
  120. 'play_path': 'mp4:' + attr['src'],
  121. 'ext': 'flv',
  122. 'width': width,
  123. 'height': height,
  124. 'vbr': vbr,
  125. })
  126. else:
  127. switch = body.find(_x('smil:seq//smil:switch'))
  128. for f in switch.findall(_x('smil:video')):
  129. attr = f.attrib
  130. vbr = int(attr['system-bitrate']) // 1000
  131. ext = determine_ext(attr['src'])
  132. if ext == 'once':
  133. ext = 'mp4'
  134. formats.append({
  135. 'format_id': compat_str(vbr),
  136. 'url': attr['src'],
  137. 'vbr': vbr,
  138. 'ext': ext,
  139. })
  140. self._sort_formats(formats)
  141. return {
  142. 'id': video_id,
  143. 'title': info['title'],
  144. 'subtitles': subtitles,
  145. 'formats': formats,
  146. 'description': info['description'],
  147. 'thumbnail': info['defaultThumbnailUrl'],
  148. 'duration': info['duration'] // 1000,
  149. }