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.

214 lines
8.0 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<media>(?:[^/]+/)+select/media/)|(?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. 'url': 'https://player.theplatform.com/p/D6x-PC/pulse_preview/embed/select/media/yMBg9E8KFxZD',
  54. 'info_dict': {
  55. 'id': 'yMBg9E8KFxZD',
  56. 'ext': 'mp4',
  57. 'description': 'md5:644ad9188d655b742f942bf2e06b002d',
  58. 'title': 'HIGHLIGHTS: USA bag first ever series Cup win',
  59. }
  60. }, {
  61. 'url': 'http://player.theplatform.com/p/NnzsPC/widget/select/media/4Y0TlYUr_ZT7',
  62. 'only_matching': True,
  63. }]
  64. @staticmethod
  65. def _sign_url(url, sig_key, sig_secret, life=600, include_qs=False):
  66. flags = '10' if include_qs else '00'
  67. expiration_date = '%x' % (int(time.time()) + life)
  68. def str_to_hex(str):
  69. return binascii.b2a_hex(str.encode('ascii')).decode('ascii')
  70. def hex_to_str(hex):
  71. return binascii.a2b_hex(hex)
  72. relative_path = url.split('http://link.theplatform.com/s/')[1].split('?')[0]
  73. clear_text = hex_to_str(flags + expiration_date + str_to_hex(relative_path))
  74. checksum = hmac.new(sig_key.encode('ascii'), clear_text, hashlib.sha1).hexdigest()
  75. sig = flags + expiration_date + checksum + str_to_hex(sig_secret)
  76. return '%s&sig=%s' % (url, sig)
  77. def _real_extract(self, url):
  78. url, smuggled_data = unsmuggle_url(url, {})
  79. mobj = re.match(self._VALID_URL, url)
  80. provider_id = mobj.group('provider_id')
  81. video_id = mobj.group('id')
  82. if not provider_id:
  83. provider_id = 'dJ5BDC'
  84. path = provider_id
  85. if mobj.group('media'):
  86. path += '/media'
  87. path += '/' + video_id
  88. if smuggled_data.get('force_smil_url', False):
  89. smil_url = url
  90. elif mobj.group('config'):
  91. config_url = url + '&form=json'
  92. config_url = config_url.replace('swf/', 'config/')
  93. config_url = config_url.replace('onsite/', 'onsite/config/')
  94. config = self._download_json(config_url, video_id, 'Downloading config')
  95. if 'releaseUrl' in config:
  96. release_url = config['releaseUrl']
  97. else:
  98. release_url = 'http://link.theplatform.com/s/%s?mbr=true' % path
  99. smil_url = release_url + '&format=SMIL&formats=MPEG4&manifest=f4m'
  100. else:
  101. smil_url = 'http://link.theplatform.com/s/%s/meta.smil?format=smil&mbr=true' % path
  102. sig = smuggled_data.get('sig')
  103. if sig:
  104. smil_url = self._sign_url(smil_url, sig['key'], sig['secret'])
  105. meta = self._download_xml(smil_url, video_id)
  106. try:
  107. error_msg = next(
  108. n.attrib['abstract']
  109. for n in meta.findall(_x('.//smil:ref'))
  110. if n.attrib.get('title') == 'Geographic Restriction' or n.attrib.get('title') == 'Expired')
  111. except StopIteration:
  112. pass
  113. else:
  114. raise ExtractorError(error_msg, expected=True)
  115. info_url = 'http://link.theplatform.com/s/%s?format=preview' % path
  116. info_json = self._download_webpage(info_url, video_id)
  117. info = json.loads(info_json)
  118. subtitles = {}
  119. captions = info.get('captions')
  120. if isinstance(captions, list):
  121. for caption in captions:
  122. lang, src, mime = caption.get('lang', 'en'), caption.get('src'), caption.get('type')
  123. subtitles[lang] = [{
  124. 'ext': 'srt' if mime == 'text/srt' else 'ttml',
  125. 'url': src,
  126. }]
  127. head = meta.find(_x('smil:head'))
  128. body = meta.find(_x('smil:body'))
  129. f4m_node = body.find(_x('smil:seq//smil:video'))
  130. if f4m_node is None:
  131. f4m_node = body.find(_x('smil:seq/smil:video'))
  132. if f4m_node is not None and '.f4m' in f4m_node.attrib['src']:
  133. f4m_url = f4m_node.attrib['src']
  134. if 'manifest.f4m?' not in f4m_url:
  135. f4m_url += '?'
  136. # the parameters are from syfy.com, other sites may use others,
  137. # they also work for nbc.com
  138. f4m_url += '&g=UXWGVKRWHFSP&hdcore=3.0.3'
  139. formats = self._extract_f4m_formats(f4m_url, video_id)
  140. else:
  141. formats = []
  142. switch = body.find(_x('smil:switch'))
  143. if switch is None:
  144. switch = body.find(_x('smil:par//smil:switch'))
  145. if switch is None:
  146. switch = body.find(_x('smil:par/smil:switch'))
  147. if switch is None:
  148. switch = body.find(_x('smil:par'))
  149. if switch is not None:
  150. base_url = head.find(_x('smil:meta')).attrib['base']
  151. for f in switch.findall(_x('smil:video')):
  152. attr = f.attrib
  153. width = int_or_none(attr.get('width'))
  154. height = int_or_none(attr.get('height'))
  155. vbr = int_or_none(attr.get('system-bitrate'), 1000)
  156. format_id = '%dx%d_%dk' % (width, height, vbr)
  157. formats.append({
  158. 'format_id': format_id,
  159. 'url': base_url,
  160. 'play_path': 'mp4:' + attr['src'],
  161. 'ext': 'flv',
  162. 'width': width,
  163. 'height': height,
  164. 'vbr': vbr,
  165. })
  166. else:
  167. switch = body.find(_x('smil:seq//smil:switch'))
  168. if switch is None:
  169. switch = body.find(_x('smil:seq/smil:switch'))
  170. for f in switch.findall(_x('smil:video')):
  171. attr = f.attrib
  172. vbr = int_or_none(attr.get('system-bitrate'), 1000)
  173. ext = determine_ext(attr['src'])
  174. if ext == 'once':
  175. ext = 'mp4'
  176. formats.append({
  177. 'format_id': compat_str(vbr),
  178. 'url': attr['src'],
  179. 'vbr': vbr,
  180. 'ext': ext,
  181. })
  182. self._sort_formats(formats)
  183. return {
  184. 'id': video_id,
  185. 'title': info['title'],
  186. 'subtitles': subtitles,
  187. 'formats': formats,
  188. 'description': info['description'],
  189. 'thumbnail': info['defaultThumbnailUrl'],
  190. 'duration': int_or_none(info.get('duration'), 1000),
  191. }