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.

169 lines
6.2 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 mobj.group('config'):
  60. config_url = url + '&form=json'
  61. config_url = config_url.replace('swf/', 'config/')
  62. config_url = config_url.replace('onsite/', 'onsite/config/')
  63. config = self._download_json(config_url, video_id, 'Downloading config')
  64. smil_url = config['releaseUrl'] + '&format=SMIL&formats=MPEG4&manifest=f4m'
  65. else:
  66. smil_url = ('http://link.theplatform.com/s/{0}/{1}/meta.smil?'
  67. 'format=smil&mbr=true'.format(provider_id, video_id))
  68. sig = smuggled_data.get('sig')
  69. if sig:
  70. smil_url = self._sign_url(smil_url, sig['key'], sig['secret'])
  71. meta = self._download_xml(smil_url, video_id)
  72. try:
  73. error_msg = next(
  74. n.attrib['abstract']
  75. for n in meta.findall(_x('.//smil:ref'))
  76. if n.attrib.get('title') == 'Geographic Restriction')
  77. except StopIteration:
  78. pass
  79. else:
  80. raise ExtractorError(error_msg, expected=True)
  81. info_url = 'http://link.theplatform.com/s/{0}/{1}?format=preview'.format(provider_id, video_id)
  82. info_json = self._download_webpage(info_url, video_id)
  83. info = json.loads(info_json)
  84. subtitles = {}
  85. captions = info.get('captions')
  86. if isinstance(captions, list):
  87. for caption in captions:
  88. lang, src, mime = caption.get('lang', 'en'), caption.get('src'), caption.get('type')
  89. subtitles[lang] = [{
  90. 'ext': 'srt' if mime == 'text/srt' else 'ttml',
  91. 'url': src,
  92. }]
  93. head = meta.find(_x('smil:head'))
  94. body = meta.find(_x('smil:body'))
  95. f4m_node = body.find(_x('smil:seq//smil:video'))
  96. if f4m_node is not None and '.f4m' in f4m_node.attrib['src']:
  97. f4m_url = f4m_node.attrib['src']
  98. if 'manifest.f4m?' not in f4m_url:
  99. f4m_url += '?'
  100. # the parameters are from syfy.com, other sites may use others,
  101. # they also work for nbc.com
  102. f4m_url += '&g=UXWGVKRWHFSP&hdcore=3.0.3'
  103. formats = self._extract_f4m_formats(f4m_url, video_id)
  104. else:
  105. formats = []
  106. switch = body.find(_x('smil:switch'))
  107. if switch is not None:
  108. base_url = head.find(_x('smil:meta')).attrib['base']
  109. for f in switch.findall(_x('smil:video')):
  110. attr = f.attrib
  111. width = int(attr['width'])
  112. height = int(attr['height'])
  113. vbr = int(attr['system-bitrate']) // 1000
  114. format_id = '%dx%d_%dk' % (width, height, vbr)
  115. formats.append({
  116. 'format_id': format_id,
  117. 'url': base_url,
  118. 'play_path': 'mp4:' + attr['src'],
  119. 'ext': 'flv',
  120. 'width': width,
  121. 'height': height,
  122. 'vbr': vbr,
  123. })
  124. else:
  125. switch = body.find(_x('smil:seq//smil:switch'))
  126. for f in switch.findall(_x('smil:video')):
  127. attr = f.attrib
  128. vbr = int(attr['system-bitrate']) // 1000
  129. ext = determine_ext(attr['src'])
  130. if ext == 'once':
  131. ext = 'mp4'
  132. formats.append({
  133. 'format_id': compat_str(vbr),
  134. 'url': attr['src'],
  135. 'vbr': vbr,
  136. 'ext': ext,
  137. })
  138. self._sort_formats(formats)
  139. return {
  140. 'id': video_id,
  141. 'title': info['title'],
  142. 'subtitles': subtitles,
  143. 'formats': formats,
  144. 'description': info['description'],
  145. 'thumbnail': info['defaultThumbnailUrl'],
  146. 'duration': info['duration'] // 1000,
  147. }