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.

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