2 from __future__ import unicode_literals
6 import xml.etree.ElementTree
8 from .common import InfoExtractor
15 compat_urllib_request,
23 class BrightcoveIE(InfoExtractor):
24 _VALID_URL = r'https?://.*brightcove\.com/(services|viewer).*\?(?P<query>.*)'
25 _FEDERATED_URL_TEMPLATE = 'http://c.brightcove.com/services/viewer/htmlFederated?%s'
29 # From http://www.8tv.cat/8aldia/videos/xavier-sala-i-martin-aquesta-tarda-a-8-al-dia/
30 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1654948606001&flashID=myExperience&%40videoPlayer=2371591881001',
31 'file': '2371591881001.mp4',
32 'md5': '5423e113865d26e40624dce2e4b45d95',
33 'note': 'Test Brightcove downloads and detection in GenericIE',
35 'title': 'Xavier Sala i Martín: “Un banc que no presta és un banc zombi que no serveix per a res”',
37 'description': 'md5:a950cc4285c43e44d763d036710cd9cd',
41 # From http://medianetwork.oracle.com/video/player/1785452137001
42 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1217746023001&flashID=myPlayer&%40videoPlayer=1785452137001',
43 'file': '1785452137001.flv',
45 'title': 'JVMLS 2012: Arrays 2.0 - Opportunities and Challenges',
46 'description': 'John Rose speaks at the JVM Language Summit, August 1, 2012.',
51 # From http://mashable.com/2013/10/26/thermoelectric-bracelet-lets-you-control-your-body-temperature/
52 'url': 'http://c.brightcove.com/services/viewer/federated_f9?&playerID=1265504713001&publisherID=AQ%7E%7E%2CAAABBzUwv1E%7E%2CxP-xFHVUstiMFlNYfvF4G9yFnNaqCw_9&videoID=2750934548001',
54 'id': '2750934548001',
56 'title': 'This Bracelet Acts as a Personal Thermostat',
57 'description': 'md5:547b78c64f4112766ccf4e151c20b6a0',
58 'uploader': 'Mashable',
62 # test that the default referer works
63 # from http://national.ballet.ca/interact/video/Lost_in_Motion_II/
64 'url': 'http://link.brightcove.com/services/player/bcpid756015033001?bckey=AQ~~,AAAApYJi_Ck~,GxhXCegT1Dp39ilhXuxMJxasUhVNZiil&bctid=2878862109001',
66 'id': '2878862109001',
68 'title': 'Lost in Motion II',
69 'description': 'md5:363109c02998fee92ec02211bd8000df',
70 'uploader': 'National Ballet of Canada',
74 # https://github.com/rg3/youtube-dl/issues/2253
75 'url': 'http://v.thestar.com/services/player/bcpid2071349530001?bckey=AQ~~,AAAAuO4KaJE~,gatFNwSKdGDmDpIYqNJ-fTHn_c4z_LH_&bctid=3101154703001',
76 'file': '3101154703001.mp4',
77 'md5': '0ba9446db037002366bab3b3eb30c88c',
79 'title': 'Still no power',
80 'uploader': 'thestar.com',
81 'description': 'Mississauga resident David Farmer is still out of power as a result of the ice storm a month ago. To keep the house warm, Farmer cuts wood from his property for a wood burning stove downstairs.',
87 def _build_brighcove_url(cls, object_str):
89 Build a Brightcove url from a xml string containing
90 <object class="BrightcoveExperience">{params}</object>
93 # Fix up some stupid HTML, see https://github.com/rg3/youtube-dl/issues/1553
94 object_str = re.sub(r'(<param name="[^"]+" value="[^"]+")>',
95 lambda m: m.group(1) + '/>', object_str)
96 # Fix up some stupid XML, see https://github.com/rg3/youtube-dl/issues/1608
97 object_str = object_str.replace('<--', '<!--')
98 object_str = fix_xml_ampersands(object_str)
100 object_doc = xml.etree.ElementTree.fromstring(object_str)
102 fv_el = find_xpath_attr(object_doc, './param', 'name', 'flashVars')
103 if fv_el is not None:
106 for k, v in compat_parse_qs(fv_el.attrib['value']).items())
110 def find_param(name):
111 if name in flashvars:
112 return flashvars[name]
113 node = find_xpath_attr(object_doc, './param', 'name', name)
115 return node.attrib['value']
120 playerID = find_param('playerID')
122 raise ExtractorError('Cannot find player ID')
123 params['playerID'] = playerID
125 playerKey = find_param('playerKey')
126 # Not all pages define this value
127 if playerKey is not None:
128 params['playerKey'] = playerKey
129 # The three fields hold the id of the video
130 videoPlayer = find_param('@videoPlayer') or find_param('videoId') or find_param('videoID')
131 if videoPlayer is not None:
132 params['@videoPlayer'] = videoPlayer
133 linkBase = find_param('linkBaseURL')
134 if linkBase is not None:
135 params['linkBaseURL'] = linkBase
136 data = compat_urllib_parse.urlencode(params)
137 return cls._FEDERATED_URL_TEMPLATE % data
140 def _extract_brightcove_url(cls, webpage):
141 """Try to extract the brightcove url from the wepbage, returns None
145 url_m = re.search(r'<meta\s+property="og:video"\s+content="(http://c.brightcove.com/[^"]+)"', webpage)
147 return url_m.group(1)
149 m_brightcove = re.search(
152 [^>]+?class=([\'"])[^>]*?BrightcoveExperience.*?\1 |
153 [^>]*?>\s*<param\s+name="movie"\s+value="https?://[^/]*brightcove\.com/
156 if m_brightcove is not None:
157 return cls._build_brighcove_url(m_brightcove.group())
161 def _real_extract(self, url):
162 url, smuggled_data = unsmuggle_url(url, {})
164 # Change the 'videoId' and others field to '@videoPlayer'
165 url = re.sub(r'(?<=[?&])(videoI(d|D)|bctid)', '%40videoPlayer', url)
166 # Change bckey (used by bcove.me urls) to playerKey
167 url = re.sub(r'(?<=[?&])bckey', 'playerKey', url)
168 mobj = re.match(self._VALID_URL, url)
169 query_str = mobj.group('query')
170 query = compat_urlparse.parse_qs(query_str)
172 videoPlayer = query.get('@videoPlayer')
174 # We set the original url as the default 'Referer' header
175 referer = smuggled_data.get('Referer', url)
176 return self._get_video_info(
177 videoPlayer[0], query_str, query, referer=referer)
179 player_key = query['playerKey']
180 return self._get_playlist_info(player_key[0])
182 def _get_video_info(self, video_id, query_str, query, referer=None):
183 request_url = self._FEDERATED_URL_TEMPLATE % query_str
184 req = compat_urllib_request.Request(request_url)
185 linkBase = query.get('linkBaseURL')
186 if linkBase is not None:
187 referer = linkBase[0]
188 if referer is not None:
189 req.add_header('Referer', referer)
190 webpage = self._download_webpage(req, video_id)
192 self.report_extraction(video_id)
193 info = self._search_regex(r'var experienceJSON = ({.*?});', webpage, 'json')
194 info = json.loads(info)['data']
195 video_info = info['programmedContent']['videoPlayer']['mediaDTO']
196 video_info['_youtubedl_adServerURL'] = info.get('adServerURL')
198 return self._extract_video_info(video_info)
200 def _get_playlist_info(self, player_key):
201 info_url = 'http://c.brightcove.com/services/json/experience/runtime/?command=get_programming_for_experience&playerKey=%s' % player_key
202 playlist_info = self._download_webpage(
203 info_url, player_key, 'Downloading playlist information')
205 json_data = json.loads(playlist_info)
206 if 'videoList' not in json_data:
207 raise ExtractorError('Empty playlist')
208 playlist_info = json_data['videoList']
209 videos = [self._extract_video_info(video_info) for video_info in playlist_info['mediaCollectionDTO']['videoDTOs']]
211 return self.playlist_result(videos, playlist_id=playlist_info['id'],
212 playlist_title=playlist_info['mediaCollectionDTO']['displayName'])
214 def _extract_video_info(self, video_info):
216 'id': compat_str(video_info['id']),
217 'title': video_info['displayName'].strip(),
218 'description': video_info.get('shortDescription'),
219 'thumbnail': video_info.get('videoStillURL') or video_info.get('thumbnailURL'),
220 'uploader': video_info.get('publisherName'),
223 renditions = video_info.get('renditions')
225 renditions = sorted(renditions, key=lambda r: r['size'])
227 'url': rend['defaultURL'],
228 'height': rend.get('frameHeight'),
229 'width': rend.get('frameWidth'),
230 } for rend in renditions]
231 elif video_info.get('FLVFullLengthURL') is not None:
233 'url': video_info['FLVFullLengthURL'],
236 if self._downloader.params.get('include_ads', False):
237 adServerURL = video_info.get('_youtubedl_adServerURL')
246 'title': info['title'],
247 'entries': [ad_info, info],
252 if 'url' not in info and not info.get('formats'):
253 raise ExtractorError('Unable to extract video url for %s' % info['id'])