GenericIE: Detect videos from Brightcove
[youtube-dl] / youtube_dl / extractor / brightcove.py
1 import re
2 import json
3 import xml.etree.ElementTree
4
5 from .common import InfoExtractor
6 from ..utils import (
7     compat_urllib_parse,
8 )
9
10 class BrightcoveIE(InfoExtractor):
11     _VALID_URL = r'http://.*brightcove\.com/.*\?(?P<query>.*videoPlayer=(?P<id>\d*).*)'
12     _FEDERATED_URL_TEMPLATE = 'http://c.brightcove.com/services/viewer/htmlFederated?%s'
13     
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
17     @classmethod
18     def _build_brighcove_url(cls, object_str):
19         """
20         Build a Brightcove url from a xml string containing
21         <object class="BrightcoveExperience">{params}</object>
22         """
23         object_doc = xml.etree.ElementTree.fromstring(object_str)
24         assert object_doc.attrib['class'] == u'BrightcoveExperience'
25         params = {'flashID': object_doc.attrib['id'],
26                   'playerID': object_doc.find('./param[@name="playerID"]').attrib['value'],
27                   '@videoPlayer': object_doc.find('./param[@name="@videoPlayer"]').attrib['value'],
28                   }
29         playerKey = object_doc.find('./param[@name="playerKey"]')
30         # Not all pages define this value
31         if playerKey is not None:
32             params['playerKey'] = playerKey.attrib['value']
33         data = compat_urllib_parse.urlencode(params)
34         return cls._FEDERATED_URL_TEMPLATE % data
35
36     def _real_extract(self, url):
37         mobj = re.match(self._VALID_URL, url)
38         query = mobj.group('query')
39         video_id = mobj.group('id')
40
41         request_url = self._FEDERATED_URL_TEMPLATE % query
42         webpage = self._download_webpage(request_url, video_id)
43
44         self.report_extraction(video_id)
45         info = self._search_regex(r'var experienceJSON = ({.*?});', webpage, 'json')
46         info = json.loads(info)['data']
47         video_info = info['programmedContent']['videoPlayer']['mediaDTO']
48         renditions = video_info['renditions']
49         renditions = sorted(renditions, key=lambda r: r['size'])
50         best_format = renditions[-1]
51         
52         return {'id': video_id,
53                 'title': video_info['displayName'],
54                 'url': best_format['defaultURL'], 
55                 'ext': 'mp4',
56                 'description': video_info.get('shortDescription'),
57                 'thumbnail': video_info.get('videoStillURL') or video_info.get('thumbnailURL'),
58                 'uploader': video_info.get('publisherName'),
59                 }