theplatform.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 ..utils import (
  10. determine_ext,
  11. ExtractorError,
  12. xpath_with_ns,
  13. unsmuggle_url,
  14. int_or_none,
  15. )
  16. default_ns = 'http://www.w3.org/2005/SMIL21/Language'
  17. _x = lambda p: xpath_with_ns(p, {'smil': default_ns})
  18. class ThePlatformIE(InfoExtractor):
  19. _VALID_URL = r'''(?x)
  20. (?:https?://(?:link|player)\.theplatform\.com/[sp]/(?P<provider_id>[^/]+)/
  21. (?:(?P<media>(?:[^/]+/)+select/media/)|(?P<config>(?:[^/\?]+/(?:swf|config)|onsite)/select/))?
  22. |theplatform:)(?P<id>[^/\?&]+)'''
  23. _TESTS = [{
  24. # from http://www.metacafe.com/watch/cb-e9I_cZgTgIPd/blackberrys_big_bold_z30/
  25. 'url': 'http://link.theplatform.com/s/dJ5BDC/e9I_cZgTgIPd/meta.smil?format=smil&Tracking=true&mbr=true',
  26. 'info_dict': {
  27. 'id': 'e9I_cZgTgIPd',
  28. 'ext': 'flv',
  29. 'title': 'Blackberry\'s big, bold Z30',
  30. 'description': 'The Z30 is Blackberry\'s biggest, baddest mobile messaging device yet.',
  31. 'duration': 247,
  32. },
  33. 'params': {
  34. # rtmp download
  35. 'skip_download': True,
  36. },
  37. }, {
  38. # from http://www.cnet.com/videos/tesla-model-s-a-second-step-towards-a-cleaner-motoring-future/
  39. 'url': 'http://link.theplatform.com/s/kYEXFC/22d_qsQ6MIRT',
  40. 'info_dict': {
  41. 'id': '22d_qsQ6MIRT',
  42. 'ext': 'flv',
  43. 'description': 'md5:ac330c9258c04f9d7512cf26b9595409',
  44. 'title': 'Tesla Model S: A second step towards a cleaner motoring future',
  45. },
  46. 'params': {
  47. # rtmp download
  48. 'skip_download': True,
  49. }
  50. }, {
  51. 'url': 'https://player.theplatform.com/p/D6x-PC/pulse_preview/embed/select/media/yMBg9E8KFxZD',
  52. 'info_dict': {
  53. 'id': 'yMBg9E8KFxZD',
  54. 'ext': 'mp4',
  55. 'description': 'md5:644ad9188d655b742f942bf2e06b002d',
  56. 'title': 'HIGHLIGHTS: USA bag first ever series Cup win',
  57. }
  58. }, {
  59. 'url': 'http://player.theplatform.com/p/NnzsPC/widget/select/media/4Y0TlYUr_ZT7',
  60. 'only_matching': True,
  61. }]
  62. @staticmethod
  63. def _sign_url(url, sig_key, sig_secret, life=600, include_qs=False):
  64. flags = '10' if include_qs else '00'
  65. expiration_date = '%x' % (int(time.time()) + life)
  66. def str_to_hex(str):
  67. return binascii.b2a_hex(str.encode('ascii')).decode('ascii')
  68. def hex_to_str(hex):
  69. return binascii.a2b_hex(hex)
  70. relative_path = url.split('http://link.theplatform.com/s/')[1].split('?')[0]
  71. clear_text = hex_to_str(flags + expiration_date + str_to_hex(relative_path))
  72. checksum = hmac.new(sig_key.encode('ascii'), clear_text, hashlib.sha1).hexdigest()
  73. sig = flags + expiration_date + checksum + str_to_hex(sig_secret)
  74. return '%s&sig=%s' % (url, sig)
  75. def _real_extract(self, url):
  76. url, smuggled_data = unsmuggle_url(url, {})
  77. mobj = re.match(self._VALID_URL, url)
  78. provider_id = mobj.group('provider_id')
  79. video_id = mobj.group('id')
  80. if not provider_id:
  81. provider_id = 'dJ5BDC'
  82. path = provider_id
  83. if mobj.group('media'):
  84. path += '/media'
  85. path += '/' + video_id
  86. if smuggled_data.get('force_smil_url', False):
  87. smil_url = url
  88. elif mobj.group('config'):
  89. config_url = url + '&form=json'
  90. config_url = config_url.replace('swf/', 'config/')
  91. config_url = config_url.replace('onsite/', 'onsite/config/')
  92. config = self._download_json(config_url, video_id, 'Downloading config')
  93. if 'releaseUrl' in config:
  94. release_url = config['releaseUrl']
  95. else:
  96. release_url = 'http://link.theplatform.com/s/%s?mbr=true' % path
  97. smil_url = release_url + '&format=SMIL&formats=MPEG4&manifest=f4m'
  98. else:
  99. smil_url = 'http://link.theplatform.com/s/%s/meta.smil?format=smil&mbr=true' % path
  100. sig = smuggled_data.get('sig')
  101. if sig:
  102. smil_url = self._sign_url(smil_url, sig['key'], sig['secret'])
  103. meta = self._download_xml(smil_url, video_id)
  104. try:
  105. error_msg = next(
  106. n.attrib['abstract']
  107. for n in meta.findall(_x('.//smil:ref'))
  108. if n.attrib.get('title') == 'Geographic Restriction' or n.attrib.get('title') == 'Expired')
  109. except StopIteration:
  110. pass
  111. else:
  112. raise ExtractorError(error_msg, expected=True)
  113. info_url = 'http://link.theplatform.com/s/%s?format=preview' % path
  114. info_json = self._download_webpage(info_url, video_id)
  115. info = json.loads(info_json)
  116. subtitles = {}
  117. captions = info.get('captions')
  118. if isinstance(captions, list):
  119. for caption in captions:
  120. lang, src, mime = caption.get('lang', 'en'), caption.get('src'), caption.get('type')
  121. subtitles[lang] = [{
  122. 'ext': 'srt' if mime == 'text/srt' else 'ttml',
  123. 'url': src,
  124. }]
  125. formats = self._parse_smil_formats(
  126. meta, smil_url, video_id, namespace=default_ns,
  127. # the parameters are from syfy.com, other sites may use others,
  128. # they also work for nbc.com
  129. f4m_params={'g': 'UXWGVKRWHFSP', 'hdcore': '3.0.3'},
  130. transform_rtmp_url=lambda streamer, src: (streamer, 'mp4:' + src))
  131. for _format in formats:
  132. ext = determine_ext(_format['url'])
  133. if ext == 'once':
  134. _format['ext'] = 'mp4'
  135. self._sort_formats(formats)
  136. return {
  137. 'id': video_id,
  138. 'title': info['title'],
  139. 'subtitles': subtitles,
  140. 'formats': formats,
  141. 'description': info['description'],
  142. 'thumbnail': info['defaultThumbnailUrl'],
  143. 'duration': int_or_none(info.get('duration'), 1000),
  144. }