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.

111 lines
4.0 KiB

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