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.

195 lines
7.3 KiB

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