[bbc.co.uk:iplayer:playlist] Skip unavailable test
[youtube-dl] / youtube_dl / extractor / bbc.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     float_or_none,
10     int_or_none,
11     parse_duration,
12     parse_iso8601,
13     unescapeHTML,
14 )
15 from ..compat import (
16     compat_etree_fromstring,
17     compat_HTTPError,
18 )
19
20
21 class BBCCoUkIE(InfoExtractor):
22     IE_NAME = 'bbc.co.uk'
23     IE_DESC = 'BBC iPlayer'
24     _ID_REGEX = r'[pb][\da-z]{7}'
25     _VALID_URL = r'''(?x)
26                     https?://
27                         (?:www\.)?bbc\.co\.uk/
28                         (?:
29                             programmes/(?!articles/)|
30                             iplayer(?:/[^/]+)?/(?:episode/|playlist/)|
31                             music/clips[/#]|
32                             radio/player/
33                         )
34                         (?P<id>%s)(?!/(?:episodes|broadcasts|clips))
35                     ''' % _ID_REGEX
36
37     _MEDIASELECTOR_URLS = [
38         # Provides HQ HLS streams with even better quality that pc mediaset but fails
39         # with geolocation in some cases when it's even not geo restricted at all (e.g.
40         # http://www.bbc.co.uk/programmes/b06bp7lf). Also may fail with selectionunavailable.
41         'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/iptv-all/vpid/%s',
42         'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/%s',
43     ]
44
45     _MEDIASELECTION_NS = 'http://bbc.co.uk/2008/mp/mediaselection'
46     _EMP_PLAYLIST_NS = 'http://bbc.co.uk/2008/emp/playlist'
47
48     _NAMESPACES = (
49         _MEDIASELECTION_NS,
50         _EMP_PLAYLIST_NS,
51     )
52
53     _TESTS = [
54         {
55             'url': 'http://www.bbc.co.uk/programmes/b039g8p7',
56             'info_dict': {
57                 'id': 'b039d07m',
58                 'ext': 'flv',
59                 'title': 'Leonard Cohen, Kaleidoscope - BBC Radio 4',
60                 'description': 'The Canadian poet and songwriter reflects on his musical career.',
61             },
62             'params': {
63                 # rtmp download
64                 'skip_download': True,
65             }
66         },
67         {
68             'url': 'http://www.bbc.co.uk/iplayer/episode/b00yng5w/The_Man_in_Black_Series_3_The_Printed_Name/',
69             'info_dict': {
70                 'id': 'b00yng1d',
71                 'ext': 'flv',
72                 'title': 'The Man in Black: Series 3: The Printed Name',
73                 'description': "Mark Gatiss introduces Nicholas Pierpan's chilling tale of a writer's devilish pact with a mysterious man. Stars Ewan Bailey.",
74                 'duration': 1800,
75             },
76             'params': {
77                 # rtmp download
78                 'skip_download': True,
79             },
80             'skip': 'Episode is no longer available on BBC iPlayer Radio',
81         },
82         {
83             'url': 'http://www.bbc.co.uk/iplayer/episode/b03vhd1f/The_Voice_UK_Series_3_Blind_Auditions_5/',
84             'info_dict': {
85                 'id': 'b00yng1d',
86                 'ext': 'flv',
87                 'title': 'The Voice UK: Series 3: Blind Auditions 5',
88                 '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.',
89                 'duration': 5100,
90             },
91             'params': {
92                 # rtmp download
93                 'skip_download': True,
94             },
95             'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
96         },
97         {
98             'url': 'http://www.bbc.co.uk/iplayer/episode/p026c7jt/tomorrows-worlds-the-unearthly-history-of-science-fiction-2-invasion',
99             'info_dict': {
100                 'id': 'b03k3pb7',
101                 'ext': 'flv',
102                 'title': "Tomorrow's Worlds: The Unearthly History of Science Fiction",
103                 'description': '2. Invasion',
104                 'duration': 3600,
105             },
106             'params': {
107                 # rtmp download
108                 'skip_download': True,
109             },
110             'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
111         }, {
112             'url': 'http://www.bbc.co.uk/programmes/b04v20dw',
113             'info_dict': {
114                 'id': 'b04v209v',
115                 'ext': 'flv',
116                 'title': 'Pete Tong, The Essential New Tune Special',
117                 'description': "Pete has a very special mix - all of 2014's Essential New Tunes!",
118                 'duration': 10800,
119             },
120             'params': {
121                 # rtmp download
122                 'skip_download': True,
123             },
124             'skip': 'Episode is no longer available on BBC iPlayer Radio',
125         }, {
126             'url': 'http://www.bbc.co.uk/music/clips/p022h44b',
127             'note': 'Audio',
128             'info_dict': {
129                 'id': 'p022h44j',
130                 'ext': 'flv',
131                 'title': 'BBC Proms Music Guides, Rachmaninov: Symphonic Dances',
132                 'description': "In this Proms Music Guide, Andrew McGregor looks at Rachmaninov's Symphonic Dances.",
133                 'duration': 227,
134             },
135             'params': {
136                 # rtmp download
137                 'skip_download': True,
138             }
139         }, {
140             'url': 'http://www.bbc.co.uk/music/clips/p025c0zz',
141             'note': 'Video',
142             'info_dict': {
143                 'id': 'p025c103',
144                 'ext': 'flv',
145                 'title': 'Reading and Leeds Festival, 2014, Rae Morris - Closer (Live on BBC Three)',
146                 'description': 'Rae Morris performs Closer for BBC Three at Reading 2014',
147                 'duration': 226,
148             },
149             'params': {
150                 # rtmp download
151                 'skip_download': True,
152             }
153         }, {
154             'url': 'http://www.bbc.co.uk/iplayer/episode/b054fn09/ad/natural-world-20152016-2-super-powered-owls',
155             'info_dict': {
156                 'id': 'p02n76xf',
157                 'ext': 'flv',
158                 'title': 'Natural World, 2015-2016: 2. Super Powered Owls',
159                 'description': 'md5:e4db5c937d0e95a7c6b5e654d429183d',
160                 'duration': 3540,
161             },
162             'params': {
163                 # rtmp download
164                 'skip_download': True,
165             },
166             'skip': 'geolocation',
167         }, {
168             'url': 'http://www.bbc.co.uk/iplayer/episode/b05zmgwn/royal-academy-summer-exhibition',
169             'info_dict': {
170                 'id': 'b05zmgw1',
171                 'ext': 'flv',
172                 '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.',
173                 'title': 'Royal Academy Summer Exhibition',
174                 'duration': 3540,
175             },
176             'params': {
177                 # rtmp download
178                 'skip_download': True,
179             },
180             'skip': 'geolocation',
181         }, {
182             # iptv-all mediaset fails with geolocation however there is no geo restriction
183             # for this programme at all
184             'url': 'http://www.bbc.co.uk/programmes/b06rkn85',
185             'info_dict': {
186                 'id': 'b06rkms3',
187                 'ext': 'flv',
188                 'title': "Best of the Mini-Mixes 2015: Part 3, Annie Mac's Friday Night - BBC Radio 1",
189                 'description': "Annie has part three in the Best of the Mini-Mixes 2015, plus the year's Most Played!",
190             },
191             'params': {
192                 # rtmp download
193                 'skip_download': True,
194             },
195             'skip': 'Now it\'s really geo-restricted',
196         }, {
197             # compact player (https://github.com/rg3/youtube-dl/issues/8147)
198             'url': 'http://www.bbc.co.uk/programmes/p028bfkf/player',
199             'info_dict': {
200                 'id': 'p028bfkj',
201                 'ext': 'flv',
202                 'title': 'Extract from BBC documentary Look Stranger - Giant Leeks and Magic Brews',
203                 'description': 'Extract from BBC documentary Look Stranger - Giant Leeks and Magic Brews',
204             },
205             'params': {
206                 # rtmp download
207                 'skip_download': True,
208             },
209         }, {
210             'url': 'http://www.bbc.co.uk/iplayer/playlist/p01dvks4',
211             'only_matching': True,
212         }, {
213             'url': 'http://www.bbc.co.uk/music/clips#p02frcc3',
214             'only_matching': True,
215         }, {
216             'url': 'http://www.bbc.co.uk/iplayer/cbeebies/episode/b0480276/bing-14-atchoo',
217             'only_matching': True,
218         }, {
219             'url': 'http://www.bbc.co.uk/radio/player/p03cchwf',
220             'only_matching': True,
221         }
222     ]
223
224     class MediaSelectionError(Exception):
225         def __init__(self, id):
226             self.id = id
227
228     def _extract_asx_playlist(self, connection, programme_id):
229         asx = self._download_xml(connection.get('href'), programme_id, 'Downloading ASX playlist')
230         return [ref.get('href') for ref in asx.findall('./Entry/ref')]
231
232     def _extract_connection(self, connection, programme_id):
233         formats = []
234         kind = connection.get('kind')
235         protocol = connection.get('protocol')
236         supplier = connection.get('supplier')
237         if protocol == 'http':
238             href = connection.get('href')
239             transfer_format = connection.get('transferFormat')
240             # ASX playlist
241             if supplier == 'asx':
242                 for i, ref in enumerate(self._extract_asx_playlist(connection, programme_id)):
243                     formats.append({
244                         'url': ref,
245                         'format_id': 'ref%s_%s' % (i, supplier),
246                     })
247             # Skip DASH until supported
248             elif transfer_format == 'dash':
249                 pass
250             elif transfer_format == 'hls':
251                 formats.extend(self._extract_m3u8_formats(
252                     href, programme_id, ext='mp4', entry_protocol='m3u8_native',
253                     m3u8_id=supplier, fatal=False))
254             # Direct link
255             else:
256                 formats.append({
257                     'url': href,
258                     'format_id': supplier or kind or protocol,
259                 })
260         elif protocol == 'rtmp':
261             application = connection.get('application', 'ondemand')
262             auth_string = connection.get('authString')
263             identifier = connection.get('identifier')
264             server = connection.get('server')
265             formats.append({
266                 'url': '%s://%s/%s?%s' % (protocol, server, application, auth_string),
267                 'play_path': identifier,
268                 'app': '%s?%s' % (application, auth_string),
269                 'page_url': 'http://www.bbc.co.uk',
270                 'player_url': 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf',
271                 'rtmp_live': False,
272                 'ext': 'flv',
273                 'format_id': supplier,
274             })
275         return formats
276
277     def _extract_items(self, playlist):
278         return playlist.findall('./{%s}item' % self._EMP_PLAYLIST_NS)
279
280     def _findall_ns(self, element, xpath):
281         elements = []
282         for ns in self._NAMESPACES:
283             elements.extend(element.findall(xpath % ns))
284         return elements
285
286     def _extract_medias(self, media_selection):
287         error = media_selection.find('./{%s}error' % self._MEDIASELECTION_NS)
288         if error is None:
289             media_selection.find('./{%s}error' % self._EMP_PLAYLIST_NS)
290         if error is not None:
291             raise BBCCoUkIE.MediaSelectionError(error.get('id'))
292         return self._findall_ns(media_selection, './{%s}media')
293
294     def _extract_connections(self, media):
295         return self._findall_ns(media, './{%s}connection')
296
297     def _extract_video(self, media, programme_id):
298         formats = []
299         vbr = int_or_none(media.get('bitrate'))
300         vcodec = media.get('encoding')
301         service = media.get('service')
302         width = int_or_none(media.get('width'))
303         height = int_or_none(media.get('height'))
304         file_size = int_or_none(media.get('media_file_size'))
305         for connection in self._extract_connections(media):
306             conn_formats = self._extract_connection(connection, programme_id)
307             for format in conn_formats:
308                 format.update({
309                     'width': width,
310                     'height': height,
311                     'vbr': vbr,
312                     'vcodec': vcodec,
313                     'filesize': file_size,
314                 })
315                 if service:
316                     format['format_id'] = '%s_%s' % (service, format['format_id'])
317             formats.extend(conn_formats)
318         return formats
319
320     def _extract_audio(self, media, programme_id):
321         formats = []
322         abr = int_or_none(media.get('bitrate'))
323         acodec = media.get('encoding')
324         service = media.get('service')
325         for connection in self._extract_connections(media):
326             conn_formats = self._extract_connection(connection, programme_id)
327             for format in conn_formats:
328                 format.update({
329                     'format_id': '%s_%s' % (service, format['format_id']),
330                     'abr': abr,
331                     'acodec': acodec,
332                     'vcodec': 'none',
333                 })
334             formats.extend(conn_formats)
335         return formats
336
337     def _get_subtitles(self, media, programme_id):
338         subtitles = {}
339         for connection in self._extract_connections(media):
340             captions = self._download_xml(connection.get('href'), programme_id, 'Downloading captions')
341             lang = captions.get('{http://www.w3.org/XML/1998/namespace}lang', 'en')
342             subtitles[lang] = [
343                 {
344                     'url': connection.get('href'),
345                     'ext': 'ttml',
346                 },
347             ]
348         return subtitles
349
350     def _raise_extractor_error(self, media_selection_error):
351         raise ExtractorError(
352             '%s returned error: %s' % (self.IE_NAME, media_selection_error.id),
353             expected=True)
354
355     def _download_media_selector(self, programme_id):
356         last_exception = None
357         for mediaselector_url in self._MEDIASELECTOR_URLS:
358             try:
359                 return self._download_media_selector_url(
360                     mediaselector_url % programme_id, programme_id)
361             except BBCCoUkIE.MediaSelectionError as e:
362                 if e.id in ('notukerror', 'geolocation', 'selectionunavailable'):
363                     last_exception = e
364                     continue
365                 self._raise_extractor_error(e)
366         self._raise_extractor_error(last_exception)
367
368     def _download_media_selector_url(self, url, programme_id=None):
369         try:
370             media_selection = self._download_xml(
371                 url, programme_id, 'Downloading media selection XML')
372         except ExtractorError as ee:
373             if isinstance(ee.cause, compat_HTTPError) and ee.cause.code in (403, 404):
374                 media_selection = compat_etree_fromstring(ee.cause.read().decode('utf-8'))
375             else:
376                 raise
377         return self._process_media_selector(media_selection, programme_id)
378
379     def _process_media_selector(self, media_selection, programme_id):
380         formats = []
381         subtitles = None
382
383         for media in self._extract_medias(media_selection):
384             kind = media.get('kind')
385             if kind == 'audio':
386                 formats.extend(self._extract_audio(media, programme_id))
387             elif kind == 'video':
388                 formats.extend(self._extract_video(media, programme_id))
389             elif kind == 'captions':
390                 subtitles = self.extract_subtitles(media, programme_id)
391         return formats, subtitles
392
393     def _download_playlist(self, playlist_id):
394         try:
395             playlist = self._download_json(
396                 'http://www.bbc.co.uk/programmes/%s/playlist.json' % playlist_id,
397                 playlist_id, 'Downloading playlist JSON')
398
399             version = playlist.get('defaultAvailableVersion')
400             if version:
401                 smp_config = version['smpConfig']
402                 title = smp_config['title']
403                 description = smp_config['summary']
404                 for item in smp_config['items']:
405                     kind = item['kind']
406                     if kind != 'programme' and kind != 'radioProgramme':
407                         continue
408                     programme_id = item.get('vpid')
409                     duration = int_or_none(item.get('duration'))
410                     formats, subtitles = self._download_media_selector(programme_id)
411                 return programme_id, title, description, duration, formats, subtitles
412         except ExtractorError as ee:
413             if not (isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404):
414                 raise
415
416         # fallback to legacy playlist
417         return self._process_legacy_playlist(playlist_id)
418
419     def _process_legacy_playlist_url(self, url, display_id):
420         playlist = self._download_legacy_playlist_url(url, display_id)
421         return self._extract_from_legacy_playlist(playlist, display_id)
422
423     def _process_legacy_playlist(self, playlist_id):
424         return self._process_legacy_playlist_url(
425             'http://www.bbc.co.uk/iplayer/playlist/%s' % playlist_id, playlist_id)
426
427     def _download_legacy_playlist_url(self, url, playlist_id=None):
428         return self._download_xml(
429             url, playlist_id, 'Downloading legacy playlist XML')
430
431     def _extract_from_legacy_playlist(self, playlist, playlist_id):
432         no_items = playlist.find('./{%s}noItems' % self._EMP_PLAYLIST_NS)
433         if no_items is not None:
434             reason = no_items.get('reason')
435             if reason == 'preAvailability':
436                 msg = 'Episode %s is not yet available' % playlist_id
437             elif reason == 'postAvailability':
438                 msg = 'Episode %s is no longer available' % playlist_id
439             elif reason == 'noMedia':
440                 msg = 'Episode %s is not currently available' % playlist_id
441             else:
442                 msg = 'Episode %s is not available: %s' % (playlist_id, reason)
443             raise ExtractorError(msg, expected=True)
444
445         for item in self._extract_items(playlist):
446             kind = item.get('kind')
447             if kind != 'programme' and kind != 'radioProgramme':
448                 continue
449             title = playlist.find('./{%s}title' % self._EMP_PLAYLIST_NS).text
450             description_el = playlist.find('./{%s}summary' % self._EMP_PLAYLIST_NS)
451             description = description_el.text if description_el is not None else None
452
453             def get_programme_id(item):
454                 def get_from_attributes(item):
455                     for p in('identifier', 'group'):
456                         value = item.get(p)
457                         if value and re.match(r'^[pb][\da-z]{7}$', value):
458                             return value
459                 get_from_attributes(item)
460                 mediator = item.find('./{%s}mediator' % self._EMP_PLAYLIST_NS)
461                 if mediator is not None:
462                     return get_from_attributes(mediator)
463
464             programme_id = get_programme_id(item)
465             duration = int_or_none(item.get('duration'))
466
467             if programme_id:
468                 formats, subtitles = self._download_media_selector(programme_id)
469             else:
470                 formats, subtitles = self._process_media_selector(item, playlist_id)
471                 programme_id = playlist_id
472
473         return programme_id, title, description, duration, formats, subtitles
474
475     def _real_extract(self, url):
476         group_id = self._match_id(url)
477
478         webpage = self._download_webpage(url, group_id, 'Downloading video page')
479
480         programme_id = None
481         duration = None
482
483         tviplayer = self._search_regex(
484             r'mediator\.bind\(({.+?})\s*,\s*document\.getElementById',
485             webpage, 'player', default=None)
486
487         if tviplayer:
488             player = self._parse_json(tviplayer, group_id).get('player', {})
489             duration = int_or_none(player.get('duration'))
490             programme_id = player.get('vpid')
491
492         if not programme_id:
493             programme_id = self._search_regex(
494                 r'"vpid"\s*:\s*"(%s)"' % self._ID_REGEX, webpage, 'vpid', fatal=False, default=None)
495
496         if programme_id:
497             formats, subtitles = self._download_media_selector(programme_id)
498             title = self._og_search_title(webpage, default=None) or self._html_search_regex(
499                 (r'<h2[^>]+id="parent-title"[^>]*>(.+?)</h2>',
500                  r'<div[^>]+class="info"[^>]*>\s*<h1>(.+?)</h1>'), webpage, 'title')
501             description = self._search_regex(
502                 (r'<p class="[^"]*medium-description[^"]*">([^<]+)</p>',
503                  r'<div[^>]+class="info_+synopsis"[^>]*>([^<]+)</div>'),
504                 webpage, 'description', default=None)
505             if not description:
506                 description = self._html_search_meta('description', webpage)
507         else:
508             programme_id, title, description, duration, formats, subtitles = self._download_playlist(group_id)
509
510         self._sort_formats(formats)
511
512         return {
513             'id': programme_id,
514             'title': title,
515             'description': description,
516             'thumbnail': self._og_search_thumbnail(webpage, default=None),
517             'duration': duration,
518             'formats': formats,
519             'subtitles': subtitles,
520         }
521
522
523 class BBCIE(BBCCoUkIE):
524     IE_NAME = 'bbc'
525     IE_DESC = 'BBC'
526     _VALID_URL = r'https?://(?:www\.)?bbc\.(?:com|co\.uk)/(?:[^/]+/)+(?P<id>[^/#?]+)'
527
528     _MEDIASELECTOR_URLS = [
529         # Provides HQ HLS streams but fails with geolocation in some cases when it's
530         # even not geo restricted at all
531         'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/iptv-all/vpid/%s',
532         # Provides more formats, namely direct mp4 links, but fails on some videos with
533         # notukerror for non UK (?) users (e.g.
534         # http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret)
535         'http://open.live.bbc.co.uk/mediaselector/4/mtis/stream/%s',
536         # Provides fewer formats, but works everywhere for everybody (hopefully)
537         'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/journalism-pc/vpid/%s',
538     ]
539
540     _TESTS = [{
541         # article with multiple videos embedded with data-playable containing vpids
542         'url': 'http://www.bbc.com/news/world-europe-32668511',
543         'info_dict': {
544             'id': 'world-europe-32668511',
545             'title': 'Russia stages massive WW2 parade despite Western boycott',
546             'description': 'md5:00ff61976f6081841f759a08bf78cc9c',
547         },
548         'playlist_count': 2,
549     }, {
550         # article with multiple videos embedded with data-playable (more videos)
551         'url': 'http://www.bbc.com/news/business-28299555',
552         'info_dict': {
553             'id': 'business-28299555',
554             'title': 'Farnborough Airshow: Video highlights',
555             'description': 'BBC reports and video highlights at the Farnborough Airshow.',
556         },
557         'playlist_count': 9,
558         'skip': 'Save time',
559     }, {
560         # article with multiple videos embedded with `new SMP()`
561         # broken
562         'url': 'http://www.bbc.co.uk/blogs/adamcurtis/entries/3662a707-0af9-3149-963f-47bea720b460',
563         'info_dict': {
564             'id': '3662a707-0af9-3149-963f-47bea720b460',
565             'title': 'BUGGER',
566         },
567         'playlist_count': 18,
568     }, {
569         # single video embedded with data-playable containing vpid
570         'url': 'http://www.bbc.com/news/world-europe-32041533',
571         'info_dict': {
572             'id': 'p02mprgb',
573             'ext': 'mp4',
574             'title': 'Aerial footage showed the site of the crash in the Alps - courtesy BFM TV',
575             'description': 'md5:2868290467291b37feda7863f7a83f54',
576             'duration': 47,
577             'timestamp': 1427219242,
578             'upload_date': '20150324',
579         },
580         'params': {
581             # rtmp download
582             'skip_download': True,
583         }
584     }, {
585         # article with single video embedded with data-playable containing XML playlist
586         # with direct video links as progressiveDownloadUrl (for now these are extracted)
587         # and playlist with f4m and m3u8 as streamingUrl
588         'url': 'http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu',
589         'info_dict': {
590             'id': '150615_telabyad_kentin_cogu',
591             'ext': 'mp4',
592             'title': "Tel Abyad'da IŞİD bayrağı indirildi YPG bayrağı Ã§ekildi",
593             'description': 'md5:33a4805a855c9baf7115fcbde57e7025',
594             'timestamp': 1434397334,
595             'upload_date': '20150615',
596         },
597         'params': {
598             'skip_download': True,
599         }
600     }, {
601         # single video embedded with data-playable containing XML playlists (regional section)
602         'url': 'http://www.bbc.com/mundo/video_fotos/2015/06/150619_video_honduras_militares_hospitales_corrupcion_aw',
603         'info_dict': {
604             'id': '150619_video_honduras_militares_hospitales_corrupcion_aw',
605             'ext': 'mp4',
606             'title': 'Honduras militariza sus hospitales por nuevo escándalo de corrupción',
607             'description': 'md5:1525f17448c4ee262b64b8f0c9ce66c8',
608             'timestamp': 1434713142,
609             'upload_date': '20150619',
610         },
611         'params': {
612             'skip_download': True,
613         }
614     }, {
615         # single video from video playlist embedded with vxp-playlist-data JSON
616         'url': 'http://www.bbc.com/news/video_and_audio/must_see/33376376',
617         'info_dict': {
618             'id': 'p02w6qjc',
619             'ext': 'mp4',
620             'title': '''Judge Mindy Glazer: "I'm sorry to see you here... I always wondered what happened to you"''',
621             'duration': 56,
622             'description': '''Judge Mindy Glazer: "I'm sorry to see you here... I always wondered what happened to you"''',
623         },
624         'params': {
625             'skip_download': True,
626         }
627     }, {
628         # single video story with digitalData
629         'url': 'http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret',
630         'info_dict': {
631             'id': 'p02q6gc4',
632             'ext': 'flv',
633             'title': 'Sri Lanka’s spicy secret',
634             'description': 'As a new train line to Jaffna opens up the country’s north, travellers can experience a truly distinct slice of Tamil culture.',
635             'timestamp': 1437674293,
636             'upload_date': '20150723',
637         },
638         'params': {
639             # rtmp download
640             'skip_download': True,
641         }
642     }, {
643         # single video story without digitalData
644         'url': 'http://www.bbc.com/autos/story/20130513-hyundais-rock-star',
645         'info_dict': {
646             'id': 'p018zqqg',
647             'ext': 'mp4',
648             'title': 'Hyundai Santa Fe Sport: Rock star',
649             'description': 'md5:b042a26142c4154a6e472933cf20793d',
650             'timestamp': 1415867444,
651             'upload_date': '20141113',
652         },
653         'params': {
654             # rtmp download
655             'skip_download': True,
656         }
657     }, {
658         # single video with playlist.sxml URL in playlist param
659         'url': 'http://www.bbc.com/sport/0/football/33653409',
660         'info_dict': {
661             'id': 'p02xycnp',
662             'ext': 'mp4',
663             'title': 'Transfers: Cristiano Ronaldo to Man Utd, Arsenal to spend?',
664             'description': 'BBC Sport\'s David Ornstein has the latest transfer gossip, including rumours of a Manchester United return for Cristiano Ronaldo.',
665             'duration': 140,
666         },
667         'params': {
668             # rtmp download
669             'skip_download': True,
670         }
671     }, {
672         # article with multiple videos embedded with playlist.sxml in playlist param
673         'url': 'http://www.bbc.com/sport/0/football/34475836',
674         'info_dict': {
675             'id': '34475836',
676             'title': 'Jurgen Klopp: Furious football from a witty and winning coach',
677             'description': 'Fast-paced football, wit, wisdom and a ready smile - why Liverpool fans should come to love new boss Jurgen Klopp.',
678         },
679         'playlist_count': 3,
680     }, {
681         # school report article with single video
682         'url': 'http://www.bbc.co.uk/schoolreport/35744779',
683         'info_dict': {
684             'id': '35744779',
685             'title': 'School which breaks down barriers in Jerusalem',
686         },
687         'playlist_count': 1,
688     }, {
689         # single video with playlist URL from weather section
690         'url': 'http://www.bbc.com/weather/features/33601775',
691         'only_matching': True,
692     }, {
693         # custom redirection to www.bbc.com
694         'url': 'http://www.bbc.co.uk/news/science-environment-33661876',
695         'only_matching': True,
696     }, {
697         # single video article embedded with data-media-vpid
698         'url': 'http://www.bbc.co.uk/sport/rowing/35908187',
699         'only_matching': True,
700     }]
701
702     @classmethod
703     def suitable(cls, url):
704         EXCLUDE_IE = (BBCCoUkIE, BBCCoUkArticleIE, BBCCoUkIPlayerPlaylistIE, BBCCoUkPlaylistIE)
705         return (False if any(ie.suitable(url) for ie in EXCLUDE_IE)
706                 else super(BBCIE, cls).suitable(url))
707
708     def _extract_from_media_meta(self, media_meta, video_id):
709         # Direct links to media in media metadata (e.g.
710         # http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu)
711         # TODO: there are also f4m and m3u8 streams incorporated in playlist.sxml
712         source_files = media_meta.get('sourceFiles')
713         if source_files:
714             return [{
715                 'url': f['url'],
716                 'format_id': format_id,
717                 'ext': f.get('encoding'),
718                 'tbr': float_or_none(f.get('bitrate'), 1000),
719                 'filesize': int_or_none(f.get('filesize')),
720             } for format_id, f in source_files.items() if f.get('url')], []
721
722         programme_id = media_meta.get('externalId')
723         if programme_id:
724             return self._download_media_selector(programme_id)
725
726         # Process playlist.sxml as legacy playlist
727         href = media_meta.get('href')
728         if href:
729             playlist = self._download_legacy_playlist_url(href)
730             _, _, _, _, formats, subtitles = self._extract_from_legacy_playlist(playlist, video_id)
731             return formats, subtitles
732
733         return [], []
734
735     def _extract_from_playlist_sxml(self, url, playlist_id, timestamp):
736         programme_id, title, description, duration, formats, subtitles = \
737             self._process_legacy_playlist_url(url, playlist_id)
738         self._sort_formats(formats)
739         return {
740             'id': programme_id,
741             'title': title,
742             'description': description,
743             'duration': duration,
744             'timestamp': timestamp,
745             'formats': formats,
746             'subtitles': subtitles,
747         }
748
749     def _real_extract(self, url):
750         playlist_id = self._match_id(url)
751
752         webpage = self._download_webpage(url, playlist_id)
753
754         json_ld_info = self._search_json_ld(webpage, playlist_id, default=None)
755         timestamp = json_ld_info.get('timestamp')
756
757         playlist_title = json_ld_info.get('title')
758         if not playlist_title:
759             playlist_title = self._og_search_title(
760                 webpage, default=None) or self._html_search_regex(
761                 r'<title>(.+?)</title>', webpage, 'playlist title', default=None)
762             if playlist_title:
763                 playlist_title = re.sub(r'(.+)\s*-\s*BBC.*?$', r'\1', playlist_title).strip()
764
765         playlist_description = json_ld_info.get(
766             'description') or self._og_search_description(webpage, default=None)
767
768         if not timestamp:
769             timestamp = parse_iso8601(self._search_regex(
770                 [r'<meta[^>]+property="article:published_time"[^>]+content="([^"]+)"',
771                  r'itemprop="datePublished"[^>]+datetime="([^"]+)"',
772                  r'"datePublished":\s*"([^"]+)'],
773                 webpage, 'date', default=None))
774
775         entries = []
776
777         # article with multiple videos embedded with playlist.sxml (e.g.
778         # http://www.bbc.com/sport/0/football/34475836)
779         playlists = re.findall(r'<param[^>]+name="playlist"[^>]+value="([^"]+)"', webpage)
780         playlists.extend(re.findall(r'data-media-id="([^"]+/playlist\.sxml)"', webpage))
781         if playlists:
782             entries = [
783                 self._extract_from_playlist_sxml(playlist_url, playlist_id, timestamp)
784                 for playlist_url in playlists]
785
786         # news article with multiple videos embedded with data-playable
787         data_playables = re.findall(r'data-playable=(["\'])({.+?})\1', webpage)
788         if data_playables:
789             for _, data_playable_json in data_playables:
790                 data_playable = self._parse_json(
791                     unescapeHTML(data_playable_json), playlist_id, fatal=False)
792                 if not data_playable:
793                     continue
794                 settings = data_playable.get('settings', {})
795                 if settings:
796                     # data-playable with video vpid in settings.playlistObject.items (e.g.
797                     # http://www.bbc.com/news/world-us-canada-34473351)
798                     playlist_object = settings.get('playlistObject', {})
799                     if playlist_object:
800                         items = playlist_object.get('items')
801                         if items and isinstance(items, list):
802                             title = playlist_object['title']
803                             description = playlist_object.get('summary')
804                             duration = int_or_none(items[0].get('duration'))
805                             programme_id = items[0].get('vpid')
806                             formats, subtitles = self._download_media_selector(programme_id)
807                             self._sort_formats(formats)
808                             entries.append({
809                                 'id': programme_id,
810                                 'title': title,
811                                 'description': description,
812                                 'timestamp': timestamp,
813                                 'duration': duration,
814                                 'formats': formats,
815                                 'subtitles': subtitles,
816                             })
817                     else:
818                         # data-playable without vpid but with a playlist.sxml URLs
819                         # in otherSettings.playlist (e.g.
820                         # http://www.bbc.com/turkce/multimedya/2015/10/151010_vid_ankara_patlama_ani)
821                         playlist = data_playable.get('otherSettings', {}).get('playlist', {})
822                         if playlist:
823                             for key in ('progressiveDownload', 'streaming'):
824                                 playlist_url = playlist.get('%sUrl' % key)
825                                 if not playlist_url:
826                                     continue
827                                 try:
828                                     entries.append(self._extract_from_playlist_sxml(
829                                         playlist_url, playlist_id, timestamp))
830                                 except Exception as e:
831                                     # Some playlist URL may fail with 500, at the same time
832                                     # the other one may work fine (e.g.
833                                     # http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu)
834                                     if isinstance(e.cause, compat_HTTPError) and e.cause.code == 500:
835                                         continue
836                                     raise
837
838         if entries:
839             return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)
840
841         # single video story (e.g. http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret)
842         programme_id = self._search_regex(
843             [r'data-(?:video-player|media)-vpid="(%s)"' % self._ID_REGEX,
844              r'<param[^>]+name="externalIdentifier"[^>]+value="(%s)"' % self._ID_REGEX,
845              r'videoId\s*:\s*["\'](%s)["\']' % self._ID_REGEX],
846             webpage, 'vpid', default=None)
847
848         if programme_id:
849             formats, subtitles = self._download_media_selector(programme_id)
850             self._sort_formats(formats)
851             # digitalData may be missing (e.g. http://www.bbc.com/autos/story/20130513-hyundais-rock-star)
852             digital_data = self._parse_json(
853                 self._search_regex(
854                     r'var\s+digitalData\s*=\s*({.+?});?\n', webpage, 'digital data', default='{}'),
855                 programme_id, fatal=False)
856             page_info = digital_data.get('page', {}).get('pageInfo', {})
857             title = page_info.get('pageName') or self._og_search_title(webpage)
858             description = page_info.get('description') or self._og_search_description(webpage)
859             timestamp = parse_iso8601(page_info.get('publicationDate')) or timestamp
860             return {
861                 'id': programme_id,
862                 'title': title,
863                 'description': description,
864                 'timestamp': timestamp,
865                 'formats': formats,
866                 'subtitles': subtitles,
867             }
868
869         def extract_all(pattern):
870             return list(filter(None, map(
871                 lambda s: self._parse_json(s, playlist_id, fatal=False),
872                 re.findall(pattern, webpage))))
873
874         # Multiple video article (e.g.
875         # http://www.bbc.co.uk/blogs/adamcurtis/entries/3662a707-0af9-3149-963f-47bea720b460)
876         EMBED_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:[^/]+/)+%s(?:\b[^"]+)?' % self._ID_REGEX
877         entries = []
878         for match in extract_all(r'new\s+SMP\(({.+?})\)'):
879             embed_url = match.get('playerSettings', {}).get('externalEmbedUrl')
880             if embed_url and re.match(EMBED_URL, embed_url):
881                 entries.append(embed_url)
882         entries.extend(re.findall(
883             r'setPlaylist\("(%s)"\)' % EMBED_URL, webpage))
884         if entries:
885             return self.playlist_result(
886                 [self.url_result(entry, 'BBCCoUk') for entry in entries],
887                 playlist_id, playlist_title, playlist_description)
888
889         # Multiple video article (e.g. http://www.bbc.com/news/world-europe-32668511)
890         medias = extract_all(r"data-media-meta='({[^']+})'")
891
892         if not medias:
893             # Single video article (e.g. http://www.bbc.com/news/video_and_audio/international)
894             media_asset = self._search_regex(
895                 r'mediaAssetPage\.init\(\s*({.+?}), "/',
896                 webpage, 'media asset', default=None)
897             if media_asset:
898                 media_asset_page = self._parse_json(media_asset, playlist_id, fatal=False)
899                 medias = []
900                 for video in media_asset_page.get('videos', {}).values():
901                     medias.extend(video.values())
902
903         if not medias:
904             # Multiple video playlist with single `now playing` entry (e.g.
905             # http://www.bbc.com/news/video_and_audio/must_see/33767813)
906             vxp_playlist = self._parse_json(
907                 self._search_regex(
908                     r'<script[^>]+class="vxp-playlist-data"[^>]+type="application/json"[^>]*>([^<]+)</script>',
909                     webpage, 'playlist data'),
910                 playlist_id)
911             playlist_medias = []
912             for item in vxp_playlist:
913                 media = item.get('media')
914                 if not media:
915                     continue
916                 playlist_medias.append(media)
917                 # Download single video if found media with asset id matching the video id from URL
918                 if item.get('advert', {}).get('assetId') == playlist_id:
919                     medias = [media]
920                     break
921             # Fallback to the whole playlist
922             if not medias:
923                 medias = playlist_medias
924
925         entries = []
926         for num, media_meta in enumerate(medias, start=1):
927             formats, subtitles = self._extract_from_media_meta(media_meta, playlist_id)
928             if not formats:
929                 continue
930             self._sort_formats(formats)
931
932             video_id = media_meta.get('externalId')
933             if not video_id:
934                 video_id = playlist_id if len(medias) == 1 else '%s-%s' % (playlist_id, num)
935
936             title = media_meta.get('caption')
937             if not title:
938                 title = playlist_title if len(medias) == 1 else '%s - Video %s' % (playlist_title, num)
939
940             duration = int_or_none(media_meta.get('durationInSeconds')) or parse_duration(media_meta.get('duration'))
941
942             images = []
943             for image in media_meta.get('images', {}).values():
944                 images.extend(image.values())
945             if 'image' in media_meta:
946                 images.append(media_meta['image'])
947
948             thumbnails = [{
949                 'url': image.get('href'),
950                 'width': int_or_none(image.get('width')),
951                 'height': int_or_none(image.get('height')),
952             } for image in images]
953
954             entries.append({
955                 'id': video_id,
956                 'title': title,
957                 'thumbnails': thumbnails,
958                 'duration': duration,
959                 'timestamp': timestamp,
960                 'formats': formats,
961                 'subtitles': subtitles,
962             })
963
964         return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)
965
966
967 class BBCCoUkArticleIE(InfoExtractor):
968     _VALID_URL = r'https?://www.bbc.co.uk/programmes/articles/(?P<id>[a-zA-Z0-9]+)'
969     IE_NAME = 'bbc.co.uk:article'
970     IE_DESC = 'BBC articles'
971
972     _TEST = {
973         'url': 'http://www.bbc.co.uk/programmes/articles/3jNQLTMrPlYGTBn0WV6M2MS/not-your-typical-role-model-ada-lovelace-the-19th-century-programmer',
974         'info_dict': {
975             'id': '3jNQLTMrPlYGTBn0WV6M2MS',
976             'title': 'Calculating Ada: The Countess of Computing - Not your typical role model: Ada Lovelace the 19th century programmer - BBC Four',
977             'description': 'Hannah Fry reveals some of her surprising discoveries about Ada Lovelace during filming.',
978         },
979         'playlist_count': 4,
980         'add_ie': ['BBCCoUk'],
981     }
982
983     def _real_extract(self, url):
984         playlist_id = self._match_id(url)
985
986         webpage = self._download_webpage(url, playlist_id)
987
988         title = self._og_search_title(webpage)
989         description = self._og_search_description(webpage).strip()
990
991         entries = [self.url_result(programme_url) for programme_url in re.findall(
992             r'<div[^>]+typeof="Clip"[^>]+resource="([^"]+)"', webpage)]
993
994         return self.playlist_result(entries, playlist_id, title, description)
995
996
997 class BBCCoUkPlaylistBaseIE(InfoExtractor):
998     def _real_extract(self, url):
999         playlist_id = self._match_id(url)
1000
1001         webpage = self._download_webpage(url, playlist_id)
1002
1003         entries = [
1004             self.url_result(self._URL_TEMPLATE % video_id, BBCCoUkIE.ie_key())
1005             for video_id in re.findall(
1006                 self._VIDEO_ID_TEMPLATE % BBCCoUkIE._ID_REGEX, webpage)]
1007
1008         title, description = self._extract_title_and_description(webpage)
1009
1010         return self.playlist_result(entries, playlist_id, title, description)
1011
1012
1013 class BBCCoUkIPlayerPlaylistIE(BBCCoUkPlaylistBaseIE):
1014     IE_NAME = 'bbc.co.uk:iplayer:playlist'
1015     _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/iplayer/episodes/(?P<id>%s)' % BBCCoUkIE._ID_REGEX
1016     _URL_TEMPLATE = 'http://www.bbc.co.uk/iplayer/episode/%s'
1017     _VIDEO_ID_TEMPLATE = r'data-ip-id=["\'](%s)'
1018     _TEST = {
1019         'url': 'http://www.bbc.co.uk/iplayer/episodes/b05rcz9v',
1020         'info_dict': {
1021             'id': 'b05rcz9v',
1022             'title': 'The Disappearance',
1023             'description': 'French thriller serial about a missing teenager.',
1024         },
1025         'playlist_mincount': 6,
1026         'skip': 'This programme is not currently available on BBC iPlayer',
1027     }
1028
1029     def _extract_title_and_description(self, webpage):
1030         title = self._search_regex(r'<h1>([^<]+)</h1>', webpage, 'title', fatal=False)
1031         description = self._search_regex(
1032             r'<p[^>]+class=(["\'])subtitle\1[^>]*>(?P<value>[^<]+)</p>',
1033             webpage, 'description', fatal=False, group='value')
1034         return title, description
1035
1036
1037 class BBCCoUkPlaylistIE(BBCCoUkPlaylistBaseIE):
1038     IE_NAME = 'bbc.co.uk:playlist'
1039     _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/programmes/(?P<id>%s)/(?:episodes|broadcasts|clips)' % BBCCoUkIE._ID_REGEX
1040     _URL_TEMPLATE = 'http://www.bbc.co.uk/programmes/%s'
1041     _VIDEO_ID_TEMPLATE = r'data-pid=["\'](%s)'
1042     _TESTS = [{
1043         'url': 'http://www.bbc.co.uk/programmes/b05rcz9v/clips',
1044         'info_dict': {
1045             'id': 'b05rcz9v',
1046             'title': 'The Disappearance - Clips - BBC Four',
1047             'description': 'French thriller serial about a missing teenager.',
1048         },
1049         'playlist_mincount': 7,
1050     }, {
1051         'url': 'http://www.bbc.co.uk/programmes/b05rcz9v/broadcasts/2016/06',
1052         'only_matching': True,
1053     }, {
1054         'url': 'http://www.bbc.co.uk/programmes/b05rcz9v/clips',
1055         'only_matching': True,
1056     }, {
1057         'url': 'http://www.bbc.co.uk/programmes/b055jkys/episodes/player',
1058         'only_matching': True,
1059     }]
1060
1061     def _extract_title_and_description(self, webpage):
1062         title = self._og_search_title(webpage, fatal=False)
1063         description = self._og_search_description(webpage)
1064         return title, description