]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/npo.py
[mlb] Improve _VALID_URL (Closes #5260)
[yt-dlp.git] / youtube_dl / extractor / npo.py
CommitLineData
331ae266
JMF
1from __future__ import unicode_literals
2
171ca612 3from .common import InfoExtractor
331ae266 4from ..utils import (
5c4a81d9 5 fix_xml_ampersands,
944a3de2 6 parse_duration,
c9ea760e 7 qualities,
609a61e3 8 strip_jsonp,
5c4a81d9 9 unified_strdate,
d0df9292 10 url_basename,
331ae266
JMF
11)
12
13
b9b42f2e 14class NPOBaseIE(InfoExtractor):
04e0bac2
S
15 def _get_token(self, video_id):
16 token_page = self._download_webpage(
17 'http://ida.omroep.nl/npoplayer/i.js',
18 video_id, note='Downloading token')
19 return self._search_regex(
20 r'npoplayer\.token = "(.+?)"', token_page, 'token')
21
22
23class NPOIE(NPOBaseIE):
331ae266 24 IE_NAME = 'npo.nl'
171ca612 25 _VALID_URL = r'https?://(?:www\.)?npo\.nl/(?!live|radio)[^/]+/[^/]+/(?P<id>[^/?]+)'
331ae266 26
944a3de2
S
27 _TESTS = [
28 {
29 'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
30 'md5': '4b3f9c429157ec4775f2c9cb7b911016',
31 'info_dict': {
32 'id': 'VPWON_1220719',
33 'ext': 'm4v',
34 'title': 'Nieuwsuur',
35 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
36 'upload_date': '20140622',
37 },
331ae266 38 },
944a3de2
S
39 {
40 'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
41 'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
42 'info_dict': {
43 'id': 'VARA_101191800',
44 'ext': 'm4v',
45 'title': 'De Mega Mike & Mega Thomas show',
46 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
47 'upload_date': '20090227',
48 'duration': 2400,
49 },
50 },
51 {
52 'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
53 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
54 'info_dict': {
55 'id': 'VPWON_1169289',
56 'ext': 'm4v',
57 'title': 'Tegenlicht',
58 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
59 'upload_date': '20130225',
60 'duration': 3000,
61 },
1ff30d7b
S
62 },
63 {
64 'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706',
65 'info_dict': {
66 'id': 'WO_VPRO_043706',
67 'ext': 'wmv',
68 'title': 'De nieuwe mens - Deel 1',
69 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
70 'duration': 4680,
71 },
72 'params': {
73 # mplayer mms download
74 'skip_download': True,
75 }
76 },
c85f3683
S
77 # non asf in streams
78 {
79 'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
80 'md5': 'b3da13de374cbe2d5332a7e910bef97f',
81 'info_dict': {
82 'id': 'WO_NOS_762771',
83 'ext': 'mp4',
84 'title': 'Hoe gaat Europa verder na Parijs?',
85 },
86 },
944a3de2 87 ]
331ae266
JMF
88
89 def _real_extract(self, url):
04e0bac2 90 video_id = self._match_id(url)
d0df9292 91 return self._get_info(video_id)
331ae266 92
d0df9292 93 def _get_info(self, video_id):
331ae266
JMF
94 metadata = self._download_json(
95 'http://e.omroep.nl/metadata/aflevering/%s' % video_id,
96 video_id,
97 # We have to remove the javascript callback
609a61e3 98 transform_source=strip_jsonp,
331ae266 99 )
04e0bac2
S
100
101 token = self._get_token(video_id)
331ae266 102
c9ea760e 103 formats = []
1ff30d7b
S
104
105 pubopties = metadata.get('pubopties')
106 if pubopties:
107 quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
108 for format_id in pubopties:
109 format_info = self._download_json(
110 'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s'
111 % (video_id, format_id, token),
112 video_id, 'Downloading %s JSON' % format_id)
113 if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
114 continue
115 streams = format_info.get('streams')
116 if streams:
117 video_info = self._download_json(
118 streams[0] + '&type=json',
119 video_id, 'Downloading %s stream JSON' % format_id)
120 else:
121 video_info = format_info
122 video_url = video_info.get('url')
123 if not video_url:
124 continue
125 if format_id == 'adaptive':
126 formats.extend(self._extract_m3u8_formats(video_url, video_id))
127 else:
128 formats.append({
129 'url': video_url,
130 'format_id': format_id,
131 'quality': quality(format_id),
132 })
133
134 streams = metadata.get('streams')
135 if streams:
136 for i, stream in enumerate(streams):
137 stream_url = stream.get('url')
138 if not stream_url:
139 continue
5c4a81d9 140 if '.asf' not in stream_url:
a0977064
S
141 formats.append({
142 'url': stream_url,
143 'quality': stream.get('kwaliteit'),
144 })
145 continue
1ff30d7b
S
146 asx = self._download_xml(
147 stream_url, video_id,
148 'Downloading stream %d ASX playlist' % i,
149 transform_source=fix_xml_ampersands)
150 ref = asx.find('./ENTRY/Ref')
151 if ref is None:
152 continue
153 video_url = ref.get('href')
154 if not video_url:
155 continue
c9ea760e 156 formats.append({
944a3de2 157 'url': video_url,
1ff30d7b
S
158 'ext': stream.get('formaat', 'asf'),
159 'quality': stream.get('kwaliteit'),
c9ea760e 160 })
1ff30d7b 161
9e91449c 162 self._sort_formats(formats)
25e5ebf3 163
9e91449c
S
164 subtitles = {}
165 if metadata.get('tt888') == 'ja':
b9b42f2e
JMF
166 subtitles['nl'] = [{
167 'ext': 'vtt',
168 'url': 'http://e.omroep.nl/tt888/%s' % video_id,
169 }]
331ae266
JMF
170
171 return {
172 'id': video_id,
173 'title': metadata['titel'],
331ae266 174 'description': metadata['info'],
944a3de2
S
175 'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
176 'upload_date': unified_strdate(metadata.get('gidsdatum')),
177 'duration': parse_duration(metadata.get('tijdsduur')),
c9ea760e 178 'formats': formats,
25e5ebf3 179 'subtitles': subtitles,
331ae266 180 }
d0df9292
JMF
181
182
04e0bac2
S
183class NPOLiveIE(NPOBaseIE):
184 IE_NAME = 'npo.nl:live'
171ca612 185 _VALID_URL = r'https?://(?:www\.)?npo\.nl/live/(?P<id>.+)'
04e0bac2
S
186
187 _TEST = {
188 'url': 'http://www.npo.nl/live/npo-1',
189 'info_dict': {
190 'id': 'LI_NEDERLAND1_136692',
191 'display_id': 'npo-1',
192 'ext': 'mp4',
193 'title': 're:^Nederland 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
194 'description': 'Livestream',
195 'is_live': True,
196 },
197 'params': {
198 'skip_download': True,
199 }
200 }
201
202 def _real_extract(self, url):
203 display_id = self._match_id(url)
204
205 webpage = self._download_webpage(url, display_id)
206
207 live_id = self._search_regex(
208 r'data-prid="([^"]+)"', webpage, 'live id')
209
210 metadata = self._download_json(
211 'http://e.omroep.nl/metadata/%s' % live_id,
212 display_id, transform_source=strip_jsonp)
213
214 token = self._get_token(display_id)
215
216 formats = []
217
218 streams = metadata.get('streams')
219 if streams:
220 for stream in streams:
221 stream_type = stream.get('type').lower()
32aaeca7
S
222 # smooth streaming is not supported
223 if stream_type in ['ss', 'ms']:
04e0bac2
S
224 continue
225 stream_info = self._download_json(
226 'http://ida.omroep.nl/aapi/?stream=%s&token=%s&type=jsonp'
227 % (stream.get('url'), token),
228 display_id, 'Downloading %s JSON' % stream_type)
229 if stream_info.get('error_code', 0) or stream_info.get('errorcode', 0):
230 continue
231 stream_url = self._download_json(
232 stream_info['stream'], display_id,
233 'Downloading %s URL' % stream_type,
234 transform_source=strip_jsonp)
235 if stream_type == 'hds':
236 f4m_formats = self._extract_f4m_formats(stream_url, display_id)
237 # f4m downloader downloads only piece of live stream
238 for f4m_format in f4m_formats:
239 f4m_format['preference'] = -1
240 formats.extend(f4m_formats)
241 elif stream_type == 'hls':
242 formats.extend(self._extract_m3u8_formats(stream_url, display_id, 'mp4'))
243 else:
244 formats.append({
245 'url': stream_url,
32aaeca7 246 'preference': -10,
04e0bac2
S
247 })
248
249 self._sort_formats(formats)
250
251 return {
252 'id': live_id,
253 'display_id': display_id,
254 'title': self._live_title(metadata['titel']),
255 'description': metadata['info'],
256 'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
257 'formats': formats,
258 'is_live': True,
259 }
260
261
171ca612
S
262class NPORadioIE(InfoExtractor):
263 IE_NAME = 'npo.nl:radio'
264 _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/(?P<id>[^/]+)/?$'
265
266 _TEST = {
267 'url': 'http://www.npo.nl/radio/radio-1',
268 'info_dict': {
269 'id': 'radio-1',
270 'ext': 'mp3',
271 'title': 're:^NPO Radio 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
272 'is_live': True,
273 },
274 'params': {
275 'skip_download': True,
276 }
277 }
278
279 @staticmethod
280 def _html_get_attribute_regex(attribute):
281 return r'{0}\s*=\s*\'([^\']+)\''.format(attribute)
282
283 def _real_extract(self, url):
284 video_id = self._match_id(url)
285
286 webpage = self._download_webpage(url, video_id)
287
288 title = self._html_search_regex(
289 self._html_get_attribute_regex('data-channel'), webpage, 'title')
290
291 stream = self._parse_json(
292 self._html_search_regex(self._html_get_attribute_regex('data-streams'), webpage, 'data-streams'),
293 video_id)
294
295 codec = stream.get('codec')
296
297 return {
298 'id': video_id,
299 'url': stream['url'],
300 'title': self._live_title(title),
301 'acodec': codec,
302 'ext': codec,
303 'is_live': True,
304 }
305
306
307class NPORadioFragmentIE(InfoExtractor):
308 IE_NAME = 'npo.nl:radio:fragment'
309 _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/[^/]+/fragment/(?P<id>\d+)'
310
311 _TEST = {
312 'url': 'http://www.npo.nl/radio/radio-5/fragment/174356',
313 'md5': 'dd8cc470dad764d0fdc70a9a1e2d18c2',
314 'info_dict': {
315 'id': '174356',
316 'ext': 'mp3',
317 'title': 'Jubileumconcert Willeke Alberti',
318 },
319 }
320
321 def _real_extract(self, url):
322 audio_id = self._match_id(url)
323
324 webpage = self._download_webpage(url, audio_id)
325
326 title = self._html_search_regex(
327 r'href="/radio/[^/]+/fragment/%s" title="([^"]+)"' % audio_id,
328 webpage, 'title')
329
330 audio_url = self._search_regex(
331 r"data-streams='([^']+)'", webpage, 'audio url')
332
333 return {
334 'id': audio_id,
335 'url': audio_url,
336 'title': title,
337 }
338
339
d0df9292
JMF
340class TegenlichtVproIE(NPOIE):
341 IE_NAME = 'tegenlicht.vpro.nl'
342 _VALID_URL = r'https?://tegenlicht\.vpro\.nl/afleveringen/.*?'
343
344 _TESTS = [
345 {
346 'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
347 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
348 'info_dict': {
349 'id': 'VPWON_1169289',
350 'ext': 'm4v',
351 'title': 'Tegenlicht',
352 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
353 'upload_date': '20130225',
354 },
355 },
356 ]
357
358 def _real_extract(self, url):
359 name = url_basename(url)
360 webpage = self._download_webpage(url, name)
361 urn = self._html_search_meta('mediaurn', webpage)
362 info_page = self._download_json(
363 'http://rs.vpro.nl/v2/api/media/%s.json' % urn, name)
364 return self._get_info(info_page['mid'])