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.

129 lines
5.5 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. object_doc = xml.etree.ElementTree.fromstring(object_str)
  50. assert u'BrightcoveExperience' in object_doc.attrib['class']
  51. params = {'flashID': object_doc.attrib['id'],
  52. 'playerID': find_xpath_attr(object_doc, './param', 'name', 'playerID').attrib['value'],
  53. }
  54. playerKey = find_xpath_attr(object_doc, './param', 'name', 'playerKey')
  55. # Not all pages define this value
  56. if playerKey is not None:
  57. params['playerKey'] = playerKey.attrib['value']
  58. videoPlayer = find_xpath_attr(object_doc, './param', 'name', '@videoPlayer')
  59. if videoPlayer is not None:
  60. params['@videoPlayer'] = videoPlayer.attrib['value']
  61. data = compat_urllib_parse.urlencode(params)
  62. return cls._FEDERATED_URL_TEMPLATE % data
  63. def _real_extract(self, url):
  64. mobj = re.match(self._VALID_URL, url)
  65. query_str = mobj.group('query')
  66. query = compat_urlparse.parse_qs(query_str)
  67. videoPlayer = query.get('@videoPlayer')
  68. if videoPlayer:
  69. return self._get_video_info(videoPlayer[0], query_str)
  70. else:
  71. player_key = query['playerKey']
  72. return self._get_playlist_info(player_key[0])
  73. def _get_video_info(self, video_id, query):
  74. request_url = self._FEDERATED_URL_TEMPLATE % query
  75. webpage = self._download_webpage(request_url, video_id)
  76. self.report_extraction(video_id)
  77. info = self._search_regex(r'var experienceJSON = ({.*?});', webpage, 'json')
  78. info = json.loads(info)['data']
  79. video_info = info['programmedContent']['videoPlayer']['mediaDTO']
  80. return self._extract_video_info(video_info)
  81. def _get_playlist_info(self, player_key):
  82. playlist_info = self._download_webpage(self._PLAYLIST_URL_TEMPLATE % player_key,
  83. player_key, u'Downloading playlist information')
  84. playlist_info = json.loads(playlist_info)['videoList']
  85. videos = [self._extract_video_info(video_info) for video_info in playlist_info['mediaCollectionDTO']['videoDTOs']]
  86. return self.playlist_result(videos, playlist_id=playlist_info['id'],
  87. playlist_title=playlist_info['mediaCollectionDTO']['displayName'])
  88. def _extract_video_info(self, video_info):
  89. info = {
  90. 'id': video_info['id'],
  91. 'title': video_info['displayName'],
  92. 'description': video_info.get('shortDescription'),
  93. 'thumbnail': video_info.get('videoStillURL') or video_info.get('thumbnailURL'),
  94. 'uploader': video_info.get('publisherName'),
  95. }
  96. renditions = video_info.get('renditions')
  97. if renditions:
  98. renditions = sorted(renditions, key=lambda r: r['size'])
  99. best_format = renditions[-1]
  100. info.update({
  101. 'url': best_format['defaultURL'],
  102. 'ext': 'mp4',
  103. })
  104. elif video_info.get('FLVFullLengthURL') is not None:
  105. info.update({
  106. 'url': video_info['FLVFullLengthURL'],
  107. 'ext': 'flv',
  108. })
  109. else:
  110. raise ExtractorError(u'Unable to extract video url for %s' % info['id'])
  111. return info