1 from __future__ import unicode_literals
3 import xml.etree.ElementTree
5 from .subtitles import SubtitlesInfoExtractor
6 from ..utils import ExtractorError
7 from ..compat import compat_HTTPError
10 class BBCCoUkIE(SubtitlesInfoExtractor):
12 IE_DESC = 'BBC iPlayer'
13 _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:programmes|iplayer/episode)/(?P<id>[\da-z]{8})'
17 'url': 'http://www.bbc.co.uk/programmes/b039g8p7',
21 'title': 'Kaleidoscope: Leonard Cohen',
22 'description': 'md5:db4755d7a665ae72343779f7dacb402c',
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',
77 def _extract_asx_playlist(self, connection, programme_id):
78 asx = self._download_xml(connection.get('href'), programme_id, 'Downloading ASX playlist')
79 return [ref.get('href') for ref in asx.findall('./Entry/ref')]
81 def _extract_connection(self, connection, programme_id):
83 protocol = connection.get('protocol')
84 supplier = connection.get('supplier')
85 if protocol == 'http':
86 href = connection.get('href')
89 for i, ref in enumerate(self._extract_asx_playlist(connection, programme_id)):
92 'format_id': 'ref%s_%s' % (i, supplier),
98 'format_id': supplier,
100 elif protocol == 'rtmp':
101 application = connection.get('application', 'ondemand')
102 auth_string = connection.get('authString')
103 identifier = connection.get('identifier')
104 server = connection.get('server')
106 'url': '%s://%s/%s?%s' % (protocol, server, application, auth_string),
107 'play_path': identifier,
108 'app': '%s?%s' % (application, auth_string),
109 'page_url': 'http://www.bbc.co.uk',
110 'player_url': 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf',
113 'format_id': supplier,
117 def _extract_items(self, playlist):
118 return playlist.findall('./{http://bbc.co.uk/2008/emp/playlist}item')
120 def _extract_medias(self, media_selection):
121 error = media_selection.find('./{http://bbc.co.uk/2008/mp/mediaselection}error')
122 if error is not None:
123 raise ExtractorError(
124 '%s returned error: %s' % (self.IE_NAME, error.get('id')), expected=True)
125 return media_selection.findall('./{http://bbc.co.uk/2008/mp/mediaselection}media')
127 def _extract_connections(self, media):
128 return media.findall('./{http://bbc.co.uk/2008/mp/mediaselection}connection')
130 def _extract_video(self, media, programme_id):
132 vbr = int(media.get('bitrate'))
133 vcodec = media.get('encoding')
134 service = media.get('service')
135 width = int(media.get('width'))
136 height = int(media.get('height'))
137 file_size = int(media.get('media_file_size'))
138 for connection in self._extract_connections(media):
139 conn_formats = self._extract_connection(connection, programme_id)
140 for format in conn_formats:
142 'format_id': '%s_%s' % (service, format['format_id']),
147 'filesize': file_size,
149 formats.extend(conn_formats)
152 def _extract_audio(self, media, programme_id):
154 abr = int(media.get('bitrate'))
155 acodec = media.get('encoding')
156 service = media.get('service')
157 for connection in self._extract_connections(media):
158 conn_formats = self._extract_connection(connection, programme_id)
159 for format in conn_formats:
161 'format_id': '%s_%s' % (service, format['format_id']),
165 formats.extend(conn_formats)
168 def _extract_captions(self, media, programme_id):
170 for connection in self._extract_connections(media):
171 captions = self._download_xml(connection.get('href'), programme_id, 'Downloading captions')
172 lang = captions.get('{http://www.w3.org/XML/1998/namespace}lang', 'en')
173 ps = captions.findall('./{0}body/{0}div/{0}p'.format('{http://www.w3.org/2006/10/ttaf1}'))
175 for pos, p in enumerate(ps):
176 srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (str(pos), p.get('begin'), p.get('end'),
177 p.text.strip() if p.text is not None else '')
178 subtitles[lang] = srt
181 def _download_media_selector(self, programme_id):
183 media_selection = self._download_xml(
184 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/%s' % programme_id,
185 programme_id, 'Downloading media selection XML')
186 except ExtractorError as ee:
187 if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
188 media_selection = xml.etree.ElementTree.fromstring(ee.cause.read().encode('utf-8'))
195 for media in self._extract_medias(media_selection):
196 kind = media.get('kind')
198 formats.extend(self._extract_audio(media, programme_id))
199 elif kind == 'video':
200 formats.extend(self._extract_video(media, programme_id))
201 elif kind == 'captions':
202 subtitles = self._extract_captions(media, programme_id)
204 return formats, subtitles
206 def _real_extract(self, url):
207 group_id = self._match_id(url)
209 webpage = self._download_webpage(url, group_id, 'Downloading video page')
211 programme_id = self._search_regex(
212 r'"vpid"\s*:\s*"([\da-z]{8})"', webpage, 'vpid', fatal=False)
214 player = self._download_json(
215 'http://www.bbc.co.uk/iplayer/episode/%s.json' % group_id,
216 group_id)['jsConf']['player']
217 title = player['title']
218 description = player['subtitle']
219 duration = player['duration']
220 formats, subtitles = self._download_media_selector(programme_id)
222 playlist = self._download_xml(
223 'http://www.bbc.co.uk/iplayer/playlist/%s' % group_id,
224 group_id, 'Downloading playlist XML')
226 no_items = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}noItems')
227 if no_items is not None:
228 reason = no_items.get('reason')
229 if reason == 'preAvailability':
230 msg = 'Episode %s is not yet available' % group_id
231 elif reason == 'postAvailability':
232 msg = 'Episode %s is no longer available' % group_id
233 elif reason == 'noMedia':
234 msg = 'Episode %s is not currently available' % group_id
236 msg = 'Episode %s is not available: %s' % (group_id, reason)
237 raise ExtractorError(msg, expected=True)
239 for item in self._extract_items(playlist):
240 kind = item.get('kind')
241 if kind != 'programme' and kind != 'radioProgramme':
243 title = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}title').text
244 description = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}summary').text
245 programme_id = item.get('identifier')
246 duration = int(item.get('duration'))
247 formats, subtitles = self._download_media_selector(programme_id)
249 if self._downloader.params.get('listsubtitles', False):
250 self._list_available_subtitles(programme_id, subtitles)
253 self._sort_formats(formats)
258 'description': description,
259 'duration': duration,
261 'subtitles': subtitles,