]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bbccouk.py
Prepare for upload.
[youtubedl] / youtube_dl / extractor / bbccouk.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .subtitles import SubtitlesInfoExtractor
6 from ..utils import ExtractorError
7
8
9 class BBCCoUkIE(SubtitlesInfoExtractor):
10 IE_NAME = 'bbc.co.uk'
11 IE_DESC = 'BBC iPlayer'
12 _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:programmes|iplayer/episode)/(?P<id>[\da-z]{8})'
13
14 _TESTS = [
15 {
16 'url': 'http://www.bbc.co.uk/programmes/b039g8p7',
17 'info_dict': {
18 'id': 'b039d07m',
19 'ext': 'flv',
20 'title': 'Kaleidoscope: Leonard Cohen',
21 'description': 'md5:db4755d7a665ae72343779f7dacb402c',
22 'duration': 1740,
23 },
24 'params': {
25 # rtmp download
26 'skip_download': True,
27 }
28 },
29 {
30 'url': 'http://www.bbc.co.uk/iplayer/episode/b00yng5w/The_Man_in_Black_Series_3_The_Printed_Name/',
31 'info_dict': {
32 'id': 'b00yng1d',
33 'ext': 'flv',
34 'title': 'The Man in Black: Series 3: The Printed Name',
35 'description': "Mark Gatiss introduces Nicholas Pierpan's chilling tale of a writer's devilish pact with a mysterious man. Stars Ewan Bailey.",
36 'duration': 1800,
37 },
38 'params': {
39 # rtmp download
40 'skip_download': True,
41 },
42 'skip': 'Episode is no longer available on BBC iPlayer Radio',
43 },
44 {
45 'url': 'http://www.bbc.co.uk/iplayer/episode/b03vhd1f/The_Voice_UK_Series_3_Blind_Auditions_5/',
46 'info_dict': {
47 'id': 'b00yng1d',
48 'ext': 'flv',
49 'title': 'The Voice UK: Series 3: Blind Auditions 5',
50 '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.",
51 'duration': 5100,
52 },
53 'params': {
54 # rtmp download
55 'skip_download': True,
56 },
57 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
58 }
59 ]
60
61 def _extract_asx_playlist(self, connection, programme_id):
62 asx = self._download_xml(connection.get('href'), programme_id, 'Downloading ASX playlist')
63 return [ref.get('href') for ref in asx.findall('./Entry/ref')]
64
65 def _extract_connection(self, connection, programme_id):
66 formats = []
67 protocol = connection.get('protocol')
68 supplier = connection.get('supplier')
69 if protocol == 'http':
70 href = connection.get('href')
71 # ASX playlist
72 if supplier == 'asx':
73 for i, ref in enumerate(self._extract_asx_playlist(connection, programme_id)):
74 formats.append({
75 'url': ref,
76 'format_id': 'ref%s_%s' % (i, supplier),
77 })
78 # Direct link
79 else:
80 formats.append({
81 'url': href,
82 'format_id': supplier,
83 })
84 elif protocol == 'rtmp':
85 application = connection.get('application', 'ondemand')
86 auth_string = connection.get('authString')
87 identifier = connection.get('identifier')
88 server = connection.get('server')
89 formats.append({
90 'url': '%s://%s/%s?%s' % (protocol, server, application, auth_string),
91 'play_path': identifier,
92 'app': '%s?%s' % (application, auth_string),
93 'page_url': 'http://www.bbc.co.uk',
94 'player_url': 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf',
95 'rtmp_live': False,
96 'ext': 'flv',
97 'format_id': supplier,
98 })
99 return formats
100
101 def _extract_items(self, playlist):
102 return playlist.findall('./{http://bbc.co.uk/2008/emp/playlist}item')
103
104 def _extract_medias(self, media_selection):
105 return media_selection.findall('./{http://bbc.co.uk/2008/mp/mediaselection}media')
106
107 def _extract_connections(self, media):
108 return media.findall('./{http://bbc.co.uk/2008/mp/mediaselection}connection')
109
110 def _extract_video(self, media, programme_id):
111 formats = []
112 vbr = int(media.get('bitrate'))
113 vcodec = media.get('encoding')
114 service = media.get('service')
115 width = int(media.get('width'))
116 height = int(media.get('height'))
117 file_size = int(media.get('media_file_size'))
118 for connection in self._extract_connections(media):
119 conn_formats = self._extract_connection(connection, programme_id)
120 for format in conn_formats:
121 format.update({
122 'format_id': '%s_%s' % (service, format['format_id']),
123 'width': width,
124 'height': height,
125 'vbr': vbr,
126 'vcodec': vcodec,
127 'filesize': file_size,
128 })
129 formats.extend(conn_formats)
130 return formats
131
132 def _extract_audio(self, media, programme_id):
133 formats = []
134 abr = int(media.get('bitrate'))
135 acodec = media.get('encoding')
136 service = media.get('service')
137 for connection in self._extract_connections(media):
138 conn_formats = self._extract_connection(connection, programme_id)
139 for format in conn_formats:
140 format.update({
141 'format_id': '%s_%s' % (service, format['format_id']),
142 'abr': abr,
143 'acodec': acodec,
144 })
145 formats.extend(conn_formats)
146 return formats
147
148 def _extract_captions(self, media, programme_id):
149 subtitles = {}
150 for connection in self._extract_connections(media):
151 captions = self._download_xml(connection.get('href'), programme_id, 'Downloading captions')
152 lang = captions.get('{http://www.w3.org/XML/1998/namespace}lang', 'en')
153 ps = captions.findall('./{0}body/{0}div/{0}p'.format('{http://www.w3.org/2006/10/ttaf1}'))
154 srt = ''
155 for pos, p in enumerate(ps):
156 srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (str(pos), p.get('begin'), p.get('end'),
157 p.text.strip() if p.text is not None else '')
158 subtitles[lang] = srt
159 return subtitles
160
161 def _real_extract(self, url):
162 mobj = re.match(self._VALID_URL, url)
163 group_id = mobj.group('id')
164
165 webpage = self._download_webpage(url, group_id, 'Downloading video page')
166 if re.search(r'id="emp-error" class="notinuk">', webpage):
167 raise ExtractorError('Currently BBC iPlayer TV programmes are available to play in the UK only',
168 expected=True)
169
170 playlist = self._download_xml('http://www.bbc.co.uk/iplayer/playlist/%s' % group_id, group_id,
171 'Downloading playlist XML')
172
173 no_items = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}noItems')
174 if no_items is not None:
175 reason = no_items.get('reason')
176 if reason == 'preAvailability':
177 msg = 'Episode %s is not yet available' % group_id
178 elif reason == 'postAvailability':
179 msg = 'Episode %s is no longer available' % group_id
180 else:
181 msg = 'Episode %s is not available: %s' % (group_id, reason)
182 raise ExtractorError(msg, expected=True)
183
184 formats = []
185 subtitles = None
186
187 for item in self._extract_items(playlist):
188 kind = item.get('kind')
189 if kind != 'programme' and kind != 'radioProgramme':
190 continue
191 title = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}title').text
192 description = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}summary').text
193
194 programme_id = item.get('identifier')
195 duration = int(item.get('duration'))
196
197 media_selection = self._download_xml(
198 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/%s' % programme_id,
199 programme_id, 'Downloading media selection XML')
200
201 for media in self._extract_medias(media_selection):
202 kind = media.get('kind')
203 if kind == 'audio':
204 formats.extend(self._extract_audio(media, programme_id))
205 elif kind == 'video':
206 formats.extend(self._extract_video(media, programme_id))
207 elif kind == 'captions':
208 subtitles = self._extract_captions(media, programme_id)
209
210 if self._downloader.params.get('listsubtitles', False):
211 self._list_available_subtitles(programme_id, subtitles)
212 return
213
214 self._sort_formats(formats)
215
216 return {
217 'id': programme_id,
218 'title': title,
219 'description': description,
220 'duration': duration,
221 'formats': formats,
222 'subtitles': subtitles,
223 }