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