1 from __future__ import unicode_literals
3 import xml.etree.ElementTree
5 from .common import InfoExtractor
6 from ..utils import ExtractorError
7 from ..compat import compat_HTTPError
10 class BBCCoUkIE(InfoExtractor):
12 IE_DESC = 'BBC iPlayer'
13 _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:(?:(?:programmes|iplayer(?:/[^/]+)?/(?:episode|playlist))/)|music/clips[/#])(?P<id>[\da-z]{8})'
17 'url': 'http://www.bbc.co.uk/programmes/b039g8p7',
21 'title': 'Kaleidoscope, Leonard Cohen',
22 'description': 'The Canadian poet and songwriter reflects on his musical career.',
27 'skip_download': True,
31 'url': 'http://www.bbc.co.uk/iplayer/episode/b00yng5w/The_Man_in_Black_Series_3_The_Printed_Name/',
35 'title': 'The Man in Black: Series 3: The Printed Name',
36 'description': "Mark Gatiss introduces Nicholas Pierpan's chilling tale of a writer's devilish pact with a mysterious man. Stars Ewan Bailey.",
41 'skip_download': True,
43 'skip': 'Episode is no longer available on BBC iPlayer Radio',
46 'url': 'http://www.bbc.co.uk/iplayer/episode/b03vhd1f/The_Voice_UK_Series_3_Blind_Auditions_5/',
50 'title': 'The Voice UK: Series 3: Blind Auditions 5',
51 '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.",
56 'skip_download': True,
58 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
61 'url': 'http://www.bbc.co.uk/iplayer/episode/p026c7jt/tomorrows-worlds-the-unearthly-history-of-science-fiction-2-invasion',
65 'title': "Tomorrow's Worlds: The Unearthly History of Science Fiction",
66 'description': '2. Invasion',
71 'skip_download': True,
73 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
75 'url': 'http://www.bbc.co.uk/programmes/b04v20dw',
79 'title': 'Pete Tong, The Essential New Tune Special',
80 'description': "Pete has a very special mix - all of 2014's Essential New Tunes!",
85 'skip_download': True,
88 'url': 'http://www.bbc.co.uk/music/clips/p02frcc3',
93 'title': 'Pete Tong, Past, Present and Future Special, Madeon - After Hours mix',
94 'description': 'French house superstar Madeon takes us out of the club and onto the after party.',
99 'skip_download': True,
102 'url': 'http://www.bbc.co.uk/music/clips/p025c0zz',
107 'title': 'Reading and Leeds Festival, 2014, Rae Morris - Closer (Live on BBC Three)',
108 'description': 'Rae Morris performs Closer for BBC Three at Reading 2014',
113 'skip_download': True,
116 'url': 'http://www.bbc.co.uk/iplayer/playlist/p01dvks4',
117 'only_matching': True,
119 'url': 'http://www.bbc.co.uk/music/clips#p02frcc3',
120 'only_matching': True,
122 'url': 'http://www.bbc.co.uk/iplayer/cbeebies/episode/b0480276/bing-14-atchoo',
123 'only_matching': True,
127 def _extract_asx_playlist(self, connection, programme_id):
128 asx = self._download_xml(connection.get('href'), programme_id, 'Downloading ASX playlist')
129 return [ref.get('href') for ref in asx.findall('./Entry/ref')]
131 def _extract_connection(self, connection, programme_id):
133 protocol = connection.get('protocol')
134 supplier = connection.get('supplier')
135 if protocol == 'http':
136 href = connection.get('href')
138 if supplier == 'asx':
139 for i, ref in enumerate(self._extract_asx_playlist(connection, programme_id)):
142 'format_id': 'ref%s_%s' % (i, supplier),
148 'format_id': supplier,
150 elif protocol == 'rtmp':
151 application = connection.get('application', 'ondemand')
152 auth_string = connection.get('authString')
153 identifier = connection.get('identifier')
154 server = connection.get('server')
156 'url': '%s://%s/%s?%s' % (protocol, server, application, auth_string),
157 'play_path': identifier,
158 'app': '%s?%s' % (application, auth_string),
159 'page_url': 'http://www.bbc.co.uk',
160 'player_url': 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf',
163 'format_id': supplier,
167 def _extract_items(self, playlist):
168 return playlist.findall('./{http://bbc.co.uk/2008/emp/playlist}item')
170 def _extract_medias(self, media_selection):
171 error = media_selection.find('./{http://bbc.co.uk/2008/mp/mediaselection}error')
172 if error is not None:
173 raise ExtractorError(
174 '%s returned error: %s' % (self.IE_NAME, error.get('id')), expected=True)
175 return media_selection.findall('./{http://bbc.co.uk/2008/mp/mediaselection}media')
177 def _extract_connections(self, media):
178 return media.findall('./{http://bbc.co.uk/2008/mp/mediaselection}connection')
180 def _extract_video(self, media, programme_id):
182 vbr = int(media.get('bitrate'))
183 vcodec = media.get('encoding')
184 service = media.get('service')
185 width = int(media.get('width'))
186 height = int(media.get('height'))
187 file_size = int(media.get('media_file_size'))
188 for connection in self._extract_connections(media):
189 conn_formats = self._extract_connection(connection, programme_id)
190 for format in conn_formats:
192 'format_id': '%s_%s' % (service, format['format_id']),
197 'filesize': file_size,
199 formats.extend(conn_formats)
202 def _extract_audio(self, media, programme_id):
204 abr = int(media.get('bitrate'))
205 acodec = media.get('encoding')
206 service = media.get('service')
207 for connection in self._extract_connections(media):
208 conn_formats = self._extract_connection(connection, programme_id)
209 for format in conn_formats:
211 'format_id': '%s_%s' % (service, format['format_id']),
215 formats.extend(conn_formats)
218 def _get_subtitles(self, media, programme_id):
220 for connection in self._extract_connections(media):
221 captions = self._download_xml(connection.get('href'), programme_id, 'Downloading captions')
222 lang = captions.get('{http://www.w3.org/XML/1998/namespace}lang', 'en')
223 ps = captions.findall('./{0}body/{0}div/{0}p'.format('{http://www.w3.org/2006/10/ttaf1}'))
226 def _extract_text(p):
227 if p.text is not None:
228 stripped_text = p.text.strip()
231 return ' '.join(span.text.strip() for span in p.findall('{http://www.w3.org/2006/10/ttaf1}span'))
232 for pos, p in enumerate(ps):
233 srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (str(pos), p.get('begin'), p.get('end'), _extract_text(p))
236 'url': connection.get('href'),
246 def _download_media_selector(self, programme_id):
248 media_selection = self._download_xml(
249 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/%s' % programme_id,
250 programme_id, 'Downloading media selection XML')
251 except ExtractorError as ee:
252 if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
253 media_selection = xml.etree.ElementTree.fromstring(ee.cause.read().encode('utf-8'))
260 for media in self._extract_medias(media_selection):
261 kind = media.get('kind')
263 formats.extend(self._extract_audio(media, programme_id))
264 elif kind == 'video':
265 formats.extend(self._extract_video(media, programme_id))
266 elif kind == 'captions':
267 subtitles = self.extract_subtitles(media, programme_id)
269 return formats, subtitles
271 def _download_playlist(self, playlist_id):
273 playlist = self._download_json(
274 'http://www.bbc.co.uk/programmes/%s/playlist.json' % playlist_id,
275 playlist_id, 'Downloading playlist JSON')
277 version = playlist.get('defaultAvailableVersion')
279 smp_config = version['smpConfig']
280 title = smp_config['title']
281 description = smp_config['summary']
282 for item in smp_config['items']:
284 if kind != 'programme' and kind != 'radioProgramme':
286 programme_id = item.get('vpid')
287 duration = int(item.get('duration'))
288 formats, subtitles = self._download_media_selector(programme_id)
289 return programme_id, title, description, duration, formats, subtitles
290 except ExtractorError as ee:
291 if not (isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404):
294 # fallback to legacy playlist
295 playlist = self._download_xml(
296 'http://www.bbc.co.uk/iplayer/playlist/%s' % playlist_id,
297 playlist_id, 'Downloading legacy playlist XML')
299 no_items = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}noItems')
300 if no_items is not None:
301 reason = no_items.get('reason')
302 if reason == 'preAvailability':
303 msg = 'Episode %s is not yet available' % playlist_id
304 elif reason == 'postAvailability':
305 msg = 'Episode %s is no longer available' % playlist_id
306 elif reason == 'noMedia':
307 msg = 'Episode %s is not currently available' % playlist_id
309 msg = 'Episode %s is not available: %s' % (playlist_id, reason)
310 raise ExtractorError(msg, expected=True)
312 for item in self._extract_items(playlist):
313 kind = item.get('kind')
314 if kind != 'programme' and kind != 'radioProgramme':
316 title = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}title').text
317 description = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}summary').text
318 programme_id = item.get('identifier')
319 duration = int(item.get('duration'))
320 formats, subtitles = self._download_media_selector(programme_id)
322 return programme_id, title, description, duration, formats, subtitles
324 def _real_extract(self, url):
325 group_id = self._match_id(url)
327 webpage = self._download_webpage(url, group_id, 'Downloading video page')
329 programme_id = self._search_regex(
330 r'"vpid"\s*:\s*"([\da-z]{8})"', webpage, 'vpid', fatal=False, default=None)
332 player = self._download_json(
333 'http://www.bbc.co.uk/iplayer/episode/%s.json' % group_id,
334 group_id)['jsConf']['player']
335 title = player['title']
336 description = player['subtitle']
337 duration = player['duration']
338 formats, subtitles = self._download_media_selector(programme_id)
340 programme_id, title, description, duration, formats, subtitles = self._download_playlist(group_id)
342 self._sort_formats(formats)
347 'description': description,
348 'duration': duration,
350 'subtitles': subtitles,