brightcove.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. )
  8. class BrightcoveIE(InfoExtractor):
  9. _VALID_URL = r'http://.*brightcove\.com/.*\?(?P<query>.*videoPlayer=(?P<id>\d*).*)'
  10. _FEDERATED_URL_TEMPLATE = 'http://c.brightcove.com/services/viewer/htmlFederated?%s'
  11. # There is a test for Brigtcove in GenericIE, that way we test both the download
  12. # and the detection of videos, and we don't have to find an URL that is always valid
  13. @classmethod
  14. def _build_brighcove_url(cls, object_str):
  15. """
  16. Build a Brightcove url from a xml string containing
  17. <object class="BrightcoveExperience">{params}</object>
  18. """
  19. object_doc = xml.etree.ElementTree.fromstring(object_str)
  20. assert object_doc.attrib['class'] == u'BrightcoveExperience'
  21. params = {'flashID': object_doc.attrib['id'],
  22. 'playerID': object_doc.find('./param[@name="playerID"]').attrib['value'],
  23. '@videoPlayer': object_doc.find('./param[@name="@videoPlayer"]').attrib['value'],
  24. }
  25. playerKey = object_doc.find('./param[@name="playerKey"]')
  26. # Not all pages define this value
  27. if playerKey is not None:
  28. params['playerKey'] = playerKey.attrib['value']
  29. data = compat_urllib_parse.urlencode(params)
  30. return cls._FEDERATED_URL_TEMPLATE % data
  31. def _real_extract(self, url):
  32. mobj = re.match(self._VALID_URL, url)
  33. query = mobj.group('query')
  34. video_id = mobj.group('id')
  35. request_url = self._FEDERATED_URL_TEMPLATE % query
  36. webpage = self._download_webpage(request_url, video_id)
  37. self.report_extraction(video_id)
  38. info = self._search_regex(r'var experienceJSON = ({.*?});', webpage, 'json')
  39. info = json.loads(info)['data']
  40. video_info = info['programmedContent']['videoPlayer']['mediaDTO']
  41. renditions = video_info['renditions']
  42. renditions = sorted(renditions, key=lambda r: r['size'])
  43. best_format = renditions[-1]
  44. return {'id': video_id,
  45. 'title': video_info['displayName'],
  46. 'url': best_format['defaultURL'],
  47. 'ext': 'mp4',
  48. 'description': video_info.get('shortDescription'),
  49. 'thumbnail': video_info.get('videoStillURL') or video_info.get('thumbnailURL'),
  50. 'uploader': video_info.get('publisherName'),
  51. }