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.

120 lines
4.5 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_str,
  7. ExtractorError,
  8. xpath_with_ns,
  9. )
  10. _x = lambda p: xpath_with_ns(p, {'smil': 'http://www.w3.org/2005/SMIL21/Language'})
  11. class ThePlatformIE(InfoExtractor):
  12. _VALID_URL = r'''(?x)
  13. (?:https?://(?:link|player)\.theplatform\.com/[sp]/[^/]+/
  14. (?P<config>(?:[^/\?]+/(?:swf|config)|onsite)/select/)?
  15. |theplatform:)(?P<id>[^/\?&]+)'''
  16. _TEST = {
  17. # from http://www.metacafe.com/watch/cb-e9I_cZgTgIPd/blackberrys_big_bold_z30/
  18. 'url': 'http://link.theplatform.com/s/dJ5BDC/e9I_cZgTgIPd/meta.smil?format=smil&Tracking=true&mbr=true',
  19. 'info_dict': {
  20. 'id': 'e9I_cZgTgIPd',
  21. 'ext': 'flv',
  22. 'title': 'Blackberry\'s big, bold Z30',
  23. 'description': 'The Z30 is Blackberry\'s biggest, baddest mobile messaging device yet.',
  24. 'duration': 247,
  25. },
  26. 'params': {
  27. # rtmp download
  28. 'skip_download': True,
  29. },
  30. }
  31. def _get_info(self, video_id, smil_url):
  32. meta = self._download_xml(smil_url, video_id)
  33. try:
  34. error_msg = next(
  35. n.attrib['abstract']
  36. for n in meta.findall(_x('.//smil:ref'))
  37. if n.attrib.get('title') == 'Geographic Restriction')
  38. except StopIteration:
  39. pass
  40. else:
  41. raise ExtractorError(error_msg, expected=True)
  42. info_url = 'http://link.theplatform.com/s/dJ5BDC/{0}?format=preview'.format(video_id)
  43. info_json = self._download_webpage(info_url, video_id)
  44. info = json.loads(info_json)
  45. head = meta.find(_x('smil:head'))
  46. body = meta.find(_x('smil:body'))
  47. f4m_node = body.find(_x('smil:seq//smil:video'))
  48. if f4m_node is not None and '.f4m' in f4m_node.attrib['src']:
  49. f4m_url = f4m_node.attrib['src']
  50. if 'manifest.f4m?' not in f4m_url:
  51. f4m_url += '?'
  52. # the parameters are from syfy.com, other sites may use others,
  53. # they also work for nbc.com
  54. f4m_url += '&g=UXWGVKRWHFSP&hdcore=3.0.3'
  55. formats = self._extract_f4m_formats(f4m_url, video_id)
  56. else:
  57. formats = []
  58. switch = body.find(_x('smil:switch'))
  59. if switch is not None:
  60. base_url = head.find(_x('smil:meta')).attrib['base']
  61. for f in switch.findall(_x('smil:video')):
  62. attr = f.attrib
  63. width = int(attr['width'])
  64. height = int(attr['height'])
  65. vbr = int(attr['system-bitrate']) // 1000
  66. format_id = '%dx%d_%dk' % (width, height, vbr)
  67. formats.append({
  68. 'format_id': format_id,
  69. 'url': base_url,
  70. 'play_path': 'mp4:' + attr['src'],
  71. 'ext': 'flv',
  72. 'width': width,
  73. 'height': height,
  74. 'vbr': vbr,
  75. })
  76. else:
  77. switch = body.find(_x('smil:seq//smil:switch'))
  78. for f in switch.findall(_x('smil:video')):
  79. attr = f.attrib
  80. vbr = int(attr['system-bitrate']) // 1000
  81. formats.append({
  82. 'format_id': compat_str(vbr),
  83. 'url': attr['src'],
  84. 'vbr': vbr,
  85. })
  86. self._sort_formats(formats)
  87. return {
  88. 'id': video_id,
  89. 'title': info['title'],
  90. 'formats': formats,
  91. 'description': info['description'],
  92. 'thumbnail': info['defaultThumbnailUrl'],
  93. 'duration': info['duration']//1000,
  94. }
  95. def _real_extract(self, url):
  96. mobj = re.match(self._VALID_URL, url)
  97. video_id = mobj.group('id')
  98. if mobj.group('config'):
  99. config_url = url+ '&form=json'
  100. config_url = config_url.replace('swf/', 'config/')
  101. config_url = config_url.replace('onsite/', 'onsite/config/')
  102. config = self._download_json(config_url, video_id, 'Downloading config')
  103. smil_url = config['releaseUrl'] + '&format=SMIL&formats=MPEG4&manifest=f4m'
  104. else:
  105. smil_url = ('http://link.theplatform.com/s/dJ5BDC/{0}/meta.smil?'
  106. 'format=smil&mbr=true'.format(video_id))
  107. return self._get_info(video_id, smil_url)