brightcove.py 5.0 KB

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