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