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.

210 lines
7.9 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. smil_url = config['releaseUrl'] + '&format=SMIL&formats=MPEG4&manifest=f4m'
  96. else:
  97. smil_url = 'http://link.theplatform.com/s/%s/meta.smil?format=smil&mbr=true' % path
  98. sig = smuggled_data.get('sig')
  99. if sig:
  100. smil_url = self._sign_url(smil_url, sig['key'], sig['secret'])
  101. meta = self._download_xml(smil_url, video_id)
  102. try:
  103. error_msg = next(
  104. n.attrib['abstract']
  105. for n in meta.findall(_x('.//smil:ref'))
  106. if n.attrib.get('title') == 'Geographic Restriction' or n.attrib.get('title') == 'Expired')
  107. except StopIteration:
  108. pass
  109. else:
  110. raise ExtractorError(error_msg, expected=True)
  111. info_url = 'http://link.theplatform.com/s/%s?format=preview' % path
  112. info_json = self._download_webpage(info_url, video_id)
  113. info = json.loads(info_json)
  114. subtitles = {}
  115. captions = info.get('captions')
  116. if isinstance(captions, list):
  117. for caption in captions:
  118. lang, src, mime = caption.get('lang', 'en'), caption.get('src'), caption.get('type')
  119. subtitles[lang] = [{
  120. 'ext': 'srt' if mime == 'text/srt' else 'ttml',
  121. 'url': src,
  122. }]
  123. head = meta.find(_x('smil:head'))
  124. body = meta.find(_x('smil:body'))
  125. f4m_node = body.find(_x('smil:seq//smil:video'))
  126. if f4m_node is None:
  127. f4m_node = body.find(_x('smil:seq/smil:video'))
  128. if f4m_node is not None and '.f4m' in f4m_node.attrib['src']:
  129. f4m_url = f4m_node.attrib['src']
  130. if 'manifest.f4m?' not in f4m_url:
  131. f4m_url += '?'
  132. # the parameters are from syfy.com, other sites may use others,
  133. # they also work for nbc.com
  134. f4m_url += '&g=UXWGVKRWHFSP&hdcore=3.0.3'
  135. formats = self._extract_f4m_formats(f4m_url, video_id)
  136. else:
  137. formats = []
  138. switch = body.find(_x('smil:switch'))
  139. if switch is None:
  140. switch = body.find(_x('smil:par//smil:switch'))
  141. if switch is None:
  142. switch = body.find(_x('smil:par/smil:switch'))
  143. if switch is None:
  144. switch = body.find(_x('smil:par'))
  145. if switch is not None:
  146. base_url = head.find(_x('smil:meta')).attrib['base']
  147. for f in switch.findall(_x('smil:video')):
  148. attr = f.attrib
  149. width = int_or_none(attr.get('width'))
  150. height = int_or_none(attr.get('height'))
  151. vbr = int_or_none(attr.get('system-bitrate'), 1000)
  152. format_id = '%dx%d_%dk' % (width, height, vbr)
  153. formats.append({
  154. 'format_id': format_id,
  155. 'url': base_url,
  156. 'play_path': 'mp4:' + attr['src'],
  157. 'ext': 'flv',
  158. 'width': width,
  159. 'height': height,
  160. 'vbr': vbr,
  161. })
  162. else:
  163. switch = body.find(_x('smil:seq//smil:switch'))
  164. if switch is None:
  165. switch = body.find(_x('smil:seq/smil:switch'))
  166. for f in switch.findall(_x('smil:video')):
  167. attr = f.attrib
  168. vbr = int_or_none(attr.get('system-bitrate'), 1000)
  169. ext = determine_ext(attr['src'])
  170. if ext == 'once':
  171. ext = 'mp4'
  172. formats.append({
  173. 'format_id': compat_str(vbr),
  174. 'url': attr['src'],
  175. 'vbr': vbr,
  176. 'ext': ext,
  177. })
  178. self._sort_formats(formats)
  179. return {
  180. 'id': video_id,
  181. 'title': info['title'],
  182. 'subtitles': subtitles,
  183. 'formats': formats,
  184. 'description': info['description'],
  185. 'thumbnail': info['defaultThumbnailUrl'],
  186. 'duration': int_or_none(info.get('duration'), 1000),
  187. }