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.

86 lines
3.8 KiB

  1. import re
  2. import json
  3. import xml.etree.ElementTree
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_urllib_parse,
  7. find_xpath_attr,
  8. compat_urlparse,
  9. )
  10. class BrightcoveIE(InfoExtractor):
  11. _VALID_URL = r'https?://.*brightcove\.com/(services|viewer).*\?(?P<query>.*)'
  12. _FEDERATED_URL_TEMPLATE = 'http://c.brightcove.com/services/viewer/htmlFederated?%s'
  13. _PLAYLIST_URL_TEMPLATE = 'http://c.brightcove.com/services/json/experience/runtime/?command=get_programming_for_experience&playerKey=%s'
  14. # There is a test for Brigtcove in GenericIE, that way we test both the download
  15. # and the detection of videos, and we don't have to find an URL that is always valid
  16. @classmethod
  17. def _build_brighcove_url(cls, object_str):
  18. """
  19. Build a Brightcove url from a xml string containing
  20. <object class="BrightcoveExperience">{params}</object>
  21. """
  22. object_doc = xml.etree.ElementTree.fromstring(object_str)
  23. assert u'BrightcoveExperience' in object_doc.attrib['class']
  24. params = {'flashID': object_doc.attrib['id'],
  25. 'playerID': find_xpath_attr(object_doc, './param', 'name', 'playerID').attrib['value'],
  26. }
  27. playerKey = find_xpath_attr(object_doc, './param', 'name', 'playerKey')
  28. # Not all pages define this value
  29. if playerKey is not None:
  30. params['playerKey'] = playerKey.attrib['value']
  31. videoPlayer = find_xpath_attr(object_doc, './param', 'name', '@videoPlayer')
  32. if videoPlayer is not None:
  33. params['@videoPlayer'] = videoPlayer.attrib['value']
  34. data = compat_urllib_parse.urlencode(params)
  35. return cls._FEDERATED_URL_TEMPLATE % data
  36. def _real_extract(self, url):
  37. mobj = re.match(self._VALID_URL, url)
  38. query_str = mobj.group('query')
  39. query = compat_urlparse.parse_qs(query_str)
  40. videoPlayer = query.get('@videoPlayer')
  41. if videoPlayer:
  42. return self._get_video_info(videoPlayer[0], query_str)
  43. else:
  44. player_key = query['playerKey']
  45. return self._get_playlist_info(player_key[0])
  46. def _get_video_info(self, video_id, query):
  47. request_url = self._FEDERATED_URL_TEMPLATE % query
  48. webpage = self._download_webpage(request_url, video_id)
  49. self.report_extraction(video_id)
  50. info = self._search_regex(r'var experienceJSON = ({.*?});', webpage, 'json')
  51. info = json.loads(info)['data']
  52. video_info = info['programmedContent']['videoPlayer']['mediaDTO']
  53. return self._extract_video_info(video_info)
  54. def _get_playlist_info(self, player_key):
  55. playlist_info = self._download_webpage(self._PLAYLIST_URL_TEMPLATE % player_key,
  56. player_key, u'Downloading playlist information')
  57. playlist_info = json.loads(playlist_info)['videoList']
  58. videos = [self._extract_video_info(video_info) for video_info in playlist_info['mediaCollectionDTO']['videoDTOs']]
  59. return self.playlist_result(videos, playlist_id=playlist_info['id'],
  60. playlist_title=playlist_info['mediaCollectionDTO']['displayName'])
  61. def _extract_video_info(self, video_info):
  62. renditions = video_info['renditions']
  63. renditions = sorted(renditions, key=lambda r: r['size'])
  64. best_format = renditions[-1]
  65. return {'id': video_info['id'],
  66. 'title': video_info['displayName'],
  67. 'url': best_format['defaultURL'],
  68. 'ext': 'mp4',
  69. 'description': video_info.get('shortDescription'),
  70. 'thumbnail': video_info.get('videoStillURL') or video_info.get('thumbnailURL'),
  71. 'uploader': video_info.get('publisherName'),
  72. }