]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/brightcove.py
Update the changelog.
[youtubedl] / youtube_dl / extractor / brightcove.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import base64
5 import re
6 import struct
7
8 from .common import InfoExtractor
9 from .adobepass import AdobePassIE
10 from ..compat import (
11 compat_etree_fromstring,
12 compat_parse_qs,
13 compat_urllib_parse_urlparse,
14 compat_urlparse,
15 compat_xml_parse_error,
16 compat_HTTPError,
17 )
18 from ..utils import (
19 ExtractorError,
20 extract_attributes,
21 find_xpath_attr,
22 fix_xml_ampersands,
23 float_or_none,
24 js_to_json,
25 int_or_none,
26 parse_iso8601,
27 smuggle_url,
28 unescapeHTML,
29 unsmuggle_url,
30 update_url_query,
31 clean_html,
32 mimetype2ext,
33 UnsupportedError,
34 )
35
36
37 class BrightcoveLegacyIE(InfoExtractor):
38 IE_NAME = 'brightcove:legacy'
39 _VALID_URL = r'(?:https?://.*brightcove\.com/(services|viewer).*?\?|brightcove:)(?P<query>.*)'
40
41 _TESTS = [
42 {
43 # From http://www.8tv.cat/8aldia/videos/xavier-sala-i-martin-aquesta-tarda-a-8-al-dia/
44 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1654948606001&flashID=myExperience&%40videoPlayer=2371591881001',
45 'md5': '5423e113865d26e40624dce2e4b45d95',
46 'note': 'Test Brightcove downloads and detection in GenericIE',
47 'info_dict': {
48 'id': '2371591881001',
49 'ext': 'mp4',
50 'title': 'Xavier Sala i Martín: “Un banc que no presta és un banc zombi que no serveix per a res”',
51 'uploader': '8TV',
52 'description': 'md5:a950cc4285c43e44d763d036710cd9cd',
53 'timestamp': 1368213670,
54 'upload_date': '20130510',
55 'uploader_id': '1589608506001',
56 },
57 'skip': 'The player has been deactivated by the content owner',
58 },
59 {
60 # From http://medianetwork.oracle.com/video/player/1785452137001
61 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1217746023001&flashID=myPlayer&%40videoPlayer=1785452137001',
62 'info_dict': {
63 'id': '1785452137001',
64 'ext': 'flv',
65 'title': 'JVMLS 2012: Arrays 2.0 - Opportunities and Challenges',
66 'description': 'John Rose speaks at the JVM Language Summit, August 1, 2012.',
67 'uploader': 'Oracle',
68 'timestamp': 1344975024,
69 'upload_date': '20120814',
70 'uploader_id': '1460825906',
71 },
72 'skip': 'video not playable',
73 },
74 {
75 # From http://mashable.com/2013/10/26/thermoelectric-bracelet-lets-you-control-your-body-temperature/
76 'url': 'http://c.brightcove.com/services/viewer/federated_f9?&playerID=1265504713001&publisherID=AQ%7E%7E%2CAAABBzUwv1E%7E%2CxP-xFHVUstiMFlNYfvF4G9yFnNaqCw_9&videoID=2750934548001',
77 'info_dict': {
78 'id': '2750934548001',
79 'ext': 'mp4',
80 'title': 'This Bracelet Acts as a Personal Thermostat',
81 'description': 'md5:547b78c64f4112766ccf4e151c20b6a0',
82 # 'uploader': 'Mashable',
83 'timestamp': 1382041798,
84 'upload_date': '20131017',
85 'uploader_id': '1130468786001',
86 },
87 },
88 {
89 # test that the default referer works
90 # from http://national.ballet.ca/interact/video/Lost_in_Motion_II/
91 'url': 'http://link.brightcove.com/services/player/bcpid756015033001?bckey=AQ~~,AAAApYJi_Ck~,GxhXCegT1Dp39ilhXuxMJxasUhVNZiil&bctid=2878862109001',
92 'info_dict': {
93 'id': '2878862109001',
94 'ext': 'mp4',
95 'title': 'Lost in Motion II',
96 'description': 'md5:363109c02998fee92ec02211bd8000df',
97 'uploader': 'National Ballet of Canada',
98 },
99 'skip': 'Video gone',
100 },
101 {
102 # test flv videos served by akamaihd.net
103 # From http://www.redbull.com/en/bike/stories/1331655643987/replay-uci-dh-world-cup-2014-from-fort-william
104 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?%40videoPlayer=ref%3Aevent-stream-356&linkBaseURL=http%3A%2F%2Fwww.redbull.com%2Fen%2Fbike%2Fvideos%2F1331655630249%2Freplay-uci-fort-william-2014-dh&playerKey=AQ%7E%7E%2CAAAApYJ7UqE%7E%2Cxqr_zXk0I-zzNndy8NlHogrCb5QdyZRf&playerID=1398061561001#__youtubedl_smuggle=%7B%22Referer%22%3A+%22http%3A%2F%2Fwww.redbull.com%2Fen%2Fbike%2Fstories%2F1331655643987%2Freplay-uci-dh-world-cup-2014-from-fort-william%22%7D',
105 # The md5 checksum changes on each download
106 'info_dict': {
107 'id': '3750436379001',
108 'ext': 'flv',
109 'title': 'UCI MTB World Cup 2014: Fort William, UK - Downhill Finals',
110 'uploader': 'RBTV Old (do not use)',
111 'description': 'UCI MTB World Cup 2014: Fort William, UK - Downhill Finals',
112 'timestamp': 1409122195,
113 'upload_date': '20140827',
114 'uploader_id': '710858724001',
115 },
116 'skip': 'Video gone',
117 },
118 {
119 # playlist with 'videoList'
120 # from http://support.brightcove.com/en/video-cloud/docs/playlist-support-single-video-players
121 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=3550052898001&playerKey=AQ%7E%7E%2CAAABmA9XpXk%7E%2C-Kp7jNgisre1fG5OdqpAFUTcs0lP_ZoL',
122 'info_dict': {
123 'title': 'Sealife',
124 'id': '3550319591001',
125 },
126 'playlist_mincount': 7,
127 'skip': 'Unsupported URL',
128 },
129 {
130 # playlist with 'playlistTab' (https://github.com/ytdl-org/youtube-dl/issues/9965)
131 'url': 'http://c.brightcove.com/services/json/experience/runtime/?command=get_programming_for_experience&playerKey=AQ%7E%7E,AAABXlLMdok%7E,NJ4EoMlZ4rZdx9eU1rkMVd8EaYPBBUlg',
132 'info_dict': {
133 'id': '1522758701001',
134 'title': 'Lesson 08',
135 },
136 'playlist_mincount': 10,
137 'skip': 'Unsupported URL',
138 },
139 {
140 # playerID inferred from bcpid
141 # from http://www.un.org/chinese/News/story.asp?NewsID=27724
142 'url': 'https://link.brightcove.com/services/player/bcpid1722935254001/?bctid=5360463607001&autoStart=false&secureConnections=true&width=650&height=350',
143 'only_matching': True, # Tested in GenericIE
144 }
145 ]
146
147 @classmethod
148 def _build_brighcove_url(cls, object_str):
149 """
150 Build a Brightcove url from a xml string containing
151 <object class="BrightcoveExperience">{params}</object>
152 """
153
154 # Fix up some stupid HTML, see https://github.com/ytdl-org/youtube-dl/issues/1553
155 object_str = re.sub(r'(<param(?:\s+[a-zA-Z0-9_]+="[^"]*")*)>',
156 lambda m: m.group(1) + '/>', object_str)
157 # Fix up some stupid XML, see https://github.com/ytdl-org/youtube-dl/issues/1608
158 object_str = object_str.replace('<--', '<!--')
159 # remove namespace to simplify extraction
160 object_str = re.sub(r'(<object[^>]*)(xmlns=".*?")', r'\1', object_str)
161 object_str = fix_xml_ampersands(object_str)
162
163 try:
164 object_doc = compat_etree_fromstring(object_str.encode('utf-8'))
165 except compat_xml_parse_error:
166 return
167
168 fv_el = find_xpath_attr(object_doc, './param', 'name', 'flashVars')
169 if fv_el is not None:
170 flashvars = dict(
171 (k, v[0])
172 for k, v in compat_parse_qs(fv_el.attrib['value']).items())
173 else:
174 flashvars = {}
175
176 data_url = object_doc.attrib.get('data', '')
177 data_url_params = compat_parse_qs(compat_urllib_parse_urlparse(data_url).query)
178
179 def find_param(name):
180 if name in flashvars:
181 return flashvars[name]
182 node = find_xpath_attr(object_doc, './param', 'name', name)
183 if node is not None:
184 return node.attrib['value']
185 return data_url_params.get(name)
186
187 params = {}
188
189 playerID = find_param('playerID') or find_param('playerId')
190 if playerID is None:
191 raise ExtractorError('Cannot find player ID')
192 params['playerID'] = playerID
193
194 playerKey = find_param('playerKey')
195 # Not all pages define this value
196 if playerKey is not None:
197 params['playerKey'] = playerKey
198 # These fields hold the id of the video
199 videoPlayer = find_param('@videoPlayer') or find_param('videoId') or find_param('videoID') or find_param('@videoList')
200 if videoPlayer is not None:
201 if isinstance(videoPlayer, list):
202 videoPlayer = videoPlayer[0]
203 videoPlayer = videoPlayer.strip()
204 # UUID is also possible for videoPlayer (e.g.
205 # http://www.popcornflix.com/hoodies-vs-hooligans/7f2d2b87-bbf2-4623-acfb-ea942b4f01dd
206 # or http://www8.hp.com/cn/zh/home.html)
207 if not (re.match(
208 r'^(?:\d+|[\da-fA-F]{8}-?[\da-fA-F]{4}-?[\da-fA-F]{4}-?[\da-fA-F]{4}-?[\da-fA-F]{12})$',
209 videoPlayer) or videoPlayer.startswith('ref:')):
210 return None
211 params['@videoPlayer'] = videoPlayer
212 linkBase = find_param('linkBaseURL')
213 if linkBase is not None:
214 params['linkBaseURL'] = linkBase
215 return cls._make_brightcove_url(params)
216
217 @classmethod
218 def _build_brighcove_url_from_js(cls, object_js):
219 # The layout of JS is as follows:
220 # customBC.createVideo = function (width, height, playerID, playerKey, videoPlayer, VideoRandomID) {
221 # // build Brightcove <object /> XML
222 # }
223 m = re.search(
224 r'''(?x)customBC\.createVideo\(
225 .*? # skipping width and height
226 ["\'](?P<playerID>\d+)["\']\s*,\s* # playerID
227 ["\'](?P<playerKey>AQ[^"\']{48})[^"\']*["\']\s*,\s* # playerKey begins with AQ and is 50 characters
228 # in length, however it's appended to itself
229 # in places, so truncate
230 ["\'](?P<videoID>\d+)["\'] # @videoPlayer
231 ''', object_js)
232 if m:
233 return cls._make_brightcove_url(m.groupdict())
234
235 @classmethod
236 def _make_brightcove_url(cls, params):
237 return update_url_query(
238 'http://c.brightcove.com/services/viewer/htmlFederated', params)
239
240 @classmethod
241 def _extract_brightcove_url(cls, webpage):
242 """Try to extract the brightcove url from the webpage, returns None
243 if it can't be found
244 """
245 urls = cls._extract_brightcove_urls(webpage)
246 return urls[0] if urls else None
247
248 @classmethod
249 def _extract_brightcove_urls(cls, webpage):
250 """Return a list of all Brightcove URLs from the webpage """
251
252 url_m = re.search(
253 r'''(?x)
254 <meta\s+
255 (?:property|itemprop)=([\'"])(?:og:video|embedURL)\1[^>]+
256 content=([\'"])(?P<url>https?://(?:secure|c)\.brightcove.com/(?:(?!\2).)+)\2
257 ''', webpage)
258 if url_m:
259 url = unescapeHTML(url_m.group('url'))
260 # Some sites don't add it, we can't download with this url, for example:
261 # http://www.ktvu.com/videos/news/raw-video-caltrain-releases-video-of-man-almost/vCTZdY/
262 if 'playerKey' in url or 'videoId' in url or 'idVideo' in url:
263 return [url]
264
265 matches = re.findall(
266 r'''(?sx)<object
267 (?:
268 [^>]+?class=[\'"][^>]*?BrightcoveExperience.*?[\'"] |
269 [^>]*?>\s*<param\s+name="movie"\s+value="https?://[^/]*brightcove\.com/
270 ).+?>\s*</object>''',
271 webpage)
272 if matches:
273 return list(filter(None, [cls._build_brighcove_url(m) for m in matches]))
274
275 matches = re.findall(r'(customBC\.createVideo\(.+?\);)', webpage)
276 if matches:
277 return list(filter(None, [
278 cls._build_brighcove_url_from_js(custom_bc)
279 for custom_bc in matches]))
280 return [src for _, src in re.findall(
281 r'<iframe[^>]+src=([\'"])((?:https?:)?//link\.brightcove\.com/services/player/(?!\1).+)\1', webpage)]
282
283 def _real_extract(self, url):
284 url, smuggled_data = unsmuggle_url(url, {})
285
286 # Change the 'videoId' and others field to '@videoPlayer'
287 url = re.sub(r'(?<=[?&])(videoI(d|D)|idVideo|bctid)', '%40videoPlayer', url)
288 # Change bckey (used by bcove.me urls) to playerKey
289 url = re.sub(r'(?<=[?&])bckey', 'playerKey', url)
290 mobj = re.match(self._VALID_URL, url)
291 query_str = mobj.group('query')
292 query = compat_urlparse.parse_qs(query_str)
293
294 videoPlayer = query.get('@videoPlayer')
295 if videoPlayer:
296 # We set the original url as the default 'Referer' header
297 referer = query.get('linkBaseURL', [None])[0] or smuggled_data.get('Referer', url)
298 video_id = videoPlayer[0]
299 if 'playerID' not in query:
300 mobj = re.search(r'/bcpid(\d+)', url)
301 if mobj is not None:
302 query['playerID'] = [mobj.group(1)]
303 publisher_id = query.get('publisherId')
304 if publisher_id and publisher_id[0].isdigit():
305 publisher_id = publisher_id[0]
306 if not publisher_id:
307 player_key = query.get('playerKey')
308 if player_key and ',' in player_key[0]:
309 player_key = player_key[0]
310 else:
311 player_id = query.get('playerID')
312 if player_id and player_id[0].isdigit():
313 headers = {}
314 if referer:
315 headers['Referer'] = referer
316 player_page = self._download_webpage(
317 'http://link.brightcove.com/services/player/bcpid' + player_id[0],
318 video_id, headers=headers, fatal=False)
319 if player_page:
320 player_key = self._search_regex(
321 r'<param\s+name="playerKey"\s+value="([\w~,-]+)"',
322 player_page, 'player key', fatal=False)
323 if player_key:
324 enc_pub_id = player_key.split(',')[1].replace('~', '=')
325 publisher_id = struct.unpack('>Q', base64.urlsafe_b64decode(enc_pub_id))[0]
326 if publisher_id:
327 brightcove_new_url = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s' % (publisher_id, video_id)
328 if referer:
329 brightcove_new_url = smuggle_url(brightcove_new_url, {'referrer': referer})
330 return self.url_result(brightcove_new_url, BrightcoveNewIE.ie_key(), video_id)
331 # TODO: figure out if it's possible to extract playlistId from playerKey
332 # elif 'playerKey' in query:
333 # player_key = query['playerKey']
334 # return self._get_playlist_info(player_key[0])
335 raise UnsupportedError(url)
336
337
338 class BrightcoveNewIE(AdobePassIE):
339 IE_NAME = 'brightcove:new'
340 _VALID_URL = r'https?://players\.brightcove\.net/(?P<account_id>\d+)/(?P<player_id>[^/]+)_(?P<embed>[^/]+)/index\.html\?.*(?P<content_type>video|playlist)Id=(?P<video_id>\d+|ref:[^&]+)'
341 _TESTS = [{
342 'url': 'http://players.brightcove.net/929656772001/e41d32dc-ec74-459e-a845-6c69f7b724ea_default/index.html?videoId=4463358922001',
343 'md5': 'c8100925723840d4b0d243f7025703be',
344 'info_dict': {
345 'id': '4463358922001',
346 'ext': 'mp4',
347 'title': 'Meet the man behind Popcorn Time',
348 'description': 'md5:eac376a4fe366edc70279bfb681aea16',
349 'duration': 165.768,
350 'timestamp': 1441391203,
351 'upload_date': '20150904',
352 'uploader_id': '929656772001',
353 'formats': 'mincount:20',
354 },
355 }, {
356 # with rtmp streams
357 'url': 'http://players.brightcove.net/4036320279001/5d112ed9-283f-485f-a7f9-33f42e8bc042_default/index.html?videoId=4279049078001',
358 'info_dict': {
359 'id': '4279049078001',
360 'ext': 'mp4',
361 'title': 'Titansgrave: Chapter 0',
362 'description': 'Titansgrave: Chapter 0',
363 'duration': 1242.058,
364 'timestamp': 1433556729,
365 'upload_date': '20150606',
366 'uploader_id': '4036320279001',
367 'formats': 'mincount:39',
368 },
369 'params': {
370 # m3u8 download
371 'skip_download': True,
372 }
373 }, {
374 # playlist stream
375 'url': 'https://players.brightcove.net/1752604059001/S13cJdUBz_default/index.html?playlistId=5718313430001',
376 'info_dict': {
377 'id': '5718313430001',
378 'title': 'No Audio Playlist',
379 },
380 'playlist_count': 7,
381 'params': {
382 # m3u8 download
383 'skip_download': True,
384 }
385 }, {
386 'url': 'http://players.brightcove.net/5690807595001/HyZNerRl7_default/index.html?playlistId=5743160747001',
387 'only_matching': True,
388 }, {
389 # ref: prefixed video id
390 'url': 'http://players.brightcove.net/3910869709001/21519b5c-4b3b-4363-accb-bdc8f358f823_default/index.html?videoId=ref:7069442',
391 'only_matching': True,
392 }, {
393 # non numeric ref: prefixed video id
394 'url': 'http://players.brightcove.net/710858724001/default_default/index.html?videoId=ref:event-stream-356',
395 'only_matching': True,
396 }, {
397 # unavailable video without message but with error_code
398 'url': 'http://players.brightcove.net/1305187701/c832abfb-641b-44eb-9da0-2fe76786505f_default/index.html?videoId=4377407326001',
399 'only_matching': True,
400 }]
401
402 @staticmethod
403 def _extract_url(ie, webpage):
404 urls = BrightcoveNewIE._extract_urls(ie, webpage)
405 return urls[0] if urls else None
406
407 @staticmethod
408 def _extract_urls(ie, webpage):
409 # Reference:
410 # 1. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#setvideoiniframe
411 # 2. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#tag
412 # 3. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#setvideousingjavascript
413 # 4. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/in-page-embed-player-implementation.html
414 # 5. https://support.brightcove.com/en/video-cloud/docs/dynamically-assigning-videos-player
415
416 entries = []
417
418 # Look for iframe embeds [1]
419 for _, url in re.findall(
420 r'<iframe[^>]+src=(["\'])((?:https?:)?//players\.brightcove\.net/\d+/[^/]+/index\.html.+?)\1', webpage):
421 entries.append(url if url.startswith('http') else 'http:' + url)
422
423 # Look for <video> tags [2] and embed_in_page embeds [3]
424 # [2] looks like:
425 for video, script_tag, account_id, player_id, embed in re.findall(
426 r'''(?isx)
427 (<video\s+[^>]*\bdata-video-id\s*=\s*['"]?[^>]+>)
428 (?:.*?
429 (<script[^>]+
430 src=["\'](?:https?:)?//players\.brightcove\.net/
431 (\d+)/([^/]+)_([^/]+)/index(?:\.min)?\.js
432 )
433 )?
434 ''', webpage):
435 attrs = extract_attributes(video)
436
437 # According to examples from [4] it's unclear whether video id
438 # may be optional and what to do when it is
439 video_id = attrs.get('data-video-id')
440 if not video_id:
441 continue
442
443 account_id = account_id or attrs.get('data-account')
444 if not account_id:
445 continue
446
447 player_id = player_id or attrs.get('data-player') or 'default'
448 embed = embed or attrs.get('data-embed') or 'default'
449
450 bc_url = 'http://players.brightcove.net/%s/%s_%s/index.html?videoId=%s' % (
451 account_id, player_id, embed, video_id)
452
453 # Some brightcove videos may be embedded with video tag only and
454 # without script tag or any mentioning of brightcove at all. Such
455 # embeds are considered ambiguous since they are matched based only
456 # on data-video-id and data-account attributes and in the wild may
457 # not be brightcove embeds at all. Let's check reconstructed
458 # brightcove URLs in case of such embeds and only process valid
459 # ones. By this we ensure there is indeed a brightcove embed.
460 if not script_tag and not ie._is_valid_url(
461 bc_url, video_id, 'possible brightcove video'):
462 continue
463
464 entries.append(bc_url)
465
466 return entries
467
468 def _parse_brightcove_metadata(self, json_data, video_id, headers={}):
469 title = json_data['name'].strip()
470
471 formats = []
472 for source in json_data.get('sources', []):
473 container = source.get('container')
474 ext = mimetype2ext(source.get('type'))
475 src = source.get('src')
476 # https://support.brightcove.com/playback-api-video-fields-reference#key_systems_object
477 if ext == 'ism' or container == 'WVM' or source.get('key_systems'):
478 continue
479 elif ext == 'm3u8' or container == 'M2TS':
480 if not src:
481 continue
482 formats.extend(self._extract_m3u8_formats(
483 src, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
484 elif ext == 'mpd':
485 if not src:
486 continue
487 formats.extend(self._extract_mpd_formats(src, video_id, 'dash', fatal=False))
488 else:
489 streaming_src = source.get('streaming_src')
490 stream_name, app_name = source.get('stream_name'), source.get('app_name')
491 if not src and not streaming_src and (not stream_name or not app_name):
492 continue
493 tbr = float_or_none(source.get('avg_bitrate'), 1000)
494 height = int_or_none(source.get('height'))
495 width = int_or_none(source.get('width'))
496 f = {
497 'tbr': tbr,
498 'filesize': int_or_none(source.get('size')),
499 'container': container,
500 'ext': ext or container.lower(),
501 }
502 if width == 0 and height == 0:
503 f.update({
504 'vcodec': 'none',
505 })
506 else:
507 f.update({
508 'width': width,
509 'height': height,
510 'vcodec': source.get('codec'),
511 })
512
513 def build_format_id(kind):
514 format_id = kind
515 if tbr:
516 format_id += '-%dk' % int(tbr)
517 if height:
518 format_id += '-%dp' % height
519 return format_id
520
521 if src or streaming_src:
522 f.update({
523 'url': src or streaming_src,
524 'format_id': build_format_id('http' if src else 'http-streaming'),
525 'source_preference': 0 if src else -1,
526 })
527 else:
528 f.update({
529 'url': app_name,
530 'play_path': stream_name,
531 'format_id': build_format_id('rtmp'),
532 })
533 formats.append(f)
534 if not formats:
535 # for sonyliv.com DRM protected videos
536 s3_source_url = json_data.get('custom_fields', {}).get('s3sourceurl')
537 if s3_source_url:
538 formats.append({
539 'url': s3_source_url,
540 'format_id': 'source',
541 })
542
543 errors = json_data.get('errors')
544 if not formats and errors:
545 error = errors[0]
546 raise ExtractorError(
547 error.get('message') or error.get('error_subcode') or error['error_code'], expected=True)
548
549 self._sort_formats(formats)
550
551 for f in formats:
552 f.setdefault('http_headers', {}).update(headers)
553
554 subtitles = {}
555 for text_track in json_data.get('text_tracks', []):
556 if text_track.get('src'):
557 subtitles.setdefault(text_track.get('srclang'), []).append({
558 'url': text_track['src'],
559 })
560
561 is_live = False
562 duration = float_or_none(json_data.get('duration'), 1000)
563 if duration is not None and duration <= 0:
564 is_live = True
565
566 return {
567 'id': video_id,
568 'title': self._live_title(title) if is_live else title,
569 'description': clean_html(json_data.get('description')),
570 'thumbnail': json_data.get('thumbnail') or json_data.get('poster'),
571 'duration': duration,
572 'timestamp': parse_iso8601(json_data.get('published_at')),
573 'uploader_id': json_data.get('account_id'),
574 'formats': formats,
575 'subtitles': subtitles,
576 'tags': json_data.get('tags', []),
577 'is_live': is_live,
578 }
579
580 def _real_extract(self, url):
581 url, smuggled_data = unsmuggle_url(url, {})
582 self._initialize_geo_bypass({
583 'countries': smuggled_data.get('geo_countries'),
584 'ip_blocks': smuggled_data.get('geo_ip_blocks'),
585 })
586
587 account_id, player_id, embed, content_type, video_id = re.match(self._VALID_URL, url).groups()
588
589 policy_key_id = '%s_%s' % (account_id, player_id)
590 policy_key = self._downloader.cache.load('brightcove', policy_key_id)
591 policy_key_extracted = False
592 store_pk = lambda x: self._downloader.cache.store('brightcove', policy_key_id, x)
593
594 def extract_policy_key():
595 webpage = self._download_webpage(
596 'http://players.brightcove.net/%s/%s_%s/index.min.js'
597 % (account_id, player_id, embed), video_id)
598
599 policy_key = None
600
601 catalog = self._search_regex(
602 r'catalog\(({.+?})\);', webpage, 'catalog', default=None)
603 if catalog:
604 catalog = self._parse_json(
605 js_to_json(catalog), video_id, fatal=False)
606 if catalog:
607 policy_key = catalog.get('policyKey')
608
609 if not policy_key:
610 policy_key = self._search_regex(
611 r'policyKey\s*:\s*(["\'])(?P<pk>.+?)\1',
612 webpage, 'policy key', group='pk')
613
614 store_pk(policy_key)
615 return policy_key
616
617 api_url = 'https://edge.api.brightcove.com/playback/v1/accounts/%s/%ss/%s' % (account_id, content_type, video_id)
618 headers = {}
619 referrer = smuggled_data.get('referrer')
620 if referrer:
621 headers.update({
622 'Referer': referrer,
623 'Origin': re.search(r'https?://[^/]+', referrer).group(0),
624 })
625
626 for _ in range(2):
627 if not policy_key:
628 policy_key = extract_policy_key()
629 policy_key_extracted = True
630 headers['Accept'] = 'application/json;pk=%s' % policy_key
631 try:
632 json_data = self._download_json(api_url, video_id, headers=headers)
633 break
634 except ExtractorError as e:
635 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (401, 403):
636 json_data = self._parse_json(e.cause.read().decode(), video_id)[0]
637 message = json_data.get('message') or json_data['error_code']
638 if json_data.get('error_subcode') == 'CLIENT_GEO':
639 self.raise_geo_restricted(msg=message)
640 elif json_data.get('error_code') == 'INVALID_POLICY_KEY' and not policy_key_extracted:
641 policy_key = None
642 store_pk(None)
643 continue
644 raise ExtractorError(message, expected=True)
645 raise
646
647 errors = json_data.get('errors')
648 if errors and errors[0].get('error_subcode') == 'TVE_AUTH':
649 custom_fields = json_data['custom_fields']
650 tve_token = self._extract_mvpd_auth(
651 smuggled_data['source_url'], video_id,
652 custom_fields['bcadobepassrequestorid'],
653 custom_fields['bcadobepassresourceid'])
654 json_data = self._download_json(
655 api_url, video_id, headers={
656 'Accept': 'application/json;pk=%s' % policy_key
657 }, query={
658 'tveToken': tve_token,
659 })
660
661 if content_type == 'playlist':
662 return self.playlist_result(
663 [self._parse_brightcove_metadata(vid, vid.get('id'), headers)
664 for vid in json_data.get('videos', []) if vid.get('id')],
665 json_data.get('id'), json_data.get('name'),
666 json_data.get('description'))
667
668 return self._parse_brightcove_metadata(
669 json_data, video_id, headers=headers)