]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/bbc.py
[youtube] Use 'vp8' and 'vp9' in lowercase (fixes #6358)
[yt-dlp.git] / youtube_dl / extractor / bbc.py
CommitLineData
9afa1770 1# coding: utf-8
082c6c86
S
2from __future__ import unicode_literals
3
9afa1770 4import re
c056efa2 5import xml.etree.ElementTree
082c6c86 6
f13b1e7d 7from .common import InfoExtractor
8683b4d8
S
8from ..utils import (
9 ExtractorError,
9afa1770 10 float_or_none,
8683b4d8 11 int_or_none,
9afa1770
S
12 parse_duration,
13 parse_iso8601,
8683b4d8 14)
c056efa2 15from ..compat import compat_HTTPError
082c6c86
S
16
17
f13b1e7d 18class BBCCoUkIE(InfoExtractor):
082c6c86 19 IE_NAME = 'bbc.co.uk'
2e3fd9ec 20 IE_DESC = 'BBC iPlayer'
0692ef86 21 _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:(?:(?:programmes|iplayer(?:/[^/]+)?/(?:episode|playlist))/)|music/clips[/#])(?P<id>[\da-z]{8})'
082c6c86 22
9afa1770 23 _MEDIASELECTOR_URL = 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/%s'
a8b081a0 24
2e3fd9ec
S
25 _TESTS = [
26 {
f2d0fc68 27 'url': 'http://www.bbc.co.uk/programmes/b039g8p7',
2e3fd9ec 28 'info_dict': {
f2d0fc68 29 'id': 'b039d07m',
2e3fd9ec 30 'ext': 'flv',
c4914185
S
31 'title': 'Kaleidoscope, Leonard Cohen',
32 'description': 'The Canadian poet and songwriter reflects on his musical career.',
f2d0fc68 33 'duration': 1740,
2e3fd9ec
S
34 },
35 'params': {
36 # rtmp download
37 'skip_download': True,
38 }
082c6c86 39 },
2e3fd9ec
S
40 {
41 'url': 'http://www.bbc.co.uk/iplayer/episode/b00yng5w/The_Man_in_Black_Series_3_The_Printed_Name/',
42 'info_dict': {
43 'id': 'b00yng1d',
44 'ext': 'flv',
45 'title': 'The Man in Black: Series 3: The Printed Name',
46 'description': "Mark Gatiss introduces Nicholas Pierpan's chilling tale of a writer's devilish pact with a mysterious man. Stars Ewan Bailey.",
47 'duration': 1800,
48 },
49 'params': {
50 # rtmp download
51 'skip_download': True,
c7f0177f
S
52 },
53 'skip': 'Episode is no longer available on BBC iPlayer Radio',
2e3fd9ec
S
54 },
55 {
56 'url': 'http://www.bbc.co.uk/iplayer/episode/b03vhd1f/The_Voice_UK_Series_3_Blind_Auditions_5/',
57 'info_dict': {
58 'id': 'b00yng1d',
59 'ext': 'flv',
17968e44
S
60 'title': 'The Voice UK: Series 3: Blind Auditions 5',
61 'description': "Emma Willis and Marvin Humes present the fifth set of blind auditions in the singing competition, as the coaches continue to build their teams based on voice alone.",
62 'duration': 5100,
2e3fd9ec
S
63 },
64 'params': {
65 # rtmp download
66 'skip_download': True,
67 },
68 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
c056efa2
S
69 },
70 {
71 'url': 'http://www.bbc.co.uk/iplayer/episode/p026c7jt/tomorrows-worlds-the-unearthly-history-of-science-fiction-2-invasion',
72 'info_dict': {
73 'id': 'b03k3pb7',
74 'ext': 'flv',
75 'title': "Tomorrow's Worlds: The Unearthly History of Science Fiction",
76 'description': '2. Invasion',
77 'duration': 3600,
78 },
79 'params': {
80 # rtmp download
81 'skip_download': True,
82 },
83 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
ae6986fb
S
84 }, {
85 'url': 'http://www.bbc.co.uk/programmes/b04v20dw',
86 'info_dict': {
87 'id': 'b04v209v',
88 'ext': 'flv',
89 'title': 'Pete Tong, The Essential New Tune Special',
90 'description': "Pete has a very special mix - all of 2014's Essential New Tunes!",
91 'duration': 10800,
92 },
93 'params': {
94 # rtmp download
95 'skip_download': True,
96 }
c7e67594
S
97 }, {
98 'url': 'http://www.bbc.co.uk/music/clips/p02frcc3',
99 'note': 'Audio',
100 'info_dict': {
101 'id': 'p02frcch',
102 'ext': 'flv',
103 'title': 'Pete Tong, Past, Present and Future Special, Madeon - After Hours mix',
104 'description': 'French house superstar Madeon takes us out of the club and onto the after party.',
105 'duration': 3507,
106 },
107 'params': {
108 # rtmp download
109 'skip_download': True,
110 }
111 }, {
112 'url': 'http://www.bbc.co.uk/music/clips/p025c0zz',
113 'note': 'Video',
114 'info_dict': {
115 'id': 'p025c103',
116 'ext': 'flv',
117 'title': 'Reading and Leeds Festival, 2014, Rae Morris - Closer (Live on BBC Three)',
118 'description': 'Rae Morris performs Closer for BBC Three at Reading 2014',
119 'duration': 226,
120 },
121 'params': {
122 # rtmp download
123 'skip_download': True,
124 }
e68ae99a
S
125 }, {
126 'url': 'http://www.bbc.co.uk/iplayer/episode/b054fn09/ad/natural-world-20152016-2-super-powered-owls',
127 'info_dict': {
128 'id': 'p02n76xf',
129 'ext': 'flv',
130 'title': 'Natural World, 2015-2016: 2. Super Powered Owls',
131 'description': 'md5:e4db5c937d0e95a7c6b5e654d429183d',
132 'duration': 3540,
133 },
134 'params': {
135 # rtmp download
136 'skip_download': True,
137 },
138 'skip': 'geolocation',
25fa8d66
YCH
139 }, {
140 'url': 'http://www.bbc.co.uk/iplayer/episode/b05zmgwn/royal-academy-summer-exhibition',
141 'info_dict': {
142 'id': 'b05zmgw1',
143 'ext': 'flv',
144 'description': 'Kirsty Wark and Morgan Quaintance visit the Royal Academy as it prepares for its annual artistic extravaganza, meeting people who have come together to make the show unique.',
145 'title': 'Royal Academy Summer Exhibition',
146 'duration': 3540,
147 },
148 'params': {
149 # rtmp download
150 'skip_download': True,
151 },
152 'skip': 'geolocation',
31763975
S
153 }, {
154 'url': 'http://www.bbc.co.uk/iplayer/playlist/p01dvks4',
155 'only_matching': True,
c7e67594
S
156 }, {
157 'url': 'http://www.bbc.co.uk/music/clips#p02frcc3',
158 'only_matching': True,
0692ef86
S
159 }, {
160 'url': 'http://www.bbc.co.uk/iplayer/cbeebies/episode/b0480276/bing-14-atchoo',
161 'only_matching': True,
ae6986fb 162 }
2e3fd9ec
S
163 ]
164
165 def _extract_asx_playlist(self, connection, programme_id):
166 asx = self._download_xml(connection.get('href'), programme_id, 'Downloading ASX playlist')
167 return [ref.get('href') for ref in asx.findall('./Entry/ref')]
168
169 def _extract_connection(self, connection, programme_id):
170 formats = []
171 protocol = connection.get('protocol')
172 supplier = connection.get('supplier')
173 if protocol == 'http':
174 href = connection.get('href')
7a896817 175 transfer_format = connection.get('transferFormat')
2e3fd9ec
S
176 # ASX playlist
177 if supplier == 'asx':
178 for i, ref in enumerate(self._extract_asx_playlist(connection, programme_id)):
179 formats.append({
180 'url': ref,
181 'format_id': 'ref%s_%s' % (i, supplier),
182 })
7a896817
S
183 # Skip DASH until supported
184 elif transfer_format == 'dash':
185 pass
2e3fd9ec
S
186 # Direct link
187 else:
188 formats.append({
189 'url': href,
190 'format_id': supplier,
191 })
192 elif protocol == 'rtmp':
193 application = connection.get('application', 'ondemand')
194 auth_string = connection.get('authString')
195 identifier = connection.get('identifier')
196 server = connection.get('server')
197 formats.append({
198 'url': '%s://%s/%s?%s' % (protocol, server, application, auth_string),
199 'play_path': identifier,
200 'app': '%s?%s' % (application, auth_string),
201 'page_url': 'http://www.bbc.co.uk',
202 'player_url': 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf',
203 'rtmp_live': False,
204 'ext': 'flv',
205 'format_id': supplier,
206 })
207 return formats
208
209 def _extract_items(self, playlist):
210 return playlist.findall('./{http://bbc.co.uk/2008/emp/playlist}item')
211
212 def _extract_medias(self, media_selection):
c056efa2
S
213 error = media_selection.find('./{http://bbc.co.uk/2008/mp/mediaselection}error')
214 if error is not None:
215 raise ExtractorError(
216 '%s returned error: %s' % (self.IE_NAME, error.get('id')), expected=True)
2e3fd9ec
S
217 return media_selection.findall('./{http://bbc.co.uk/2008/mp/mediaselection}media')
218
219 def _extract_connections(self, media):
220 return media.findall('./{http://bbc.co.uk/2008/mp/mediaselection}connection')
221
222 def _extract_video(self, media, programme_id):
223 formats = []
d97f5cd7 224 vbr = int_or_none(media.get('bitrate'))
2e3fd9ec
S
225 vcodec = media.get('encoding')
226 service = media.get('service')
d97f5cd7
S
227 width = int_or_none(media.get('width'))
228 height = int_or_none(media.get('height'))
229 file_size = int_or_none(media.get('media_file_size'))
2e3fd9ec
S
230 for connection in self._extract_connections(media):
231 conn_formats = self._extract_connection(connection, programme_id)
232 for format in conn_formats:
233 format.update({
234 'format_id': '%s_%s' % (service, format['format_id']),
235 'width': width,
236 'height': height,
237 'vbr': vbr,
238 'vcodec': vcodec,
239 'filesize': file_size,
240 })
241 formats.extend(conn_formats)
242 return formats
243
244 def _extract_audio(self, media, programme_id):
245 formats = []
d97f5cd7 246 abr = int_or_none(media.get('bitrate'))
2e3fd9ec
S
247 acodec = media.get('encoding')
248 service = media.get('service')
249 for connection in self._extract_connections(media):
250 conn_formats = self._extract_connection(connection, programme_id)
251 for format in conn_formats:
252 format.update({
253 'format_id': '%s_%s' % (service, format['format_id']),
254 'abr': abr,
255 'acodec': acodec,
256 })
257 formats.extend(conn_formats)
258 return formats
259
f13b1e7d 260 def _get_subtitles(self, media, programme_id):
2e3fd9ec
S
261 subtitles = {}
262 for connection in self._extract_connections(media):
263 captions = self._download_xml(connection.get('href'), programme_id, 'Downloading captions')
264 lang = captions.get('{http://www.w3.org/XML/1998/namespace}lang', 'en')
f13b1e7d
JMF
265 subtitles[lang] = [
266 {
267 'url': connection.get('href'),
268 'ext': 'ttml',
269 },
f13b1e7d 270 ]
2e3fd9ec 271 return subtitles
082c6c86 272
c056efa2 273 def _download_media_selector(self, programme_id):
9afa1770
S
274 return self._download_media_selector_url(
275 self._MEDIASELECTOR_URL % programme_id, programme_id)
276
277 def _download_media_selector_url(self, url, programme_id=None):
c056efa2
S
278 try:
279 media_selection = self._download_xml(
9afa1770 280 url, programme_id, 'Downloading media selection XML')
c056efa2
S
281 except ExtractorError as ee:
282 if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
974a6146 283 media_selection = xml.etree.ElementTree.fromstring(ee.cause.read().decode('utf-8'))
2e3fd9ec 284 else:
c056efa2 285 raise
9afa1770 286 return self._process_media_selector(media_selection, programme_id)
082c6c86 287
9afa1770 288 def _process_media_selector(self, media_selection, programme_id):
082c6c86 289 formats = []
2e3fd9ec
S
290 subtitles = None
291
c056efa2
S
292 for media in self._extract_medias(media_selection):
293 kind = media.get('kind')
294 if kind == 'audio':
295 formats.extend(self._extract_audio(media, programme_id))
296 elif kind == 'video':
297 formats.extend(self._extract_video(media, programme_id))
298 elif kind == 'captions':
f13b1e7d 299 subtitles = self.extract_subtitles(media, programme_id)
2e3fd9ec 300
c056efa2 301 return formats, subtitles
2e3fd9ec 302
ae6986fb
S
303 def _download_playlist(self, playlist_id):
304 try:
305 playlist = self._download_json(
306 'http://www.bbc.co.uk/programmes/%s/playlist.json' % playlist_id,
307 playlist_id, 'Downloading playlist JSON')
308
309 version = playlist.get('defaultAvailableVersion')
310 if version:
311 smp_config = version['smpConfig']
312 title = smp_config['title']
313 description = smp_config['summary']
314 for item in smp_config['items']:
315 kind = item['kind']
316 if kind != 'programme' and kind != 'radioProgramme':
317 continue
318 programme_id = item.get('vpid')
d97f5cd7 319 duration = int_or_none(item.get('duration'))
ae6986fb
S
320 formats, subtitles = self._download_media_selector(programme_id)
321 return programme_id, title, description, duration, formats, subtitles
322 except ExtractorError as ee:
f813928e 323 if not (isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404):
ae6986fb
S
324 raise
325
326 # fallback to legacy playlist
9afa1770
S
327 return self._process_legacy_playlist(playlist_id)
328
329 def _process_legacy_playlist_url(self, url, display_id):
330 playlist = self._download_legacy_playlist_url(url, display_id)
331 return self._extract_from_legacy_playlist(playlist, display_id)
332
333 def _process_legacy_playlist(self, playlist_id):
334 return self._process_legacy_playlist_url(
335 'http://www.bbc.co.uk/iplayer/playlist/%s' % playlist_id, playlist_id)
336
337 def _download_legacy_playlist_url(self, url, playlist_id=None):
338 return self._download_xml(
339 url, playlist_id, 'Downloading legacy playlist XML')
ae6986fb 340
9afa1770 341 def _extract_from_legacy_playlist(self, playlist, playlist_id):
ae6986fb
S
342 no_items = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}noItems')
343 if no_items is not None:
344 reason = no_items.get('reason')
345 if reason == 'preAvailability':
346 msg = 'Episode %s is not yet available' % playlist_id
347 elif reason == 'postAvailability':
348 msg = 'Episode %s is no longer available' % playlist_id
349 elif reason == 'noMedia':
350 msg = 'Episode %s is not currently available' % playlist_id
351 else:
352 msg = 'Episode %s is not available: %s' % (playlist_id, reason)
353 raise ExtractorError(msg, expected=True)
354
355 for item in self._extract_items(playlist):
356 kind = item.get('kind')
357 if kind != 'programme' and kind != 'radioProgramme':
358 continue
359 title = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}title').text
360 description = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}summary').text
9afa1770
S
361
362 def get_programme_id(item):
363 def get_from_attributes(item):
364 for p in('identifier', 'group'):
365 value = item.get(p)
366 if value and re.match(r'^[pb][\da-z]{7}$', value):
367 return value
368 get_from_attributes(item)
369 mediator = item.find('./{http://bbc.co.uk/2008/emp/playlist}mediator')
370 if mediator is not None:
371 return get_from_attributes(mediator)
372
373 programme_id = get_programme_id(item)
d97f5cd7 374 duration = int_or_none(item.get('duration'))
9afa1770
S
375 # TODO: programme_id can be None and media items can be incorporated right inside
376 # playlist's item (e.g. http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu)
377 # as f4m and m3u8
ae6986fb
S
378 formats, subtitles = self._download_media_selector(programme_id)
379
380 return programme_id, title, description, duration, formats, subtitles
381
c056efa2
S
382 def _real_extract(self, url):
383 group_id = self._match_id(url)
384
385 webpage = self._download_webpage(url, group_id, 'Downloading video page')
386
8683b4d8
S
387 programme_id = None
388
389 tviplayer = self._search_regex(
390 r'mediator\.bind\(({.+?})\s*,\s*document\.getElementById',
391 webpage, 'player', default=None)
392
393 if tviplayer:
394 player = self._parse_json(tviplayer, group_id).get('player', {})
395 duration = int_or_none(player.get('duration'))
396 programme_id = player.get('vpid')
397
398 if not programme_id:
399 programme_id = self._search_regex(
400 r'"vpid"\s*:\s*"([\da-z]{8})"', webpage, 'vpid', fatal=False, default=None)
401
c056efa2 402 if programme_id:
c056efa2 403 formats, subtitles = self._download_media_selector(programme_id)
8683b4d8
S
404 title = self._og_search_title(webpage)
405 description = self._search_regex(
25fa8d66 406 r'<p class="[^"]*medium-description[^"]*">([^<]+)</p>',
8683b4d8 407 webpage, 'description', fatal=False)
c056efa2 408 else:
ae6986fb 409 programme_id, title, description, duration, formats, subtitles = self._download_playlist(group_id)
2e3fd9ec 410
082c6c86
S
411 self._sort_formats(formats)
412
413 return {
2e3fd9ec 414 'id': programme_id,
082c6c86
S
415 'title': title,
416 'description': description,
650cfd0c 417 'thumbnail': self._og_search_thumbnail(webpage, default=None),
082c6c86
S
418 'duration': duration,
419 'formats': formats,
2e3fd9ec 420 'subtitles': subtitles,
5f6a1245 421 }
10273d6e 422
423
9afa1770
S
424class BBCIE(BBCCoUkIE):
425 IE_NAME = 'bbc'
426 IE_DESC = 'BBC'
427 _VALID_URL = r'https?://(?:www\.)?bbc\.(?:com|co\.uk)/(?:[^/]+/)+(?P<id>[^/#?]+)'
10273d6e 428
9afa1770 429 # fails with notukerror for some videos
51da40e6 430 # _MEDIASELECTOR_URL = 'http://open.live.bbc.co.uk/mediaselector/4/mtis/stream/%s'
9afa1770 431 _MEDIASELECTOR_URL = 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/journalism-pc/vpid/%s'
10273d6e 432
433 _TESTS = [{
9afa1770
S
434 # article with multiple videos embedded with data-media-meta containing
435 # playlist.sxml, externalId and no direct video links
10273d6e 436 'url': 'http://www.bbc.com/news/world-europe-32668511',
437 'info_dict': {
438 'id': 'world-europe-32668511',
439 'title': 'Russia stages massive WW2 parade despite Western boycott',
9afa1770 440 'description': 'md5:00ff61976f6081841f759a08bf78cc9c',
10273d6e 441 },
442 'playlist_count': 2,
a3bfddfa 443 }, {
9afa1770 444 # article with multiple videos embedded with data-media-meta (more videos)
10273d6e 445 'url': 'http://www.bbc.com/news/business-28299555',
446 'info_dict': {
447 'id': 'business-28299555',
448 'title': 'Farnborough Airshow: Video highlights',
9afa1770 449 'description': 'BBC reports and video highlights at the Farnborough Airshow.',
10273d6e 450 },
451 'playlist_count': 9,
9afa1770 452 'skip': 'Save time',
a3bfddfa 453 }, {
9afa1770 454 # single video embedded with mediaAssetPage.init()
10273d6e 455 'url': 'http://www.bbc.com/news/world-europe-32041533',
10273d6e 456 'info_dict': {
457 'id': 'p02mprgb',
9afa1770 458 'ext': 'flv',
10273d6e 459 'title': 'Aerial footage showed the site of the crash in the Alps - courtesy BFM TV',
10273d6e 460 'duration': 47,
9afa1770 461 'timestamp': 1427219242,
da92eeae 462 'upload_date': '20150324',
10273d6e 463 },
464 'params': {
9afa1770 465 # rtmp download
10273d6e 466 'skip_download': True,
467 }
a3bfddfa 468 }, {
9afa1770
S
469 # article with single video embedded with data-media-meta containing
470 # direct video links (for now these are extracted) and playlist.xml (with
471 # media items as f4m and m3u8 - currently unsupported)
de939d89 472 'url': 'http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu',
de939d89 473 'info_dict': {
9afa1770 474 'id': '150615_telabyad_kentin_cogu',
de939d89 475 'ext': 'mp4',
9afa1770 476 'title': "YPG: Tel Abyad'ın tamamı kontrolümüzde",
de939d89 477 'duration': 47,
9afa1770 478 'timestamp': 1434397334,
da92eeae 479 'upload_date': '20150615',
de939d89 480 },
481 'params': {
482 'skip_download': True,
483 }
a3bfddfa 484 }, {
9afa1770 485 # single video embedded with mediaAssetPage.init() (regional section)
de939d89 486 'url': 'http://www.bbc.com/mundo/video_fotos/2015/06/150619_video_honduras_militares_hospitales_corrupcion_aw',
de939d89 487 'info_dict': {
9afa1770 488 'id': '150619_video_honduras_militares_hospitales_corrupcion_aw',
de939d89 489 'ext': 'mp4',
9afa1770 490 'title': 'Honduras militariza sus hospitales por nuevo escándalo de corrupción',
de939d89 491 'duration': 87,
9afa1770 492 'timestamp': 1434713142,
da92eeae 493 'upload_date': '20150619',
de939d89 494 },
495 'params': {
496 'skip_download': True,
497 }
9afa1770
S
498 }, {
499 # single video story with digitalData
500 'url': 'http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret',
501 'info_dict': {
502 'id': 'p02q6gc4',
503 'ext': 'flv',
504 'title': 'Sri Lanka’s spicy secret',
505 'description': 'As a new train line to Jaffna opens up the country’s north, travellers can experience a truly distinct slice of Tamil culture.',
506 'timestamp': 1437674293,
507 'upload_date': '20150723',
508 },
509 'params': {
510 # rtmp download
511 'skip_download': True,
512 }
513 }, {
514 # single video story without digitalData
515 'url': 'http://www.bbc.com/autos/story/20130513-hyundais-rock-star',
516 'info_dict': {
517 'id': 'p018zqqg',
518 'ext': 'flv',
519 'title': 'Hyundai Santa Fe Sport: Rock star',
520 'description': 'md5:b042a26142c4154a6e472933cf20793d',
521 'timestamp': 1368473503,
522 'upload_date': '20130513',
523 },
524 'params': {
525 # rtmp download
526 'skip_download': True,
527 }
528 }, {
529 # single video with playlist.sxml URL
530 'url': 'http://www.bbc.com/sport/0/football/33653409',
531 'info_dict': {
532 'id': 'p02xycnp',
533 'ext': 'flv',
534 'title': 'Transfers: Cristiano Ronaldo to Man Utd, Arsenal to spend?',
535 'description': 'md5:398fca0e2e701c609d726e034fa1fc89',
536 'duration': 140,
537 },
538 'params': {
539 # rtmp download
540 'skip_download': True,
541 }
542 }, {
543 # single video with playlist URL from weather section
544 'url': 'http://www.bbc.com/weather/features/33601775',
545 'only_matching': True,
546 }, {
547 # custom redirection to www.bbc.com
548 'url': 'http://www.bbc.co.uk/news/science-environment-33661876',
549 'only_matching': True,
10273d6e 550 }]
551
9afa1770
S
552 @classmethod
553 def suitable(cls, url):
554 return False if BBCCoUkIE.suitable(url) else super(BBCIE, cls).suitable(url)
555
556 def _extract_from_media_meta(self, media_meta, video_id):
557 # Direct links to media in media metadata (e.g.
558 # http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu)
559 # TODO: there are also f4m and m3u8 streams incorporated in playlist.sxml
560 source_files = media_meta.get('sourceFiles')
561 if source_files:
562 return [{
563 'url': f['url'],
564 'format_id': format_id,
565 'ext': f.get('encoding'),
566 'tbr': float_or_none(f.get('bitrate'), 1000),
567 'filesize': int_or_none(f.get('filesize')),
568 } for format_id, f in source_files.items() if f.get('url')], []
569
570 programme_id = media_meta.get('externalId')
571 if programme_id:
572 return self._download_media_selector(programme_id)
573
574 # Process playlist.sxml as legacy playlist
575 href = media_meta.get('href')
576 if href:
577 playlist = self._download_legacy_playlist_url(href)
578 _, _, _, _, formats, subtitles = self._extract_from_legacy_playlist(playlist, video_id)
579 return formats, subtitles
580
581 return [], []
582
10273d6e 583 def _real_extract(self, url):
9afa1770
S
584 playlist_id = self._match_id(url)
585
586 webpage = self._download_webpage(url, playlist_id)
587
588 timestamp = parse_iso8601(self._search_regex(
589 [r'"datePublished":\s*"([^"]+)',
590 r'<meta[^>]+property="article:published_time"[^>]+content="([^"]+)"',
591 r'itemprop="datePublished"[^>]+datetime="([^"]+)"'],
592 webpage, 'date', default=None))
593
594 # single video with playlist.sxml URL (e.g. http://www.bbc.com/sport/0/football/3365340ng)
595 playlist = self._search_regex(
596 r'<param[^>]+name="playlist"[^>]+value="([^"]+)"',
597 webpage, 'playlist', default=None)
598 if playlist:
599 programme_id, title, description, duration, formats, subtitles = \
600 self._process_legacy_playlist_url(playlist, playlist_id)
601 self._sort_formats(formats)
602 return {
603 'id': programme_id,
604 'title': title,
605 'description': description,
606 'duration': duration,
607 'timestamp': timestamp,
608 'formats': formats,
609 'subtitles': subtitles,
610 }
de939d89 611
9afa1770
S
612 # single video story (e.g. http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret)
613 programme_id = self._search_regex(
614 [r'data-video-player-vpid="([\da-z]{8})"',
615 r'<param[^>]+name="externalIdentifier"[^>]+value="([\da-z]{8})"'],
616 webpage, 'vpid', default=None)
617 if programme_id:
618 formats, subtitles = self._download_media_selector(programme_id)
619 self._sort_formats(formats)
620 # digitalData may be missing (e.g. http://www.bbc.com/autos/story/20130513-hyundais-rock-star)
621 digital_data = self._parse_json(
622 self._search_regex(
623 r'var\s+digitalData\s*=\s*({.+?});?\n', webpage, 'digital data', default='{}'),
624 programme_id, fatal=False)
625 page_info = digital_data.get('page', {}).get('pageInfo', {})
626 title = page_info.get('pageName') or self._og_search_title(webpage)
627 description = page_info.get('description') or self._og_search_description(webpage)
628 timestamp = parse_iso8601(page_info.get('publicationDate')) or timestamp
629 return {
630 'id': programme_id,
631 'title': title,
632 'description': description,
633 'timestamp': timestamp,
634 'formats': formats,
635 'subtitles': subtitles,
636 }
a3bfddfa 637
9afa1770
S
638 playlist_title = self._html_search_regex(
639 r'<title>(.*?)(?:\s*-\s*BBC [^ ]+)?</title>', webpage, 'playlist title')
640 playlist_description = self._og_search_description(webpage)
641
642 # Multiple video article (e.g. http://www.bbc.com/news/world-europe-32668511)
643 medias = list(filter(None, map(
644 lambda s: self._parse_json(s, playlist_id, fatal=False),
645 re.findall(r"data-media-meta='({[^']+})'", webpage))))
646
647 if not medias:
648 # Single video article (e.g. http://www.bbc.com/news/video_and_audio/international)
649 media_asset_page = self._parse_json(
650 self._search_regex(
651 r'mediaAssetPage\.init\(\s*({.+?}), "/', webpage, 'media asset'),
652 playlist_id)
653 medias = []
654 for video in media_asset_page.get('videos', {}).values():
655 medias.extend(video.values())
656
657 entries = []
658 for num, media_meta in enumerate(medias, start=1):
659 formats, subtitles = self._extract_from_media_meta(media_meta, playlist_id)
660 if not formats:
661 continue
10273d6e 662 self._sort_formats(formats)
663
9afa1770
S
664 video_id = media_meta.get('externalId')
665 if not video_id:
666 video_id = playlist_id if len(medias) == 1 else '%s-%s' % (playlist_id, num)
667
668 title = media_meta.get('caption')
669 if not title:
670 title = playlist_title if len(medias) == 1 else '%s - Video %s' % (playlist_title, num)
671
672 duration = int_or_none(media_meta.get('durationInSeconds')) or parse_duration(media_meta.get('duration'))
da92eeae 673
9afa1770
S
674 images = []
675 for image in media_meta.get('images', {}).values():
676 images.extend(image.values())
677 if 'image' in media_meta:
678 images.append(media_meta['image'])
679
680 thumbnails = [{
681 'url': image.get('href'),
682 'width': int_or_none(image.get('width')),
683 'height': int_or_none(image.get('height')),
684 } for image in images]
685
686 entries.append({
687 'id': video_id,
10273d6e 688 'title': title,
9afa1770 689 'thumbnails': thumbnails,
10273d6e 690 'duration': duration,
9afa1770 691 'timestamp': timestamp,
10273d6e 692 'formats': formats,
693 'subtitles': subtitles,
a3bfddfa 694 })
10273d6e 695
9afa1770 696 return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)