]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/bbccouk.py
[downloader/mplayer] Use check_executable
[yt-dlp.git] / youtube_dl / extractor / bbccouk.py
CommitLineData
082c6c86
S
1from __future__ import unicode_literals
2
c056efa2 3import xml.etree.ElementTree
082c6c86 4
2e3fd9ec 5from .subtitles import SubtitlesInfoExtractor
082c6c86 6from ..utils import ExtractorError
c056efa2 7from ..compat import compat_HTTPError
082c6c86
S
8
9
2e3fd9ec 10class BBCCoUkIE(SubtitlesInfoExtractor):
082c6c86 11 IE_NAME = 'bbc.co.uk'
2e3fd9ec 12 IE_DESC = 'BBC iPlayer'
31763975 13 _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:programmes|iplayer/(?:episode|playlist))/(?P<id>[\da-z]{8})'
082c6c86 14
2e3fd9ec
S
15 _TESTS = [
16 {
f2d0fc68 17 'url': 'http://www.bbc.co.uk/programmes/b039g8p7',
2e3fd9ec 18 'info_dict': {
f2d0fc68 19 'id': 'b039d07m',
2e3fd9ec 20 'ext': 'flv',
c4914185
S
21 'title': 'Kaleidoscope, Leonard Cohen',
22 'description': 'The Canadian poet and songwriter reflects on his musical career.',
f2d0fc68 23 'duration': 1740,
2e3fd9ec
S
24 },
25 'params': {
26 # rtmp download
27 'skip_download': True,
28 }
082c6c86 29 },
2e3fd9ec
S
30 {
31 'url': 'http://www.bbc.co.uk/iplayer/episode/b00yng5w/The_Man_in_Black_Series_3_The_Printed_Name/',
32 'info_dict': {
33 'id': 'b00yng1d',
34 'ext': 'flv',
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.",
37 'duration': 1800,
38 },
39 'params': {
40 # rtmp download
41 'skip_download': True,
c7f0177f
S
42 },
43 'skip': 'Episode is no longer available on BBC iPlayer Radio',
2e3fd9ec
S
44 },
45 {
46 'url': 'http://www.bbc.co.uk/iplayer/episode/b03vhd1f/The_Voice_UK_Series_3_Blind_Auditions_5/',
47 'info_dict': {
48 'id': 'b00yng1d',
49 'ext': 'flv',
17968e44
S
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.",
52 'duration': 5100,
2e3fd9ec
S
53 },
54 'params': {
55 # rtmp download
56 'skip_download': True,
57 },
58 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
c056efa2
S
59 },
60 {
61 'url': 'http://www.bbc.co.uk/iplayer/episode/p026c7jt/tomorrows-worlds-the-unearthly-history-of-science-fiction-2-invasion',
62 'info_dict': {
63 'id': 'b03k3pb7',
64 'ext': 'flv',
65 'title': "Tomorrow's Worlds: The Unearthly History of Science Fiction",
66 'description': '2. Invasion',
67 'duration': 3600,
68 },
69 'params': {
70 # rtmp download
71 'skip_download': True,
72 },
73 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
ae6986fb
S
74 }, {
75 'url': 'http://www.bbc.co.uk/programmes/b04v20dw',
76 'info_dict': {
77 'id': 'b04v209v',
78 'ext': 'flv',
79 'title': 'Pete Tong, The Essential New Tune Special',
80 'description': "Pete has a very special mix - all of 2014's Essential New Tunes!",
81 'duration': 10800,
82 },
83 'params': {
84 # rtmp download
85 'skip_download': True,
86 }
31763975
S
87 }, {
88 'url': 'http://www.bbc.co.uk/iplayer/playlist/p01dvks4',
89 'only_matching': True,
ae6986fb 90 }
2e3fd9ec
S
91 ]
92
93 def _extract_asx_playlist(self, connection, programme_id):
94 asx = self._download_xml(connection.get('href'), programme_id, 'Downloading ASX playlist')
95 return [ref.get('href') for ref in asx.findall('./Entry/ref')]
96
97 def _extract_connection(self, connection, programme_id):
98 formats = []
99 protocol = connection.get('protocol')
100 supplier = connection.get('supplier')
101 if protocol == 'http':
102 href = connection.get('href')
103 # ASX playlist
104 if supplier == 'asx':
105 for i, ref in enumerate(self._extract_asx_playlist(connection, programme_id)):
106 formats.append({
107 'url': ref,
108 'format_id': 'ref%s_%s' % (i, supplier),
109 })
110 # Direct link
111 else:
112 formats.append({
113 'url': href,
114 'format_id': supplier,
115 })
116 elif protocol == 'rtmp':
117 application = connection.get('application', 'ondemand')
118 auth_string = connection.get('authString')
119 identifier = connection.get('identifier')
120 server = connection.get('server')
121 formats.append({
122 'url': '%s://%s/%s?%s' % (protocol, server, application, auth_string),
123 'play_path': identifier,
124 'app': '%s?%s' % (application, auth_string),
125 'page_url': 'http://www.bbc.co.uk',
126 'player_url': 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf',
127 'rtmp_live': False,
128 'ext': 'flv',
129 'format_id': supplier,
130 })
131 return formats
132
133 def _extract_items(self, playlist):
134 return playlist.findall('./{http://bbc.co.uk/2008/emp/playlist}item')
135
136 def _extract_medias(self, media_selection):
c056efa2
S
137 error = media_selection.find('./{http://bbc.co.uk/2008/mp/mediaselection}error')
138 if error is not None:
139 raise ExtractorError(
140 '%s returned error: %s' % (self.IE_NAME, error.get('id')), expected=True)
2e3fd9ec
S
141 return media_selection.findall('./{http://bbc.co.uk/2008/mp/mediaselection}media')
142
143 def _extract_connections(self, media):
144 return media.findall('./{http://bbc.co.uk/2008/mp/mediaselection}connection')
145
146 def _extract_video(self, media, programme_id):
147 formats = []
148 vbr = int(media.get('bitrate'))
149 vcodec = media.get('encoding')
150 service = media.get('service')
151 width = int(media.get('width'))
152 height = int(media.get('height'))
153 file_size = int(media.get('media_file_size'))
154 for connection in self._extract_connections(media):
155 conn_formats = self._extract_connection(connection, programme_id)
156 for format in conn_formats:
157 format.update({
158 'format_id': '%s_%s' % (service, format['format_id']),
159 'width': width,
160 'height': height,
161 'vbr': vbr,
162 'vcodec': vcodec,
163 'filesize': file_size,
164 })
165 formats.extend(conn_formats)
166 return formats
167
168 def _extract_audio(self, media, programme_id):
169 formats = []
170 abr = int(media.get('bitrate'))
171 acodec = media.get('encoding')
172 service = media.get('service')
173 for connection in self._extract_connections(media):
174 conn_formats = self._extract_connection(connection, programme_id)
175 for format in conn_formats:
176 format.update({
177 'format_id': '%s_%s' % (service, format['format_id']),
178 'abr': abr,
179 'acodec': acodec,
180 })
181 formats.extend(conn_formats)
182 return formats
183
184 def _extract_captions(self, media, programme_id):
185 subtitles = {}
186 for connection in self._extract_connections(media):
187 captions = self._download_xml(connection.get('href'), programme_id, 'Downloading captions')
188 lang = captions.get('{http://www.w3.org/XML/1998/namespace}lang', 'en')
189 ps = captions.findall('./{0}body/{0}div/{0}p'.format('{http://www.w3.org/2006/10/ttaf1}'))
190 srt = ''
191 for pos, p in enumerate(ps):
192 srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (str(pos), p.get('begin'), p.get('end'),
193 p.text.strip() if p.text is not None else '')
194 subtitles[lang] = srt
195 return subtitles
082c6c86 196
c056efa2
S
197 def _download_media_selector(self, programme_id):
198 try:
199 media_selection = self._download_xml(
200 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/%s' % programme_id,
201 programme_id, 'Downloading media selection XML')
202 except ExtractorError as ee:
203 if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
204 media_selection = xml.etree.ElementTree.fromstring(ee.cause.read().encode('utf-8'))
2e3fd9ec 205 else:
c056efa2 206 raise
082c6c86
S
207
208 formats = []
2e3fd9ec
S
209 subtitles = None
210
c056efa2
S
211 for media in self._extract_medias(media_selection):
212 kind = media.get('kind')
213 if kind == 'audio':
214 formats.extend(self._extract_audio(media, programme_id))
215 elif kind == 'video':
216 formats.extend(self._extract_video(media, programme_id))
217 elif kind == 'captions':
218 subtitles = self._extract_captions(media, programme_id)
2e3fd9ec 219
c056efa2 220 return formats, subtitles
2e3fd9ec 221
ae6986fb
S
222 def _download_playlist(self, playlist_id):
223 try:
224 playlist = self._download_json(
225 'http://www.bbc.co.uk/programmes/%s/playlist.json' % playlist_id,
226 playlist_id, 'Downloading playlist JSON')
227
228 version = playlist.get('defaultAvailableVersion')
229 if version:
230 smp_config = version['smpConfig']
231 title = smp_config['title']
232 description = smp_config['summary']
233 for item in smp_config['items']:
234 kind = item['kind']
235 if kind != 'programme' and kind != 'radioProgramme':
236 continue
237 programme_id = item.get('vpid')
238 duration = int(item.get('duration'))
239 formats, subtitles = self._download_media_selector(programme_id)
240 return programme_id, title, description, duration, formats, subtitles
241 except ExtractorError as ee:
242 if not isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404:
243 raise
244
245 # fallback to legacy playlist
246 playlist = self._download_xml(
931e2d1d
PH
247 'http://www.bbc.co.uk/iplayer/playlist/%s' % playlist_id,
248 playlist_id, 'Downloading legacy playlist XML')
ae6986fb
S
249
250 no_items = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}noItems')
251 if no_items is not None:
252 reason = no_items.get('reason')
253 if reason == 'preAvailability':
254 msg = 'Episode %s is not yet available' % playlist_id
255 elif reason == 'postAvailability':
256 msg = 'Episode %s is no longer available' % playlist_id
257 elif reason == 'noMedia':
258 msg = 'Episode %s is not currently available' % playlist_id
259 else:
260 msg = 'Episode %s is not available: %s' % (playlist_id, reason)
261 raise ExtractorError(msg, expected=True)
262
263 for item in self._extract_items(playlist):
264 kind = item.get('kind')
265 if kind != 'programme' and kind != 'radioProgramme':
266 continue
267 title = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}title').text
268 description = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}summary').text
269 programme_id = item.get('identifier')
270 duration = int(item.get('duration'))
271 formats, subtitles = self._download_media_selector(programme_id)
272
273 return programme_id, title, description, duration, formats, subtitles
274
c056efa2
S
275 def _real_extract(self, url):
276 group_id = self._match_id(url)
277
278 webpage = self._download_webpage(url, group_id, 'Downloading video page')
279
280 programme_id = self._search_regex(
da4d4191 281 r'"vpid"\s*:\s*"([\da-z]{8})"', webpage, 'vpid', fatal=False, default=None)
c056efa2
S
282 if programme_id:
283 player = self._download_json(
284 'http://www.bbc.co.uk/iplayer/episode/%s.json' % group_id,
285 group_id)['jsConf']['player']
286 title = player['title']
287 description = player['subtitle']
288 duration = player['duration']
289 formats, subtitles = self._download_media_selector(programme_id)
290 else:
ae6986fb 291 programme_id, title, description, duration, formats, subtitles = self._download_playlist(group_id)
2e3fd9ec
S
292
293 if self._downloader.params.get('listsubtitles', False):
294 self._list_available_subtitles(programme_id, subtitles)
295 return
082c6c86
S
296
297 self._sort_formats(formats)
298
299 return {
2e3fd9ec 300 'id': programme_id,
082c6c86
S
301 'title': title,
302 'description': description,
303 'duration': duration,
304 'formats': formats,
2e3fd9ec 305 'subtitles': subtitles,
5f6a1245 306 }