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