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.

134 lines
5.7 KiB

  1. # encoding: utf-8
  2. import re
  3. import json
  4. import xml.etree.ElementTree
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. compat_urllib_parse,
  8. find_xpath_attr,
  9. compat_urlparse,
  10. ExtractorError,
  11. )
  12. class BrightcoveIE(InfoExtractor):
  13. _VALID_URL = r'https?://.*brightcove\.com/(services|viewer).*\?(?P<query>.*)'
  14. _FEDERATED_URL_TEMPLATE = 'http://c.brightcove.com/services/viewer/htmlFederated?%s'
  15. _PLAYLIST_URL_TEMPLATE = 'http://c.brightcove.com/services/json/experience/runtime/?command=get_programming_for_experience&playerKey=%s'
  16. _TESTS = [
  17. {
  18. # From http://www.8tv.cat/8aldia/videos/xavier-sala-i-martin-aquesta-tarda-a-8-al-dia/
  19. u'url': u'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1654948606001&flashID=myExperience&%40videoPlayer=2371591881001',
  20. u'file': u'2371591881001.mp4',
  21. u'md5': u'9e80619e0a94663f0bdc849b4566af19',
  22. u'note': u'Test Brightcove downloads and detection in GenericIE',
  23. u'info_dict': {
  24. u'title': u'Xavier Sala i Martín: “Un banc que no presta és un banc zombi que no serveix per a res”',
  25. u'uploader': u'8TV',
  26. u'description': u'md5:a950cc4285c43e44d763d036710cd9cd',
  27. }
  28. },
  29. {
  30. # From http://medianetwork.oracle.com/video/player/1785452137001
  31. u'url': u'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1217746023001&flashID=myPlayer&%40videoPlayer=1785452137001',
  32. u'file': u'1785452137001.flv',
  33. u'info_dict': {
  34. u'title': u'JVMLS 2012: Arrays 2.0 - Opportunities and Challenges',
  35. u'description': u'John Rose speaks at the JVM Language Summit, August 1, 2012.',
  36. u'uploader': u'Oracle',
  37. },
  38. },
  39. ]
  40. @classmethod
  41. def _build_brighcove_url(cls, object_str):
  42. """
  43. Build a Brightcove url from a xml string containing
  44. <object class="BrightcoveExperience">{params}</object>
  45. """
  46. # Fix up some stupid HTML, see https://github.com/rg3/youtube-dl/issues/1553
  47. object_str = re.sub(r'(<param name="[^"]+" value="[^"]+")>',
  48. lambda m: m.group(1) + '/>', object_str)
  49. # Fix up some stupid XML, see https://github.com/rg3/youtube-dl/issues/1608
  50. object_str = object_str.replace(u'<--', u'<!--')
  51. object_doc = xml.etree.ElementTree.fromstring(object_str)
  52. assert u'BrightcoveExperience' in object_doc.attrib['class']
  53. params = {'flashID': object_doc.attrib['id'],
  54. 'playerID': find_xpath_attr(object_doc, './param', 'name', 'playerID').attrib['value'],
  55. }
  56. playerKey = find_xpath_attr(object_doc, './param', 'name', 'playerKey')
  57. # Not all pages define this value
  58. if playerKey is not None:
  59. params['playerKey'] = playerKey.attrib['value']
  60. videoPlayer = find_xpath_attr(object_doc, './param', 'name', '@videoPlayer')
  61. if videoPlayer is not None:
  62. params['@videoPlayer'] = videoPlayer.attrib['value']
  63. data = compat_urllib_parse.urlencode(params)
  64. return cls._FEDERATED_URL_TEMPLATE % data
  65. def _real_extract(self, url):
  66. mobj = re.match(self._VALID_URL, url)
  67. query_str = mobj.group('query')
  68. query = compat_urlparse.parse_qs(query_str)
  69. videoPlayer = query.get('@videoPlayer')
  70. if videoPlayer:
  71. return self._get_video_info(videoPlayer[0], query_str)
  72. else:
  73. player_key = query['playerKey']
  74. return self._get_playlist_info(player_key[0])
  75. def _get_video_info(self, video_id, query):
  76. request_url = self._FEDERATED_URL_TEMPLATE % query
  77. webpage = self._download_webpage(request_url, video_id)
  78. self.report_extraction(video_id)
  79. info = self._search_regex(r'var experienceJSON = ({.*?});', webpage, 'json')
  80. info = json.loads(info)['data']
  81. video_info = info['programmedContent']['videoPlayer']['mediaDTO']
  82. return self._extract_video_info(video_info)
  83. def _get_playlist_info(self, player_key):
  84. playlist_info = self._download_webpage(self._PLAYLIST_URL_TEMPLATE % player_key,
  85. player_key, u'Downloading playlist information')
  86. json_data = json.loads(playlist_info)
  87. if 'videoList' not in json_data:
  88. raise ExtractorError(u'Empty playlist')
  89. playlist_info = json_data['videoList']
  90. videos = [self._extract_video_info(video_info) for video_info in playlist_info['mediaCollectionDTO']['videoDTOs']]
  91. return self.playlist_result(videos, playlist_id=playlist_info['id'],
  92. playlist_title=playlist_info['mediaCollectionDTO']['displayName'])
  93. def _extract_video_info(self, video_info):
  94. info = {
  95. 'id': video_info['id'],
  96. 'title': video_info['displayName'],
  97. 'description': video_info.get('shortDescription'),
  98. 'thumbnail': video_info.get('videoStillURL') or video_info.get('thumbnailURL'),
  99. 'uploader': video_info.get('publisherName'),
  100. }
  101. renditions = video_info.get('renditions')
  102. if renditions:
  103. renditions = sorted(renditions, key=lambda r: r['size'])
  104. best_format = renditions[-1]
  105. info.update({
  106. 'url': best_format['defaultURL'],
  107. 'ext': 'mp4',
  108. })
  109. elif video_info.get('FLVFullLengthURL') is not None:
  110. info.update({
  111. 'url': video_info['FLVFullLengthURL'],
  112. 'ext': 'flv',
  113. })
  114. else:
  115. raise ExtractorError(u'Unable to extract video url for %s' % info['id'])
  116. return info