]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bbccouk.py
debian/changelog: Initiate a new release.
[youtubedl] / youtube_dl / extractor / bbccouk.py
1 from __future__ import unicode_literals
2
3 import xml.etree.ElementTree
4
5 from .common import InfoExtractor
6 from ..utils import (
7 ExtractorError,
8 int_or_none,
9 )
10 from ..compat import compat_HTTPError
11
12
13 class BBCCoUkIE(InfoExtractor):
14 IE_NAME = 'bbc.co.uk'
15 IE_DESC = 'BBC iPlayer'
16 _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:(?:(?:programmes|iplayer(?:/[^/]+)?/(?:episode|playlist))/)|music/clips[/#])(?P<id>[\da-z]{8})'
17
18 _TESTS = [
19 {
20 'url': 'http://www.bbc.co.uk/programmes/b039g8p7',
21 'info_dict': {
22 'id': 'b039d07m',
23 'ext': 'flv',
24 'title': 'Kaleidoscope, Leonard Cohen',
25 'description': 'The Canadian poet and songwriter reflects on his musical career.',
26 'duration': 1740,
27 },
28 'params': {
29 # rtmp download
30 'skip_download': True,
31 }
32 },
33 {
34 'url': 'http://www.bbc.co.uk/iplayer/episode/b00yng5w/The_Man_in_Black_Series_3_The_Printed_Name/',
35 'info_dict': {
36 'id': 'b00yng1d',
37 'ext': 'flv',
38 'title': 'The Man in Black: Series 3: The Printed Name',
39 'description': "Mark Gatiss introduces Nicholas Pierpan's chilling tale of a writer's devilish pact with a mysterious man. Stars Ewan Bailey.",
40 'duration': 1800,
41 },
42 'params': {
43 # rtmp download
44 'skip_download': True,
45 },
46 'skip': 'Episode is no longer available on BBC iPlayer Radio',
47 },
48 {
49 'url': 'http://www.bbc.co.uk/iplayer/episode/b03vhd1f/The_Voice_UK_Series_3_Blind_Auditions_5/',
50 'info_dict': {
51 'id': 'b00yng1d',
52 'ext': 'flv',
53 'title': 'The Voice UK: Series 3: Blind Auditions 5',
54 '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.",
55 'duration': 5100,
56 },
57 'params': {
58 # rtmp download
59 'skip_download': True,
60 },
61 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
62 },
63 {
64 'url': 'http://www.bbc.co.uk/iplayer/episode/p026c7jt/tomorrows-worlds-the-unearthly-history-of-science-fiction-2-invasion',
65 'info_dict': {
66 'id': 'b03k3pb7',
67 'ext': 'flv',
68 'title': "Tomorrow's Worlds: The Unearthly History of Science Fiction",
69 'description': '2. Invasion',
70 'duration': 3600,
71 },
72 'params': {
73 # rtmp download
74 'skip_download': True,
75 },
76 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
77 }, {
78 'url': 'http://www.bbc.co.uk/programmes/b04v20dw',
79 'info_dict': {
80 'id': 'b04v209v',
81 'ext': 'flv',
82 'title': 'Pete Tong, The Essential New Tune Special',
83 'description': "Pete has a very special mix - all of 2014's Essential New Tunes!",
84 'duration': 10800,
85 },
86 'params': {
87 # rtmp download
88 'skip_download': True,
89 }
90 }, {
91 'url': 'http://www.bbc.co.uk/music/clips/p02frcc3',
92 'note': 'Audio',
93 'info_dict': {
94 'id': 'p02frcch',
95 'ext': 'flv',
96 'title': 'Pete Tong, Past, Present and Future Special, Madeon - After Hours mix',
97 'description': 'French house superstar Madeon takes us out of the club and onto the after party.',
98 'duration': 3507,
99 },
100 'params': {
101 # rtmp download
102 'skip_download': True,
103 }
104 }, {
105 'url': 'http://www.bbc.co.uk/music/clips/p025c0zz',
106 'note': 'Video',
107 'info_dict': {
108 'id': 'p025c103',
109 'ext': 'flv',
110 'title': 'Reading and Leeds Festival, 2014, Rae Morris - Closer (Live on BBC Three)',
111 'description': 'Rae Morris performs Closer for BBC Three at Reading 2014',
112 'duration': 226,
113 },
114 'params': {
115 # rtmp download
116 'skip_download': True,
117 }
118 }, {
119 'url': 'http://www.bbc.co.uk/iplayer/episode/b054fn09/ad/natural-world-20152016-2-super-powered-owls',
120 'info_dict': {
121 'id': 'p02n76xf',
122 'ext': 'flv',
123 'title': 'Natural World, 2015-2016: 2. Super Powered Owls',
124 'description': 'md5:e4db5c937d0e95a7c6b5e654d429183d',
125 'duration': 3540,
126 },
127 'params': {
128 # rtmp download
129 'skip_download': True,
130 },
131 'skip': 'geolocation',
132 }, {
133 'url': 'http://www.bbc.co.uk/iplayer/playlist/p01dvks4',
134 'only_matching': True,
135 }, {
136 'url': 'http://www.bbc.co.uk/music/clips#p02frcc3',
137 'only_matching': True,
138 }, {
139 'url': 'http://www.bbc.co.uk/iplayer/cbeebies/episode/b0480276/bing-14-atchoo',
140 'only_matching': True,
141 }
142 ]
143
144 def _extract_asx_playlist(self, connection, programme_id):
145 asx = self._download_xml(connection.get('href'), programme_id, 'Downloading ASX playlist')
146 return [ref.get('href') for ref in asx.findall('./Entry/ref')]
147
148 def _extract_connection(self, connection, programme_id):
149 formats = []
150 protocol = connection.get('protocol')
151 supplier = connection.get('supplier')
152 if protocol == 'http':
153 href = connection.get('href')
154 # ASX playlist
155 if supplier == 'asx':
156 for i, ref in enumerate(self._extract_asx_playlist(connection, programme_id)):
157 formats.append({
158 'url': ref,
159 'format_id': 'ref%s_%s' % (i, supplier),
160 })
161 # Direct link
162 else:
163 formats.append({
164 'url': href,
165 'format_id': supplier,
166 })
167 elif protocol == 'rtmp':
168 application = connection.get('application', 'ondemand')
169 auth_string = connection.get('authString')
170 identifier = connection.get('identifier')
171 server = connection.get('server')
172 formats.append({
173 'url': '%s://%s/%s?%s' % (protocol, server, application, auth_string),
174 'play_path': identifier,
175 'app': '%s?%s' % (application, auth_string),
176 'page_url': 'http://www.bbc.co.uk',
177 'player_url': 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf',
178 'rtmp_live': False,
179 'ext': 'flv',
180 'format_id': supplier,
181 })
182 return formats
183
184 def _extract_items(self, playlist):
185 return playlist.findall('./{http://bbc.co.uk/2008/emp/playlist}item')
186
187 def _extract_medias(self, media_selection):
188 error = media_selection.find('./{http://bbc.co.uk/2008/mp/mediaselection}error')
189 if error is not None:
190 raise ExtractorError(
191 '%s returned error: %s' % (self.IE_NAME, error.get('id')), expected=True)
192 return media_selection.findall('./{http://bbc.co.uk/2008/mp/mediaselection}media')
193
194 def _extract_connections(self, media):
195 return media.findall('./{http://bbc.co.uk/2008/mp/mediaselection}connection')
196
197 def _extract_video(self, media, programme_id):
198 formats = []
199 vbr = int(media.get('bitrate'))
200 vcodec = media.get('encoding')
201 service = media.get('service')
202 width = int(media.get('width'))
203 height = int(media.get('height'))
204 file_size = int(media.get('media_file_size'))
205 for connection in self._extract_connections(media):
206 conn_formats = self._extract_connection(connection, programme_id)
207 for format in conn_formats:
208 format.update({
209 'format_id': '%s_%s' % (service, format['format_id']),
210 'width': width,
211 'height': height,
212 'vbr': vbr,
213 'vcodec': vcodec,
214 'filesize': file_size,
215 })
216 formats.extend(conn_formats)
217 return formats
218
219 def _extract_audio(self, media, programme_id):
220 formats = []
221 abr = int(media.get('bitrate'))
222 acodec = media.get('encoding')
223 service = media.get('service')
224 for connection in self._extract_connections(media):
225 conn_formats = self._extract_connection(connection, programme_id)
226 for format in conn_formats:
227 format.update({
228 'format_id': '%s_%s' % (service, format['format_id']),
229 'abr': abr,
230 'acodec': acodec,
231 })
232 formats.extend(conn_formats)
233 return formats
234
235 def _get_subtitles(self, media, programme_id):
236 subtitles = {}
237 for connection in self._extract_connections(media):
238 captions = self._download_xml(connection.get('href'), programme_id, 'Downloading captions')
239 lang = captions.get('{http://www.w3.org/XML/1998/namespace}lang', 'en')
240 ps = captions.findall('./{0}body/{0}div/{0}p'.format('{http://www.w3.org/2006/10/ttaf1}'))
241 srt = ''
242
243 def _extract_text(p):
244 if p.text is not None:
245 stripped_text = p.text.strip()
246 if stripped_text:
247 return stripped_text
248 return ' '.join(span.text.strip() for span in p.findall('{http://www.w3.org/2006/10/ttaf1}span'))
249 for pos, p in enumerate(ps):
250 srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (str(pos), p.get('begin'), p.get('end'), _extract_text(p))
251 subtitles[lang] = [
252 {
253 'url': connection.get('href'),
254 'ext': 'ttml',
255 },
256 {
257 'data': srt,
258 'ext': 'srt',
259 },
260 ]
261 return subtitles
262
263 def _download_media_selector(self, programme_id):
264 try:
265 media_selection = self._download_xml(
266 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/%s' % programme_id,
267 programme_id, 'Downloading media selection XML')
268 except ExtractorError as ee:
269 if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
270 media_selection = xml.etree.ElementTree.fromstring(ee.cause.read().encode('utf-8'))
271 else:
272 raise
273
274 formats = []
275 subtitles = None
276
277 for media in self._extract_medias(media_selection):
278 kind = media.get('kind')
279 if kind == 'audio':
280 formats.extend(self._extract_audio(media, programme_id))
281 elif kind == 'video':
282 formats.extend(self._extract_video(media, programme_id))
283 elif kind == 'captions':
284 subtitles = self.extract_subtitles(media, programme_id)
285
286 return formats, subtitles
287
288 def _download_playlist(self, playlist_id):
289 try:
290 playlist = self._download_json(
291 'http://www.bbc.co.uk/programmes/%s/playlist.json' % playlist_id,
292 playlist_id, 'Downloading playlist JSON')
293
294 version = playlist.get('defaultAvailableVersion')
295 if version:
296 smp_config = version['smpConfig']
297 title = smp_config['title']
298 description = smp_config['summary']
299 for item in smp_config['items']:
300 kind = item['kind']
301 if kind != 'programme' and kind != 'radioProgramme':
302 continue
303 programme_id = item.get('vpid')
304 duration = int(item.get('duration'))
305 formats, subtitles = self._download_media_selector(programme_id)
306 return programme_id, title, description, duration, formats, subtitles
307 except ExtractorError as ee:
308 if not (isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404):
309 raise
310
311 # fallback to legacy playlist
312 playlist = self._download_xml(
313 'http://www.bbc.co.uk/iplayer/playlist/%s' % playlist_id,
314 playlist_id, 'Downloading legacy playlist XML')
315
316 no_items = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}noItems')
317 if no_items is not None:
318 reason = no_items.get('reason')
319 if reason == 'preAvailability':
320 msg = 'Episode %s is not yet available' % playlist_id
321 elif reason == 'postAvailability':
322 msg = 'Episode %s is no longer available' % playlist_id
323 elif reason == 'noMedia':
324 msg = 'Episode %s is not currently available' % playlist_id
325 else:
326 msg = 'Episode %s is not available: %s' % (playlist_id, reason)
327 raise ExtractorError(msg, expected=True)
328
329 for item in self._extract_items(playlist):
330 kind = item.get('kind')
331 if kind != 'programme' and kind != 'radioProgramme':
332 continue
333 title = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}title').text
334 description = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}summary').text
335 programme_id = item.get('identifier')
336 duration = int(item.get('duration'))
337 formats, subtitles = self._download_media_selector(programme_id)
338
339 return programme_id, title, description, duration, formats, subtitles
340
341 def _real_extract(self, url):
342 group_id = self._match_id(url)
343
344 webpage = self._download_webpage(url, group_id, 'Downloading video page')
345
346 programme_id = None
347
348 tviplayer = self._search_regex(
349 r'mediator\.bind\(({.+?})\s*,\s*document\.getElementById',
350 webpage, 'player', default=None)
351
352 if tviplayer:
353 player = self._parse_json(tviplayer, group_id).get('player', {})
354 duration = int_or_none(player.get('duration'))
355 programme_id = player.get('vpid')
356
357 if not programme_id:
358 programme_id = self._search_regex(
359 r'"vpid"\s*:\s*"([\da-z]{8})"', webpage, 'vpid', fatal=False, default=None)
360
361 if programme_id:
362 formats, subtitles = self._download_media_selector(programme_id)
363 title = self._og_search_title(webpage)
364 description = self._search_regex(
365 r'<p class="medium-description">([^<]+)</p>',
366 webpage, 'description', fatal=False)
367 else:
368 programme_id, title, description, duration, formats, subtitles = self._download_playlist(group_id)
369
370 self._sort_formats(formats)
371
372 return {
373 'id': programme_id,
374 'title': title,
375 'description': description,
376 'thumbnail': self._og_search_thumbnail(webpage, default=None),
377 'duration': duration,
378 'formats': formats,
379 'subtitles': subtitles,
380 }