]>
Commit | Line | Data |
---|---|---|
9afa1770 | 1 | # coding: utf-8 |
082c6c86 S |
2 | from __future__ import unicode_literals |
3 | ||
9afa1770 | 4 | import re |
c056efa2 | 5 | import xml.etree.ElementTree |
082c6c86 | 6 | |
f13b1e7d | 7 | from .common import InfoExtractor |
8683b4d8 S |
8 | from ..utils import ( |
9 | ExtractorError, | |
9afa1770 | 10 | float_or_none, |
8683b4d8 | 11 | int_or_none, |
9afa1770 S |
12 | parse_duration, |
13 | parse_iso8601, | |
dab062fb | 14 | unescapeHTML, |
8683b4d8 | 15 | ) |
c056efa2 | 16 | from ..compat import compat_HTTPError |
082c6c86 | 17 | |
d12a1a47 | 18 | |
f13b1e7d | 19 | class BBCCoUkIE(InfoExtractor): |
082c6c86 | 20 | IE_NAME = 'bbc.co.uk' |
2e3fd9ec | 21 | IE_DESC = 'BBC iPlayer' |
0692ef86 | 22 | _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:(?:(?:programmes|iplayer(?:/[^/]+)?/(?:episode|playlist))/)|music/clips[/#])(?P<id>[\da-z]{8})' |
082c6c86 | 23 | |
d12a1a47 | 24 | _MEDIASELECTOR_URLS = [ |
26ccc68b S |
25 | # Provides HQ HLS streams with even better quality that pc mediaset but fails |
26 | # with geolocation in some cases when it's even not geo restricted at all (e.g. | |
27 | # http://www.bbc.co.uk/programmes/b06bp7lf) | |
d1c694ea | 28 | 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/iptv-all/vpid/%s', |
d12a1a47 S |
29 | 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/%s', |
30 | ] | |
a8b081a0 | 31 | |
e6174ee9 S |
32 | _MEDIASELECTION_NS = 'http://bbc.co.uk/2008/mp/mediaselection' |
33 | _EMP_PLAYLIST_NS = 'http://bbc.co.uk/2008/emp/playlist' | |
34 | ||
35 | _NAMESPACES = ( | |
36 | _MEDIASELECTION_NS, | |
37 | _EMP_PLAYLIST_NS, | |
38 | ) | |
39 | ||
2e3fd9ec S |
40 | _TESTS = [ |
41 | { | |
f2d0fc68 | 42 | 'url': 'http://www.bbc.co.uk/programmes/b039g8p7', |
2e3fd9ec | 43 | 'info_dict': { |
f2d0fc68 | 44 | 'id': 'b039d07m', |
2e3fd9ec | 45 | 'ext': 'flv', |
c4914185 S |
46 | 'title': 'Kaleidoscope, Leonard Cohen', |
47 | 'description': 'The Canadian poet and songwriter reflects on his musical career.', | |
f2d0fc68 | 48 | 'duration': 1740, |
2e3fd9ec S |
49 | }, |
50 | 'params': { | |
51 | # rtmp download | |
52 | 'skip_download': True, | |
53 | } | |
082c6c86 | 54 | }, |
2e3fd9ec S |
55 | { |
56 | 'url': 'http://www.bbc.co.uk/iplayer/episode/b00yng5w/The_Man_in_Black_Series_3_The_Printed_Name/', | |
57 | 'info_dict': { | |
58 | 'id': 'b00yng1d', | |
59 | 'ext': 'flv', | |
60 | 'title': 'The Man in Black: Series 3: The Printed Name', | |
61 | 'description': "Mark Gatiss introduces Nicholas Pierpan's chilling tale of a writer's devilish pact with a mysterious man. Stars Ewan Bailey.", | |
62 | 'duration': 1800, | |
63 | }, | |
64 | 'params': { | |
65 | # rtmp download | |
66 | 'skip_download': True, | |
c7f0177f S |
67 | }, |
68 | 'skip': 'Episode is no longer available on BBC iPlayer Radio', | |
2e3fd9ec S |
69 | }, |
70 | { | |
71 | 'url': 'http://www.bbc.co.uk/iplayer/episode/b03vhd1f/The_Voice_UK_Series_3_Blind_Auditions_5/', | |
72 | 'info_dict': { | |
73 | 'id': 'b00yng1d', | |
74 | 'ext': 'flv', | |
17968e44 S |
75 | 'title': 'The Voice UK: Series 3: Blind Auditions 5', |
76 | '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.", | |
77 | 'duration': 5100, | |
2e3fd9ec S |
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', | |
c056efa2 S |
84 | }, |
85 | { | |
86 | 'url': 'http://www.bbc.co.uk/iplayer/episode/p026c7jt/tomorrows-worlds-the-unearthly-history-of-science-fiction-2-invasion', | |
87 | 'info_dict': { | |
88 | 'id': 'b03k3pb7', | |
89 | 'ext': 'flv', | |
90 | 'title': "Tomorrow's Worlds: The Unearthly History of Science Fiction", | |
91 | 'description': '2. Invasion', | |
92 | 'duration': 3600, | |
93 | }, | |
94 | 'params': { | |
95 | # rtmp download | |
96 | 'skip_download': True, | |
97 | }, | |
98 | 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only', | |
ae6986fb S |
99 | }, { |
100 | 'url': 'http://www.bbc.co.uk/programmes/b04v20dw', | |
101 | 'info_dict': { | |
102 | 'id': 'b04v209v', | |
103 | 'ext': 'flv', | |
104 | 'title': 'Pete Tong, The Essential New Tune Special', | |
105 | 'description': "Pete has a very special mix - all of 2014's Essential New Tunes!", | |
106 | 'duration': 10800, | |
107 | }, | |
108 | 'params': { | |
109 | # rtmp download | |
110 | 'skip_download': True, | |
111 | } | |
c7e67594 S |
112 | }, { |
113 | 'url': 'http://www.bbc.co.uk/music/clips/p02frcc3', | |
114 | 'note': 'Audio', | |
115 | 'info_dict': { | |
116 | 'id': 'p02frcch', | |
117 | 'ext': 'flv', | |
118 | 'title': 'Pete Tong, Past, Present and Future Special, Madeon - After Hours mix', | |
119 | 'description': 'French house superstar Madeon takes us out of the club and onto the after party.', | |
120 | 'duration': 3507, | |
121 | }, | |
122 | 'params': { | |
123 | # rtmp download | |
124 | 'skip_download': True, | |
125 | } | |
126 | }, { | |
127 | 'url': 'http://www.bbc.co.uk/music/clips/p025c0zz', | |
128 | 'note': 'Video', | |
129 | 'info_dict': { | |
130 | 'id': 'p025c103', | |
131 | 'ext': 'flv', | |
132 | 'title': 'Reading and Leeds Festival, 2014, Rae Morris - Closer (Live on BBC Three)', | |
133 | 'description': 'Rae Morris performs Closer for BBC Three at Reading 2014', | |
134 | 'duration': 226, | |
135 | }, | |
136 | 'params': { | |
137 | # rtmp download | |
138 | 'skip_download': True, | |
139 | } | |
e68ae99a S |
140 | }, { |
141 | 'url': 'http://www.bbc.co.uk/iplayer/episode/b054fn09/ad/natural-world-20152016-2-super-powered-owls', | |
142 | 'info_dict': { | |
143 | 'id': 'p02n76xf', | |
144 | 'ext': 'flv', | |
145 | 'title': 'Natural World, 2015-2016: 2. Super Powered Owls', | |
146 | 'description': 'md5:e4db5c937d0e95a7c6b5e654d429183d', | |
147 | 'duration': 3540, | |
148 | }, | |
149 | 'params': { | |
150 | # rtmp download | |
151 | 'skip_download': True, | |
152 | }, | |
153 | 'skip': 'geolocation', | |
25fa8d66 YCH |
154 | }, { |
155 | 'url': 'http://www.bbc.co.uk/iplayer/episode/b05zmgwn/royal-academy-summer-exhibition', | |
156 | 'info_dict': { | |
157 | 'id': 'b05zmgw1', | |
158 | 'ext': 'flv', | |
159 | '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.', | |
160 | 'title': 'Royal Academy Summer Exhibition', | |
161 | 'duration': 3540, | |
162 | }, | |
163 | 'params': { | |
164 | # rtmp download | |
165 | 'skip_download': True, | |
166 | }, | |
167 | 'skip': 'geolocation', | |
54914380 S |
168 | }, { |
169 | # iptv-all mediaset fails with geolocation however there is no geo restriction | |
170 | # for this programme at all | |
171 | 'url': 'http://www.bbc.co.uk/programmes/b06bp7lf', | |
172 | 'info_dict': { | |
173 | 'id': 'b06bp7kf', | |
174 | 'ext': 'flv', | |
175 | 'title': "Annie Mac's Friday Night, B.Traits sits in for Annie", | |
176 | 'description': 'B.Traits sits in for Annie Mac with a Mini-Mix from Disclosure.', | |
177 | 'duration': 10800, | |
178 | }, | |
179 | 'params': { | |
180 | # rtmp download | |
181 | 'skip_download': True, | |
182 | }, | |
31763975 S |
183 | }, { |
184 | 'url': 'http://www.bbc.co.uk/iplayer/playlist/p01dvks4', | |
185 | 'only_matching': True, | |
c7e67594 S |
186 | }, { |
187 | 'url': 'http://www.bbc.co.uk/music/clips#p02frcc3', | |
188 | 'only_matching': True, | |
0692ef86 S |
189 | }, { |
190 | 'url': 'http://www.bbc.co.uk/iplayer/cbeebies/episode/b0480276/bing-14-atchoo', | |
191 | 'only_matching': True, | |
ae6986fb | 192 | } |
2e3fd9ec S |
193 | ] |
194 | ||
d12a1a47 S |
195 | class MediaSelectionError(Exception): |
196 | def __init__(self, id): | |
197 | self.id = id | |
198 | ||
2e3fd9ec S |
199 | def _extract_asx_playlist(self, connection, programme_id): |
200 | asx = self._download_xml(connection.get('href'), programme_id, 'Downloading ASX playlist') | |
201 | return [ref.get('href') for ref in asx.findall('./Entry/ref')] | |
202 | ||
203 | def _extract_connection(self, connection, programme_id): | |
204 | formats = [] | |
e6174ee9 | 205 | kind = connection.get('kind') |
2e3fd9ec S |
206 | protocol = connection.get('protocol') |
207 | supplier = connection.get('supplier') | |
208 | if protocol == 'http': | |
209 | href = connection.get('href') | |
7a896817 | 210 | transfer_format = connection.get('transferFormat') |
2e3fd9ec S |
211 | # ASX playlist |
212 | if supplier == 'asx': | |
213 | for i, ref in enumerate(self._extract_asx_playlist(connection, programme_id)): | |
214 | formats.append({ | |
215 | 'url': ref, | |
216 | 'format_id': 'ref%s_%s' % (i, supplier), | |
217 | }) | |
7a896817 S |
218 | # Skip DASH until supported |
219 | elif transfer_format == 'dash': | |
220 | pass | |
d1c694ea S |
221 | elif transfer_format == 'hls': |
222 | m3u8_formats = self._extract_m3u8_formats( | |
223 | href, programme_id, ext='mp4', entry_protocol='m3u8_native', | |
224 | m3u8_id=supplier, fatal=False) | |
225 | if m3u8_formats: | |
226 | formats.extend(m3u8_formats) | |
2e3fd9ec S |
227 | # Direct link |
228 | else: | |
229 | formats.append({ | |
230 | 'url': href, | |
e6174ee9 | 231 | 'format_id': supplier or kind or protocol, |
2e3fd9ec S |
232 | }) |
233 | elif protocol == 'rtmp': | |
234 | application = connection.get('application', 'ondemand') | |
235 | auth_string = connection.get('authString') | |
236 | identifier = connection.get('identifier') | |
237 | server = connection.get('server') | |
238 | formats.append({ | |
239 | 'url': '%s://%s/%s?%s' % (protocol, server, application, auth_string), | |
240 | 'play_path': identifier, | |
241 | 'app': '%s?%s' % (application, auth_string), | |
242 | 'page_url': 'http://www.bbc.co.uk', | |
243 | 'player_url': 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf', | |
244 | 'rtmp_live': False, | |
245 | 'ext': 'flv', | |
246 | 'format_id': supplier, | |
247 | }) | |
248 | return formats | |
249 | ||
250 | def _extract_items(self, playlist): | |
e6174ee9 S |
251 | return playlist.findall('./{%s}item' % self._EMP_PLAYLIST_NS) |
252 | ||
253 | def _findall_ns(self, element, xpath): | |
254 | elements = [] | |
255 | for ns in self._NAMESPACES: | |
256 | elements.extend(element.findall(xpath % ns)) | |
257 | return elements | |
2e3fd9ec S |
258 | |
259 | def _extract_medias(self, media_selection): | |
e6174ee9 S |
260 | error = media_selection.find('./{%s}error' % self._MEDIASELECTION_NS) |
261 | if error is None: | |
262 | media_selection.find('./{%s}error' % self._EMP_PLAYLIST_NS) | |
c056efa2 | 263 | if error is not None: |
d12a1a47 | 264 | raise BBCCoUkIE.MediaSelectionError(error.get('id')) |
e6174ee9 | 265 | return self._findall_ns(media_selection, './{%s}media') |
2e3fd9ec S |
266 | |
267 | def _extract_connections(self, media): | |
e6174ee9 | 268 | return self._findall_ns(media, './{%s}connection') |
2e3fd9ec S |
269 | |
270 | def _extract_video(self, media, programme_id): | |
271 | formats = [] | |
d97f5cd7 | 272 | vbr = int_or_none(media.get('bitrate')) |
2e3fd9ec S |
273 | vcodec = media.get('encoding') |
274 | service = media.get('service') | |
d97f5cd7 S |
275 | width = int_or_none(media.get('width')) |
276 | height = int_or_none(media.get('height')) | |
277 | file_size = int_or_none(media.get('media_file_size')) | |
2e3fd9ec S |
278 | for connection in self._extract_connections(media): |
279 | conn_formats = self._extract_connection(connection, programme_id) | |
280 | for format in conn_formats: | |
281 | format.update({ | |
2e3fd9ec S |
282 | 'width': width, |
283 | 'height': height, | |
284 | 'vbr': vbr, | |
285 | 'vcodec': vcodec, | |
286 | 'filesize': file_size, | |
287 | }) | |
e6174ee9 S |
288 | if service: |
289 | format['format_id'] = '%s_%s' % (service, format['format_id']) | |
2e3fd9ec S |
290 | formats.extend(conn_formats) |
291 | return formats | |
292 | ||
293 | def _extract_audio(self, media, programme_id): | |
294 | formats = [] | |
d97f5cd7 | 295 | abr = int_or_none(media.get('bitrate')) |
2e3fd9ec S |
296 | acodec = media.get('encoding') |
297 | service = media.get('service') | |
298 | for connection in self._extract_connections(media): | |
299 | conn_formats = self._extract_connection(connection, programme_id) | |
300 | for format in conn_formats: | |
301 | format.update({ | |
302 | 'format_id': '%s_%s' % (service, format['format_id']), | |
303 | 'abr': abr, | |
304 | 'acodec': acodec, | |
305 | }) | |
306 | formats.extend(conn_formats) | |
307 | return formats | |
308 | ||
f13b1e7d | 309 | def _get_subtitles(self, media, programme_id): |
2e3fd9ec S |
310 | subtitles = {} |
311 | for connection in self._extract_connections(media): | |
312 | captions = self._download_xml(connection.get('href'), programme_id, 'Downloading captions') | |
313 | lang = captions.get('{http://www.w3.org/XML/1998/namespace}lang', 'en') | |
f13b1e7d JMF |
314 | subtitles[lang] = [ |
315 | { | |
316 | 'url': connection.get('href'), | |
317 | 'ext': 'ttml', | |
318 | }, | |
f13b1e7d | 319 | ] |
2e3fd9ec | 320 | return subtitles |
082c6c86 | 321 | |
d12a1a47 S |
322 | def _raise_extractor_error(self, media_selection_error): |
323 | raise ExtractorError( | |
324 | '%s returned error: %s' % (self.IE_NAME, media_selection_error.id), | |
325 | expected=True) | |
326 | ||
c056efa2 | 327 | def _download_media_selector(self, programme_id): |
d12a1a47 S |
328 | last_exception = None |
329 | for mediaselector_url in self._MEDIASELECTOR_URLS: | |
330 | try: | |
331 | return self._download_media_selector_url( | |
332 | mediaselector_url % programme_id, programme_id) | |
333 | except BBCCoUkIE.MediaSelectionError as e: | |
ee3d5a6d | 334 | if e.id in ('notukerror', 'geolocation'): |
d12a1a47 S |
335 | last_exception = e |
336 | continue | |
337 | self._raise_extractor_error(e) | |
338 | self._raise_extractor_error(last_exception) | |
9afa1770 S |
339 | |
340 | def _download_media_selector_url(self, url, programme_id=None): | |
c056efa2 S |
341 | try: |
342 | media_selection = self._download_xml( | |
9afa1770 | 343 | url, programme_id, 'Downloading media selection XML') |
c056efa2 S |
344 | except ExtractorError as ee: |
345 | if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403: | |
974a6146 | 346 | media_selection = xml.etree.ElementTree.fromstring(ee.cause.read().decode('utf-8')) |
2e3fd9ec | 347 | else: |
c056efa2 | 348 | raise |
9afa1770 | 349 | return self._process_media_selector(media_selection, programme_id) |
082c6c86 | 350 | |
9afa1770 | 351 | def _process_media_selector(self, media_selection, programme_id): |
082c6c86 | 352 | formats = [] |
2e3fd9ec S |
353 | subtitles = None |
354 | ||
c056efa2 S |
355 | for media in self._extract_medias(media_selection): |
356 | kind = media.get('kind') | |
357 | if kind == 'audio': | |
358 | formats.extend(self._extract_audio(media, programme_id)) | |
359 | elif kind == 'video': | |
360 | formats.extend(self._extract_video(media, programme_id)) | |
361 | elif kind == 'captions': | |
f13b1e7d | 362 | subtitles = self.extract_subtitles(media, programme_id) |
c056efa2 | 363 | return formats, subtitles |
2e3fd9ec | 364 | |
ae6986fb S |
365 | def _download_playlist(self, playlist_id): |
366 | try: | |
367 | playlist = self._download_json( | |
368 | 'http://www.bbc.co.uk/programmes/%s/playlist.json' % playlist_id, | |
369 | playlist_id, 'Downloading playlist JSON') | |
370 | ||
371 | version = playlist.get('defaultAvailableVersion') | |
372 | if version: | |
373 | smp_config = version['smpConfig'] | |
374 | title = smp_config['title'] | |
375 | description = smp_config['summary'] | |
376 | for item in smp_config['items']: | |
377 | kind = item['kind'] | |
378 | if kind != 'programme' and kind != 'radioProgramme': | |
379 | continue | |
380 | programme_id = item.get('vpid') | |
d97f5cd7 | 381 | duration = int_or_none(item.get('duration')) |
ae6986fb S |
382 | formats, subtitles = self._download_media_selector(programme_id) |
383 | return programme_id, title, description, duration, formats, subtitles | |
384 | except ExtractorError as ee: | |
f813928e | 385 | if not (isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404): |
ae6986fb S |
386 | raise |
387 | ||
388 | # fallback to legacy playlist | |
9afa1770 S |
389 | return self._process_legacy_playlist(playlist_id) |
390 | ||
391 | def _process_legacy_playlist_url(self, url, display_id): | |
392 | playlist = self._download_legacy_playlist_url(url, display_id) | |
393 | return self._extract_from_legacy_playlist(playlist, display_id) | |
394 | ||
395 | def _process_legacy_playlist(self, playlist_id): | |
396 | return self._process_legacy_playlist_url( | |
397 | 'http://www.bbc.co.uk/iplayer/playlist/%s' % playlist_id, playlist_id) | |
398 | ||
399 | def _download_legacy_playlist_url(self, url, playlist_id=None): | |
400 | return self._download_xml( | |
401 | url, playlist_id, 'Downloading legacy playlist XML') | |
ae6986fb | 402 | |
9afa1770 | 403 | def _extract_from_legacy_playlist(self, playlist, playlist_id): |
e6174ee9 | 404 | no_items = playlist.find('./{%s}noItems' % self._EMP_PLAYLIST_NS) |
ae6986fb S |
405 | if no_items is not None: |
406 | reason = no_items.get('reason') | |
407 | if reason == 'preAvailability': | |
408 | msg = 'Episode %s is not yet available' % playlist_id | |
409 | elif reason == 'postAvailability': | |
410 | msg = 'Episode %s is no longer available' % playlist_id | |
411 | elif reason == 'noMedia': | |
412 | msg = 'Episode %s is not currently available' % playlist_id | |
413 | else: | |
414 | msg = 'Episode %s is not available: %s' % (playlist_id, reason) | |
415 | raise ExtractorError(msg, expected=True) | |
416 | ||
417 | for item in self._extract_items(playlist): | |
418 | kind = item.get('kind') | |
419 | if kind != 'programme' and kind != 'radioProgramme': | |
420 | continue | |
e6174ee9 S |
421 | title = playlist.find('./{%s}title' % self._EMP_PLAYLIST_NS).text |
422 | description_el = playlist.find('./{%s}summary' % self._EMP_PLAYLIST_NS) | |
87cc0fbd | 423 | description = description_el.text if description_el else None |
9afa1770 S |
424 | |
425 | def get_programme_id(item): | |
426 | def get_from_attributes(item): | |
427 | for p in('identifier', 'group'): | |
428 | value = item.get(p) | |
429 | if value and re.match(r'^[pb][\da-z]{7}$', value): | |
430 | return value | |
431 | get_from_attributes(item) | |
e6174ee9 | 432 | mediator = item.find('./{%s}mediator' % self._EMP_PLAYLIST_NS) |
9afa1770 S |
433 | if mediator is not None: |
434 | return get_from_attributes(mediator) | |
435 | ||
436 | programme_id = get_programme_id(item) | |
d97f5cd7 | 437 | duration = int_or_none(item.get('duration')) |
e6174ee9 S |
438 | |
439 | if programme_id: | |
440 | formats, subtitles = self._download_media_selector(programme_id) | |
441 | else: | |
442 | formats, subtitles = self._process_media_selector(item, playlist_id) | |
443 | programme_id = playlist_id | |
ae6986fb S |
444 | |
445 | return programme_id, title, description, duration, formats, subtitles | |
446 | ||
c056efa2 S |
447 | def _real_extract(self, url): |
448 | group_id = self._match_id(url) | |
449 | ||
450 | webpage = self._download_webpage(url, group_id, 'Downloading video page') | |
451 | ||
8683b4d8 S |
452 | programme_id = None |
453 | ||
454 | tviplayer = self._search_regex( | |
455 | r'mediator\.bind\(({.+?})\s*,\s*document\.getElementById', | |
456 | webpage, 'player', default=None) | |
457 | ||
458 | if tviplayer: | |
459 | player = self._parse_json(tviplayer, group_id).get('player', {}) | |
460 | duration = int_or_none(player.get('duration')) | |
461 | programme_id = player.get('vpid') | |
462 | ||
463 | if not programme_id: | |
464 | programme_id = self._search_regex( | |
465 | r'"vpid"\s*:\s*"([\da-z]{8})"', webpage, 'vpid', fatal=False, default=None) | |
466 | ||
c056efa2 | 467 | if programme_id: |
c056efa2 | 468 | formats, subtitles = self._download_media_selector(programme_id) |
8683b4d8 S |
469 | title = self._og_search_title(webpage) |
470 | description = self._search_regex( | |
25fa8d66 | 471 | r'<p class="[^"]*medium-description[^"]*">([^<]+)</p>', |
8683b4d8 | 472 | webpage, 'description', fatal=False) |
c056efa2 | 473 | else: |
ae6986fb | 474 | programme_id, title, description, duration, formats, subtitles = self._download_playlist(group_id) |
2e3fd9ec | 475 | |
082c6c86 S |
476 | self._sort_formats(formats) |
477 | ||
478 | return { | |
2e3fd9ec | 479 | 'id': programme_id, |
082c6c86 S |
480 | 'title': title, |
481 | 'description': description, | |
650cfd0c | 482 | 'thumbnail': self._og_search_thumbnail(webpage, default=None), |
082c6c86 S |
483 | 'duration': duration, |
484 | 'formats': formats, | |
2e3fd9ec | 485 | 'subtitles': subtitles, |
5f6a1245 | 486 | } |
10273d6e | 487 | |
488 | ||
9afa1770 S |
489 | class BBCIE(BBCCoUkIE): |
490 | IE_NAME = 'bbc' | |
491 | IE_DESC = 'BBC' | |
492 | _VALID_URL = r'https?://(?:www\.)?bbc\.(?:com|co\.uk)/(?:[^/]+/)+(?P<id>[^/#?]+)' | |
10273d6e | 493 | |
d12a1a47 S |
494 | _MEDIASELECTOR_URLS = [ |
495 | # Provides more formats, namely direct mp4 links, but fails on some videos with | |
496 | # notukerror for non UK (?) users (e.g. | |
497 | # http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret) | |
498 | 'http://open.live.bbc.co.uk/mediaselector/4/mtis/stream/%s', | |
499 | # Provides fewer formats, but works everywhere for everybody (hopefully) | |
500 | 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/journalism-pc/vpid/%s', | |
501 | ] | |
10273d6e | 502 | |
503 | _TESTS = [{ | |
9afa1770 S |
504 | # article with multiple videos embedded with data-media-meta containing |
505 | # playlist.sxml, externalId and no direct video links | |
10273d6e | 506 | 'url': 'http://www.bbc.com/news/world-europe-32668511', |
507 | 'info_dict': { | |
508 | 'id': 'world-europe-32668511', | |
509 | 'title': 'Russia stages massive WW2 parade despite Western boycott', | |
9afa1770 | 510 | 'description': 'md5:00ff61976f6081841f759a08bf78cc9c', |
10273d6e | 511 | }, |
512 | 'playlist_count': 2, | |
a3bfddfa | 513 | }, { |
9afa1770 | 514 | # article with multiple videos embedded with data-media-meta (more videos) |
10273d6e | 515 | 'url': 'http://www.bbc.com/news/business-28299555', |
516 | 'info_dict': { | |
517 | 'id': 'business-28299555', | |
518 | 'title': 'Farnborough Airshow: Video highlights', | |
9afa1770 | 519 | 'description': 'BBC reports and video highlights at the Farnborough Airshow.', |
10273d6e | 520 | }, |
521 | 'playlist_count': 9, | |
9afa1770 | 522 | 'skip': 'Save time', |
88ed52ae S |
523 | }, { |
524 | # article with multiple videos embedded with `new SMP()` | |
525 | 'url': 'http://www.bbc.co.uk/blogs/adamcurtis/entries/3662a707-0af9-3149-963f-47bea720b460', | |
526 | 'info_dict': { | |
527 | 'id': '3662a707-0af9-3149-963f-47bea720b460', | |
528 | 'title': 'BBC Blogs - Adam Curtis - BUGGER', | |
529 | }, | |
530 | 'playlist_count': 18, | |
a3bfddfa | 531 | }, { |
9afa1770 | 532 | # single video embedded with mediaAssetPage.init() |
10273d6e | 533 | 'url': 'http://www.bbc.com/news/world-europe-32041533', |
10273d6e | 534 | 'info_dict': { |
535 | 'id': 'p02mprgb', | |
cb23bcba | 536 | 'ext': 'mp4', |
10273d6e | 537 | 'title': 'Aerial footage showed the site of the crash in the Alps - courtesy BFM TV', |
10273d6e | 538 | 'duration': 47, |
9afa1770 | 539 | 'timestamp': 1427219242, |
da92eeae | 540 | 'upload_date': '20150324', |
10273d6e | 541 | }, |
542 | 'params': { | |
9afa1770 | 543 | # rtmp download |
10273d6e | 544 | 'skip_download': True, |
545 | } | |
a3bfddfa | 546 | }, { |
9afa1770 S |
547 | # article with single video embedded with data-media-meta containing |
548 | # direct video links (for now these are extracted) and playlist.xml (with | |
549 | # media items as f4m and m3u8 - currently unsupported) | |
de939d89 | 550 | 'url': 'http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu', |
de939d89 | 551 | 'info_dict': { |
9afa1770 | 552 | 'id': '150615_telabyad_kentin_cogu', |
de939d89 | 553 | 'ext': 'mp4', |
9afa1770 | 554 | 'title': "YPG: Tel Abyad'ın tamamı kontrolümüzde", |
de939d89 | 555 | 'duration': 47, |
9afa1770 | 556 | 'timestamp': 1434397334, |
da92eeae | 557 | 'upload_date': '20150615', |
de939d89 | 558 | }, |
559 | 'params': { | |
560 | 'skip_download': True, | |
561 | } | |
c936d8cc S |
562 | }, { |
563 | # single video embedded with playlist.sxml in data-playable | |
564 | 'url': 'http://www.bbc.com/turkce/multimedya/2015/10/151010_vid_ankara_patlama_ani', | |
565 | 'info_dict': { | |
566 | 'id': '151010_vid_ankara_patlama_ani', | |
567 | 'ext': 'mp4', | |
568 | 'title': "Ankara'da patlama anı", | |
569 | 'timestamp': 1444480325, | |
570 | 'upload_date': '20151010', | |
571 | }, | |
572 | 'params': { | |
573 | 'skip_download': True, | |
574 | } | |
a3bfddfa | 575 | }, { |
9afa1770 | 576 | # single video embedded with mediaAssetPage.init() (regional section) |
de939d89 | 577 | 'url': 'http://www.bbc.com/mundo/video_fotos/2015/06/150619_video_honduras_militares_hospitales_corrupcion_aw', |
de939d89 | 578 | 'info_dict': { |
9afa1770 | 579 | 'id': '150619_video_honduras_militares_hospitales_corrupcion_aw', |
de939d89 | 580 | 'ext': 'mp4', |
9afa1770 | 581 | 'title': 'Honduras militariza sus hospitales por nuevo escándalo de corrupción', |
de939d89 | 582 | 'duration': 87, |
9afa1770 | 583 | 'timestamp': 1434713142, |
da92eeae | 584 | 'upload_date': '20150619', |
de939d89 | 585 | }, |
586 | 'params': { | |
587 | 'skip_download': True, | |
588 | } | |
a346b1ff S |
589 | }, { |
590 | # single video from video playlist embedded with vxp-playlist-data JSON | |
591 | 'url': 'http://www.bbc.com/news/video_and_audio/must_see/33376376', | |
592 | 'info_dict': { | |
593 | 'id': 'p02w6qjc', | |
594 | 'ext': 'mp4', | |
595 | 'title': '''Judge Mindy Glazer: "I'm sorry to see you here... I always wondered what happened to you"''', | |
596 | 'duration': 56, | |
597 | }, | |
598 | 'params': { | |
599 | 'skip_download': True, | |
600 | } | |
9afa1770 S |
601 | }, { |
602 | # single video story with digitalData | |
603 | 'url': 'http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret', | |
604 | 'info_dict': { | |
605 | 'id': 'p02q6gc4', | |
606 | 'ext': 'flv', | |
607 | 'title': 'Sri Lanka’s spicy secret', | |
608 | 'description': 'As a new train line to Jaffna opens up the country’s north, travellers can experience a truly distinct slice of Tamil culture.', | |
609 | 'timestamp': 1437674293, | |
610 | 'upload_date': '20150723', | |
611 | }, | |
612 | 'params': { | |
613 | # rtmp download | |
614 | 'skip_download': True, | |
615 | } | |
616 | }, { | |
617 | # single video story without digitalData | |
618 | 'url': 'http://www.bbc.com/autos/story/20130513-hyundais-rock-star', | |
619 | 'info_dict': { | |
620 | 'id': 'p018zqqg', | |
cb23bcba | 621 | 'ext': 'mp4', |
9afa1770 S |
622 | 'title': 'Hyundai Santa Fe Sport: Rock star', |
623 | 'description': 'md5:b042a26142c4154a6e472933cf20793d', | |
624 | 'timestamp': 1368473503, | |
625 | 'upload_date': '20130513', | |
626 | }, | |
627 | 'params': { | |
628 | # rtmp download | |
629 | 'skip_download': True, | |
630 | } | |
631 | }, { | |
632 | # single video with playlist.sxml URL | |
633 | 'url': 'http://www.bbc.com/sport/0/football/33653409', | |
634 | 'info_dict': { | |
635 | 'id': 'p02xycnp', | |
cb23bcba | 636 | 'ext': 'mp4', |
9afa1770 S |
637 | 'title': 'Transfers: Cristiano Ronaldo to Man Utd, Arsenal to spend?', |
638 | 'description': 'md5:398fca0e2e701c609d726e034fa1fc89', | |
639 | 'duration': 140, | |
640 | }, | |
641 | 'params': { | |
642 | # rtmp download | |
643 | 'skip_download': True, | |
644 | } | |
b5d48cb1 S |
645 | }, { |
646 | # article with multiple videos embedded with playlist.sxml | |
647 | 'url': 'http://www.bbc.com/sport/0/football/34475836', | |
648 | 'info_dict': { | |
649 | 'id': '34475836', | |
650 | 'title': 'What Liverpool can expect from Klopp', | |
651 | }, | |
652 | 'playlist_count': 3, | |
9afa1770 S |
653 | }, { |
654 | # single video with playlist URL from weather section | |
655 | 'url': 'http://www.bbc.com/weather/features/33601775', | |
656 | 'only_matching': True, | |
657 | }, { | |
658 | # custom redirection to www.bbc.com | |
659 | 'url': 'http://www.bbc.co.uk/news/science-environment-33661876', | |
660 | 'only_matching': True, | |
10273d6e | 661 | }] |
662 | ||
9afa1770 S |
663 | @classmethod |
664 | def suitable(cls, url): | |
665 | return False if BBCCoUkIE.suitable(url) else super(BBCIE, cls).suitable(url) | |
666 | ||
667 | def _extract_from_media_meta(self, media_meta, video_id): | |
668 | # Direct links to media in media metadata (e.g. | |
669 | # http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu) | |
670 | # TODO: there are also f4m and m3u8 streams incorporated in playlist.sxml | |
671 | source_files = media_meta.get('sourceFiles') | |
672 | if source_files: | |
673 | return [{ | |
674 | 'url': f['url'], | |
675 | 'format_id': format_id, | |
676 | 'ext': f.get('encoding'), | |
677 | 'tbr': float_or_none(f.get('bitrate'), 1000), | |
678 | 'filesize': int_or_none(f.get('filesize')), | |
679 | } for format_id, f in source_files.items() if f.get('url')], [] | |
680 | ||
681 | programme_id = media_meta.get('externalId') | |
682 | if programme_id: | |
683 | return self._download_media_selector(programme_id) | |
684 | ||
685 | # Process playlist.sxml as legacy playlist | |
686 | href = media_meta.get('href') | |
687 | if href: | |
688 | playlist = self._download_legacy_playlist_url(href) | |
689 | _, _, _, _, formats, subtitles = self._extract_from_legacy_playlist(playlist, video_id) | |
690 | return formats, subtitles | |
691 | ||
692 | return [], [] | |
693 | ||
baf39a1a S |
694 | def _extract_from_playlist_sxml(self, url, playlist_id, timestamp): |
695 | programme_id, title, description, duration, formats, subtitles = \ | |
696 | self._process_legacy_playlist_url(url, playlist_id) | |
697 | self._sort_formats(formats) | |
698 | return { | |
699 | 'id': programme_id, | |
700 | 'title': title, | |
701 | 'description': description, | |
702 | 'duration': duration, | |
703 | 'timestamp': timestamp, | |
704 | 'formats': formats, | |
705 | 'subtitles': subtitles, | |
706 | } | |
707 | ||
10273d6e | 708 | def _real_extract(self, url): |
9afa1770 S |
709 | playlist_id = self._match_id(url) |
710 | ||
711 | webpage = self._download_webpage(url, playlist_id) | |
712 | ||
713 | timestamp = parse_iso8601(self._search_regex( | |
714 | [r'"datePublished":\s*"([^"]+)', | |
715 | r'<meta[^>]+property="article:published_time"[^>]+content="([^"]+)"', | |
716 | r'itemprop="datePublished"[^>]+datetime="([^"]+)"'], | |
717 | webpage, 'date', default=None)) | |
718 | ||
de665713 S |
719 | # article with multiple videos embedded with playlist.sxml (e.g. |
720 | # http://www.bbc.com/sport/0/football/34475836) | |
721 | playlists = re.findall(r'<param[^>]+name="playlist"[^>]+value="([^"]+)"', webpage) | |
722 | if playlists: | |
baf39a1a S |
723 | entries = [ |
724 | self._extract_from_playlist_sxml(playlist_url, playlist_id, timestamp) | |
725 | for playlist_url in playlists] | |
de665713 | 726 | playlist_title = self._og_search_title(webpage) |
97597786 | 727 | playlist_description = self._og_search_description(webpage, default=None) |
de665713 | 728 | return self.playlist_result(entries, playlist_id, playlist_title, playlist_description) |
de939d89 | 729 | |
9afa1770 S |
730 | # single video story (e.g. http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret) |
731 | programme_id = self._search_regex( | |
732 | [r'data-video-player-vpid="([\da-z]{8})"', | |
733 | r'<param[^>]+name="externalIdentifier"[^>]+value="([\da-z]{8})"'], | |
734 | webpage, 'vpid', default=None) | |
dab062fb S |
735 | |
736 | duration = None | |
737 | if not programme_id: | |
738 | # single video in news article embedded with data-playable (e.g. | |
739 | # http://www.bbc.com/news/world-us-canada-34473351) | |
740 | data_playable = self._parse_json( | |
741 | unescapeHTML(self._search_regex( | |
b1ec70e4 S |
742 | r'data-playable=(["\'])(?P<json>{.+?})\1', webpage, |
743 | 'data playable', default='{}', group='json')), | |
dab062fb S |
744 | programme_id, fatal=False) |
745 | if data_playable: | |
baf39a1a S |
746 | # data-playable has video vpid in settings.playlistObject.items (e.g. |
747 | # http://www.bbc.com/news/world-us-canada-34473351) | |
748 | settings = data_playable.get('settings', {}) | |
749 | if settings: | |
750 | playlist_object = settings.get('playlistObject', {}) | |
751 | if playlist_object: | |
752 | items = playlist_object.get('items') | |
753 | if items and isinstance(items, list): | |
754 | duration = int_or_none(items[0].get('duration')) | |
755 | programme_id = items[0].get('vpid') | |
756 | if not programme_id: | |
757 | # data-playable has no vpid but has a playlist.sxml URLs | |
758 | # in otherSettings.playlist (e.g. | |
759 | # http://www.bbc.com/turkce/multimedya/2015/10/151010_vid_ankara_patlama_ani) | |
760 | playlist = data_playable.get('otherSettings', {}).get('playlist', {}) | |
761 | if playlist: | |
762 | return self._extract_from_playlist_sxml( | |
763 | playlist.get('progressiveDownloadUrl'), playlist_id, timestamp) | |
dab062fb | 764 | |
9afa1770 S |
765 | if programme_id: |
766 | formats, subtitles = self._download_media_selector(programme_id) | |
767 | self._sort_formats(formats) | |
768 | # digitalData may be missing (e.g. http://www.bbc.com/autos/story/20130513-hyundais-rock-star) | |
769 | digital_data = self._parse_json( | |
770 | self._search_regex( | |
771 | r'var\s+digitalData\s*=\s*({.+?});?\n', webpage, 'digital data', default='{}'), | |
772 | programme_id, fatal=False) | |
773 | page_info = digital_data.get('page', {}).get('pageInfo', {}) | |
774 | title = page_info.get('pageName') or self._og_search_title(webpage) | |
775 | description = page_info.get('description') or self._og_search_description(webpage) | |
776 | timestamp = parse_iso8601(page_info.get('publicationDate')) or timestamp | |
777 | return { | |
778 | 'id': programme_id, | |
779 | 'title': title, | |
780 | 'description': description, | |
781 | 'timestamp': timestamp, | |
dab062fb | 782 | 'duration': duration, |
9afa1770 S |
783 | 'formats': formats, |
784 | 'subtitles': subtitles, | |
785 | } | |
a3bfddfa | 786 | |
9afa1770 S |
787 | playlist_title = self._html_search_regex( |
788 | r'<title>(.*?)(?:\s*-\s*BBC [^ ]+)?</title>', webpage, 'playlist title') | |
88ed52ae S |
789 | playlist_description = self._og_search_description(webpage, default=None) |
790 | ||
791 | def extract_all(pattern): | |
792 | return list(filter(None, map( | |
793 | lambda s: self._parse_json(s, playlist_id, fatal=False), | |
794 | re.findall(pattern, webpage)))) | |
795 | ||
796 | # Multiple video article (e.g. | |
797 | # http://www.bbc.co.uk/blogs/adamcurtis/entries/3662a707-0af9-3149-963f-47bea720b460) | |
d247a2c8 | 798 | EMBED_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:[^/]+/)+[\da-z]{8}(?:\b[^"]+)?' |
88ed52ae S |
799 | entries = [] |
800 | for match in extract_all(r'new\s+SMP\(({.+?})\)'): | |
801 | embed_url = match.get('playerSettings', {}).get('externalEmbedUrl') | |
802 | if embed_url and re.match(EMBED_URL, embed_url): | |
803 | entries.append(embed_url) | |
804 | entries.extend(re.findall( | |
805 | r'setPlaylist\("(%s)"\)' % EMBED_URL, webpage)) | |
806 | if entries: | |
807 | return self.playlist_result( | |
808 | [self.url_result(entry, 'BBCCoUk') for entry in entries], | |
809 | playlist_id, playlist_title, playlist_description) | |
9afa1770 S |
810 | |
811 | # Multiple video article (e.g. http://www.bbc.com/news/world-europe-32668511) | |
88ed52ae | 812 | medias = extract_all(r"data-media-meta='({[^']+})'") |
9afa1770 S |
813 | |
814 | if not medias: | |
815 | # Single video article (e.g. http://www.bbc.com/news/video_and_audio/international) | |
a346b1ff S |
816 | media_asset = self._search_regex( |
817 | r'mediaAssetPage\.init\(\s*({.+?}), "/', | |
818 | webpage, 'media asset', default=None) | |
819 | if media_asset: | |
820 | media_asset_page = self._parse_json(media_asset, playlist_id, fatal=False) | |
821 | medias = [] | |
822 | for video in media_asset_page.get('videos', {}).values(): | |
823 | medias.extend(video.values()) | |
824 | ||
825 | if not medias: | |
826 | # Multiple video playlist with single `now playing` entry (e.g. | |
827 | # http://www.bbc.com/news/video_and_audio/must_see/33767813) | |
828 | vxp_playlist = self._parse_json( | |
9afa1770 | 829 | self._search_regex( |
a346b1ff S |
830 | r'<script[^>]+class="vxp-playlist-data"[^>]+type="application/json"[^>]*>([^<]+)</script>', |
831 | webpage, 'playlist data'), | |
9afa1770 | 832 | playlist_id) |
a346b1ff S |
833 | playlist_medias = [] |
834 | for item in vxp_playlist: | |
835 | media = item.get('media') | |
836 | if not media: | |
837 | continue | |
838 | playlist_medias.append(media) | |
839 | # Download single video if found media with asset id matching the video id from URL | |
840 | if item.get('advert', {}).get('assetId') == playlist_id: | |
841 | medias = [media] | |
842 | break | |
843 | # Fallback to the whole playlist | |
844 | if not medias: | |
845 | medias = playlist_medias | |
9afa1770 S |
846 | |
847 | entries = [] | |
848 | for num, media_meta in enumerate(medias, start=1): | |
849 | formats, subtitles = self._extract_from_media_meta(media_meta, playlist_id) | |
850 | if not formats: | |
851 | continue | |
10273d6e | 852 | self._sort_formats(formats) |
853 | ||
9afa1770 S |
854 | video_id = media_meta.get('externalId') |
855 | if not video_id: | |
856 | video_id = playlist_id if len(medias) == 1 else '%s-%s' % (playlist_id, num) | |
857 | ||
858 | title = media_meta.get('caption') | |
859 | if not title: | |
860 | title = playlist_title if len(medias) == 1 else '%s - Video %s' % (playlist_title, num) | |
861 | ||
862 | duration = int_or_none(media_meta.get('durationInSeconds')) or parse_duration(media_meta.get('duration')) | |
da92eeae | 863 | |
9afa1770 S |
864 | images = [] |
865 | for image in media_meta.get('images', {}).values(): | |
866 | images.extend(image.values()) | |
867 | if 'image' in media_meta: | |
868 | images.append(media_meta['image']) | |
869 | ||
870 | thumbnails = [{ | |
871 | 'url': image.get('href'), | |
872 | 'width': int_or_none(image.get('width')), | |
873 | 'height': int_or_none(image.get('height')), | |
874 | } for image in images] | |
875 | ||
876 | entries.append({ | |
877 | 'id': video_id, | |
10273d6e | 878 | 'title': title, |
9afa1770 | 879 | 'thumbnails': thumbnails, |
10273d6e | 880 | 'duration': duration, |
9afa1770 | 881 | 'timestamp': timestamp, |
10273d6e | 882 | 'formats': formats, |
883 | 'subtitles': subtitles, | |
a3bfddfa | 884 | }) |
10273d6e | 885 | |
9afa1770 | 886 | return self.playlist_result(entries, playlist_id, playlist_title, playlist_description) |