[npo] Relax _VALID_URL (closes #14987, closes #14994)
[youtube-dl] / youtube_dl / extractor / npo.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_HTTPError,
8     compat_str,
9 )
10 from ..utils import (
11     determine_ext,
12     ExtractorError,
13     fix_xml_ampersands,
14     orderedSet,
15     parse_duration,
16     qualities,
17     strip_jsonp,
18     unified_strdate,
19 )
20
21
22 class NPOBaseIE(InfoExtractor):
23     def _get_token(self, video_id):
24         return self._download_json(
25             'http://ida.omroep.nl/app.php/auth', video_id,
26             note='Downloading token')['token']
27
28
29 class NPOIE(NPOBaseIE):
30     IE_NAME = 'npo'
31     IE_DESC = 'npo.nl, ntr.nl, omroepwnl.nl, zapp.nl and npo3.nl'
32     _VALID_URL = r'''(?x)
33                     (?:
34                         npo:|
35                         https?://
36                             (?:www\.)?
37                             (?:
38                                 npo\.nl/(?!(?:live|radio)/)(?:[^/]+/){2}|
39                                 ntr\.nl/(?:[^/]+/){2,}|
40                                 omroepwnl\.nl/video/fragment/[^/]+__|
41                                 (?:zapp|npo3)\.nl/(?:[^/]+/){2,}
42                             )
43                         )
44                         (?P<id>[^/?#]+)
45                 '''
46
47     _TESTS = [{
48         'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
49         'md5': '4b3f9c429157ec4775f2c9cb7b911016',
50         'info_dict': {
51             'id': 'VPWON_1220719',
52             'ext': 'm4v',
53             'title': 'Nieuwsuur',
54             'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
55             'upload_date': '20140622',
56         },
57     }, {
58         'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
59         'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
60         'info_dict': {
61             'id': 'VARA_101191800',
62             'ext': 'm4v',
63             'title': 'De Mega Mike & Mega Thomas show: The best of.',
64             'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
65             'upload_date': '20090227',
66             'duration': 2400,
67         },
68     }, {
69         'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
70         'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
71         'info_dict': {
72             'id': 'VPWON_1169289',
73             'ext': 'm4v',
74             'title': 'Tegenlicht: Zwart geld. De toekomst komt uit Afrika',
75             'description': 'md5:52cf4eefbc96fffcbdc06d024147abea',
76             'upload_date': '20130225',
77             'duration': 3000,
78         },
79     }, {
80         'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706',
81         'info_dict': {
82             'id': 'WO_VPRO_043706',
83             'ext': 'm4v',
84             'title': 'De nieuwe mens - Deel 1',
85             'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
86             'duration': 4680,
87         },
88         'params': {
89             'skip_download': True,
90         }
91     }, {
92         # non asf in streams
93         'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
94         'info_dict': {
95             'id': 'WO_NOS_762771',
96             'ext': 'mp4',
97             'title': 'Hoe gaat Europa verder na Parijs?',
98         },
99         'params': {
100             'skip_download': True,
101         }
102     }, {
103         'url': 'http://www.ntr.nl/Aap-Poot-Pies/27/detail/Aap-poot-pies/VPWON_1233944#content',
104         'info_dict': {
105             'id': 'VPWON_1233944',
106             'ext': 'm4v',
107             'title': 'Aap, poot, pies',
108             'description': 'md5:c9c8005d1869ae65b858e82c01a91fde',
109             'upload_date': '20150508',
110             'duration': 599,
111         },
112         'params': {
113             'skip_download': True,
114         }
115     }, {
116         'url': 'http://www.omroepwnl.nl/video/fragment/vandaag-de-dag-verkiezingen__POMS_WNL_853698',
117         'info_dict': {
118             'id': 'POW_00996502',
119             'ext': 'm4v',
120             'title': '''"Dit is wel een 'landslide'..."''',
121             'description': 'md5:f8d66d537dfb641380226e31ca57b8e8',
122             'upload_date': '20150508',
123             'duration': 462,
124         },
125         'params': {
126             'skip_download': True,
127         }
128     }, {
129         # audio
130         'url': 'http://www.npo.nl/jouw-stad-rotterdam/29-01-2017/RBX_FUNX_6683215/RBX_FUNX_7601437',
131         'info_dict': {
132             'id': 'RBX_FUNX_6683215',
133             'ext': 'mp3',
134             'title': 'Jouw Stad Rotterdam',
135             'description': 'md5:db251505244f097717ec59fabc372d9f',
136         },
137         'params': {
138             'skip_download': True,
139         }
140     }, {
141         'url': 'http://www.zapp.nl/de-bzt-show/gemist/KN_1687547',
142         'only_matching': True,
143     }, {
144         'url': 'http://www.zapp.nl/de-bzt-show/filmpjes/POMS_KN_7315118',
145         'only_matching': True,
146     }, {
147         'url': 'http://www.zapp.nl/beste-vrienden-quiz/extra-video-s/WO_NTR_1067990',
148         'only_matching': True,
149     }, {
150         'url': 'https://www.npo3.nl/3onderzoekt/16-09-2015/VPWON_1239870',
151         'only_matching': True,
152     }, {
153         # live stream
154         'url': 'npo:LI_NL1_4188102',
155         'only_matching': True,
156     }, {
157         'url': 'http://www.npo.nl/radio-gaga/13-06-2017/BNN_101383373',
158         'only_matching': True,
159     }, {
160         'url': 'https://www.zapp.nl/1803-skelterlab/instructie-video-s/740-instructievideo-s/POMS_AT_11736927',
161         'only_matching': True,
162     }]
163
164     def _real_extract(self, url):
165         video_id = self._match_id(url)
166         return self._get_info(video_id)
167
168     def _get_info(self, video_id):
169         metadata = self._download_json(
170             'http://e.omroep.nl/metadata/%s' % video_id,
171             video_id,
172             # We have to remove the javascript callback
173             transform_source=strip_jsonp,
174         )
175
176         error = metadata.get('error')
177         if error:
178             raise ExtractorError(error, expected=True)
179
180         # For some videos actual video id (prid) is different (e.g. for
181         # http://www.omroepwnl.nl/video/fragment/vandaag-de-dag-verkiezingen__POMS_WNL_853698
182         # video id is POMS_WNL_853698 but prid is POW_00996502)
183         video_id = metadata.get('prid') or video_id
184
185         # titel is too generic in some cases so utilize aflevering_titel as well
186         # when available (e.g. http://tegenlicht.vpro.nl/afleveringen/2014-2015/access-to-africa.html)
187         title = metadata['titel']
188         sub_title = metadata.get('aflevering_titel')
189         if sub_title and sub_title != title:
190             title += ': %s' % sub_title
191
192         token = self._get_token(video_id)
193
194         formats = []
195         urls = set()
196
197         quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
198         items = self._download_json(
199             'http://ida.omroep.nl/app.php/%s' % video_id, video_id,
200             'Downloading formats JSON', query={
201                 'adaptive': 'yes',
202                 'token': token,
203             })['items'][0]
204         for num, item in enumerate(items):
205             item_url = item.get('url')
206             if not item_url or item_url in urls:
207                 continue
208             urls.add(item_url)
209             format_id = self._search_regex(
210                 r'video/ida/([^/]+)', item_url, 'format id',
211                 default=None)
212
213             def add_format_url(format_url):
214                 formats.append({
215                     'url': format_url,
216                     'format_id': format_id,
217                     'quality': quality(format_id),
218                 })
219
220             # Example: http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706
221             if item.get('contentType') in ('url', 'audio'):
222                 add_format_url(item_url)
223                 continue
224
225             try:
226                 stream_info = self._download_json(
227                     item_url + '&type=json', video_id,
228                     'Downloading %s stream JSON'
229                     % item.get('label') or item.get('format') or format_id or num)
230             except ExtractorError as ee:
231                 if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404:
232                     error = (self._parse_json(
233                         ee.cause.read().decode(), video_id,
234                         fatal=False) or {}).get('errorstring')
235                     if error:
236                         raise ExtractorError(error, expected=True)
237                 raise
238             # Stream URL instead of JSON, example: npo:LI_NL1_4188102
239             if isinstance(stream_info, compat_str):
240                 if not stream_info.startswith('http'):
241                     continue
242                 video_url = stream_info
243             # JSON
244             else:
245                 video_url = stream_info.get('url')
246             if not video_url or video_url in urls:
247                 continue
248             urls.add(item_url)
249             if determine_ext(video_url) == 'm3u8':
250                 formats.extend(self._extract_m3u8_formats(
251                     video_url, video_id, ext='mp4',
252                     entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
253             else:
254                 add_format_url(video_url)
255
256         is_live = metadata.get('medium') == 'live'
257
258         if not is_live:
259             for num, stream in enumerate(metadata.get('streams', [])):
260                 stream_url = stream.get('url')
261                 if not stream_url or stream_url in urls:
262                     continue
263                 urls.add(stream_url)
264                 # smooth streaming is not supported
265                 stream_type = stream.get('type', '').lower()
266                 if stream_type in ['ss', 'ms']:
267                     continue
268                 if stream_type == 'hds':
269                     f4m_formats = self._extract_f4m_formats(
270                         stream_url, video_id, fatal=False)
271                     # f4m downloader downloads only piece of live stream
272                     for f4m_format in f4m_formats:
273                         f4m_format['preference'] = -1
274                     formats.extend(f4m_formats)
275                 elif stream_type == 'hls':
276                     formats.extend(self._extract_m3u8_formats(
277                         stream_url, video_id, ext='mp4', fatal=False))
278                 # Example: http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706
279                 elif '.asf' in stream_url:
280                     asx = self._download_xml(
281                         stream_url, video_id,
282                         'Downloading stream %d ASX playlist' % num,
283                         transform_source=fix_xml_ampersands, fatal=False)
284                     if not asx:
285                         continue
286                     ref = asx.find('./ENTRY/Ref')
287                     if ref is None:
288                         continue
289                     video_url = ref.get('href')
290                     if not video_url or video_url in urls:
291                         continue
292                     urls.add(video_url)
293                     formats.append({
294                         'url': video_url,
295                         'ext': stream.get('formaat', 'asf'),
296                         'quality': stream.get('kwaliteit'),
297                         'preference': -10,
298                     })
299                 else:
300                     formats.append({
301                         'url': stream_url,
302                         'quality': stream.get('kwaliteit'),
303                     })
304
305         self._sort_formats(formats)
306
307         subtitles = {}
308         if metadata.get('tt888') == 'ja':
309             subtitles['nl'] = [{
310                 'ext': 'vtt',
311                 'url': 'http://tt888.omroep.nl/tt888/%s' % video_id,
312             }]
313
314         return {
315             'id': video_id,
316             'title': self._live_title(title) if is_live else title,
317             'description': metadata.get('info'),
318             'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
319             'upload_date': unified_strdate(metadata.get('gidsdatum')),
320             'duration': parse_duration(metadata.get('tijdsduur')),
321             'formats': formats,
322             'subtitles': subtitles,
323             'is_live': is_live,
324         }
325
326
327 class NPOLiveIE(NPOBaseIE):
328     IE_NAME = 'npo.nl:live'
329     _VALID_URL = r'https?://(?:www\.)?npo\.nl/live(?:/(?P<id>[^/?#&]+))?'
330
331     _TESTS = [{
332         'url': 'http://www.npo.nl/live/npo-1',
333         'info_dict': {
334             'id': 'LI_NL1_4188102',
335             'display_id': 'npo-1',
336             'ext': 'mp4',
337             'title': 're:^NPO 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
338             'is_live': True,
339         },
340         'params': {
341             'skip_download': True,
342         }
343     }, {
344         'url': 'http://www.npo.nl/live',
345         'only_matching': True,
346     }]
347
348     def _real_extract(self, url):
349         display_id = self._match_id(url) or 'npo-1'
350
351         webpage = self._download_webpage(url, display_id)
352
353         live_id = self._search_regex(
354             [r'media-id="([^"]+)"', r'data-prid="([^"]+)"'], webpage, 'live id')
355
356         return {
357             '_type': 'url_transparent',
358             'url': 'npo:%s' % live_id,
359             'ie_key': NPOIE.ie_key(),
360             'id': live_id,
361             'display_id': display_id,
362         }
363
364
365 class NPORadioIE(InfoExtractor):
366     IE_NAME = 'npo.nl:radio'
367     _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/(?P<id>[^/]+)/?$'
368
369     _TEST = {
370         'url': 'http://www.npo.nl/radio/radio-1',
371         'info_dict': {
372             'id': 'radio-1',
373             'ext': 'mp3',
374             'title': 're:^NPO Radio 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
375             'is_live': True,
376         },
377         'params': {
378             'skip_download': True,
379         }
380     }
381
382     @staticmethod
383     def _html_get_attribute_regex(attribute):
384         return r'{0}\s*=\s*\'([^\']+)\''.format(attribute)
385
386     def _real_extract(self, url):
387         video_id = self._match_id(url)
388
389         webpage = self._download_webpage(url, video_id)
390
391         title = self._html_search_regex(
392             self._html_get_attribute_regex('data-channel'), webpage, 'title')
393
394         stream = self._parse_json(
395             self._html_search_regex(self._html_get_attribute_regex('data-streams'), webpage, 'data-streams'),
396             video_id)
397
398         codec = stream.get('codec')
399
400         return {
401             'id': video_id,
402             'url': stream['url'],
403             'title': self._live_title(title),
404             'acodec': codec,
405             'ext': codec,
406             'is_live': True,
407         }
408
409
410 class NPORadioFragmentIE(InfoExtractor):
411     IE_NAME = 'npo.nl:radio:fragment'
412     _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/[^/]+/fragment/(?P<id>\d+)'
413
414     _TEST = {
415         'url': 'http://www.npo.nl/radio/radio-5/fragment/174356',
416         'md5': 'dd8cc470dad764d0fdc70a9a1e2d18c2',
417         'info_dict': {
418             'id': '174356',
419             'ext': 'mp3',
420             'title': 'Jubileumconcert Willeke Alberti',
421         },
422     }
423
424     def _real_extract(self, url):
425         audio_id = self._match_id(url)
426
427         webpage = self._download_webpage(url, audio_id)
428
429         title = self._html_search_regex(
430             r'href="/radio/[^/]+/fragment/%s" title="([^"]+)"' % audio_id,
431             webpage, 'title')
432
433         audio_url = self._search_regex(
434             r"data-streams='([^']+)'", webpage, 'audio url')
435
436         return {
437             'id': audio_id,
438             'url': audio_url,
439             'title': title,
440         }
441
442
443 class NPODataMidEmbedIE(InfoExtractor):
444     def _real_extract(self, url):
445         display_id = self._match_id(url)
446         webpage = self._download_webpage(url, display_id)
447         video_id = self._search_regex(
448             r'data-mid=(["\'])(?P<id>(?:(?!\1).)+)\1', webpage, 'video_id', group='id')
449         return {
450             '_type': 'url_transparent',
451             'ie_key': 'NPO',
452             'url': 'npo:%s' % video_id,
453             'display_id': display_id
454         }
455
456
457 class SchoolTVIE(NPODataMidEmbedIE):
458     IE_NAME = 'schooltv'
459     _VALID_URL = r'https?://(?:www\.)?schooltv\.nl/video/(?P<id>[^/?#&]+)'
460
461     _TEST = {
462         'url': 'http://www.schooltv.nl/video/ademhaling-de-hele-dag-haal-je-adem-maar-wat-gebeurt-er-dan-eigenlijk-in-je-lichaam/',
463         'info_dict': {
464             'id': 'WO_NTR_429477',
465             'display_id': 'ademhaling-de-hele-dag-haal-je-adem-maar-wat-gebeurt-er-dan-eigenlijk-in-je-lichaam',
466             'title': 'Ademhaling: De hele dag haal je adem. Maar wat gebeurt er dan eigenlijk in je lichaam?',
467             'ext': 'mp4',
468             'description': 'md5:abfa0ff690adb73fd0297fd033aaa631'
469         },
470         'params': {
471             # Skip because of m3u8 download
472             'skip_download': True
473         }
474     }
475
476
477 class HetKlokhuisIE(NPODataMidEmbedIE):
478     IE_NAME = 'hetklokhuis'
479     _VALID_URL = r'https?://(?:www\.)?hetklokhuis\.nl/[^/]+/\d+/(?P<id>[^/?#&]+)'
480
481     _TEST = {
482         'url': 'http://hetklokhuis.nl/tv-uitzending/3471/Zwaartekrachtsgolven',
483         'info_dict': {
484             'id': 'VPWON_1260528',
485             'display_id': 'Zwaartekrachtsgolven',
486             'ext': 'm4v',
487             'title': 'Het Klokhuis: Zwaartekrachtsgolven',
488             'description': 'md5:c94f31fb930d76c2efa4a4a71651dd48',
489             'upload_date': '20170223',
490         },
491         'params': {
492             'skip_download': True
493         }
494     }
495
496
497 class NPOPlaylistBaseIE(NPOIE):
498     def _real_extract(self, url):
499         playlist_id = self._match_id(url)
500
501         webpage = self._download_webpage(url, playlist_id)
502
503         entries = [
504             self.url_result('npo:%s' % video_id if not video_id.startswith('http') else video_id)
505             for video_id in orderedSet(re.findall(self._PLAYLIST_ENTRY_RE, webpage))
506         ]
507
508         playlist_title = self._html_search_regex(
509             self._PLAYLIST_TITLE_RE, webpage, 'playlist title',
510             default=None) or self._og_search_title(webpage)
511
512         return self.playlist_result(entries, playlist_id, playlist_title)
513
514
515 class VPROIE(NPOPlaylistBaseIE):
516     IE_NAME = 'vpro'
517     _VALID_URL = r'https?://(?:www\.)?(?:(?:tegenlicht\.)?vpro|2doc)\.nl/(?:[^/]+/)*(?P<id>[^/]+)\.html'
518     _PLAYLIST_TITLE_RE = (r'<h1[^>]+class=["\'].*?\bmedia-platform-title\b.*?["\'][^>]*>([^<]+)',
519                           r'<h5[^>]+class=["\'].*?\bmedia-platform-subtitle\b.*?["\'][^>]*>([^<]+)')
520     _PLAYLIST_ENTRY_RE = r'data-media-id="([^"]+)"'
521
522     _TESTS = [
523         {
524             'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
525             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
526             'info_dict': {
527                 'id': 'VPWON_1169289',
528                 'ext': 'm4v',
529                 'title': 'De toekomst komt uit Afrika',
530                 'description': 'md5:52cf4eefbc96fffcbdc06d024147abea',
531                 'upload_date': '20130225',
532             },
533             'skip': 'Video gone',
534         },
535         {
536             'url': 'http://www.vpro.nl/programmas/2doc/2015/sergio-herman.html',
537             'info_dict': {
538                 'id': 'sergio-herman',
539                 'title': 'sergio herman: fucking perfect',
540             },
541             'playlist_count': 2,
542         },
543         {
544             # playlist with youtube embed
545             'url': 'http://www.vpro.nl/programmas/2doc/2015/education-education.html',
546             'info_dict': {
547                 'id': 'education-education',
548                 'title': 'education education',
549             },
550             'playlist_count': 2,
551         },
552         {
553             'url': 'http://www.2doc.nl/documentaires/series/2doc/2015/oktober/de-tegenprestatie.html',
554             'info_dict': {
555                 'id': 'de-tegenprestatie',
556                 'title': 'De Tegenprestatie',
557             },
558             'playlist_count': 2,
559         }, {
560             'url': 'http://www.2doc.nl/speel~VARA_101375237~mh17-het-verdriet-van-nederland~.html',
561             'info_dict': {
562                 'id': 'VARA_101375237',
563                 'ext': 'm4v',
564                 'title': 'MH17: Het verdriet van Nederland',
565                 'description': 'md5:09e1a37c1fdb144621e22479691a9f18',
566                 'upload_date': '20150716',
567             },
568             'params': {
569                 # Skip because of m3u8 download
570                 'skip_download': True
571             },
572         }
573     ]
574
575
576 class WNLIE(NPOPlaylistBaseIE):
577     IE_NAME = 'wnl'
578     _VALID_URL = r'https?://(?:www\.)?omroepwnl\.nl/video/detail/(?P<id>[^/]+)__\d+'
579     _PLAYLIST_TITLE_RE = r'(?s)<h1[^>]+class="subject"[^>]*>(.+?)</h1>'
580     _PLAYLIST_ENTRY_RE = r'<a[^>]+href="([^"]+)"[^>]+class="js-mid"[^>]*>Deel \d+'
581
582     _TESTS = [{
583         'url': 'http://www.omroepwnl.nl/video/detail/vandaag-de-dag-6-mei__060515',
584         'info_dict': {
585             'id': 'vandaag-de-dag-6-mei',
586             'title': 'Vandaag de Dag 6 mei',
587         },
588         'playlist_count': 4,
589     }]
590
591
592 class AndereTijdenIE(NPOPlaylistBaseIE):
593     IE_NAME = 'anderetijden'
594     _VALID_URL = r'https?://(?:www\.)?anderetijden\.nl/programma/(?:[^/]+/)+(?P<id>[^/?#&]+)'
595     _PLAYLIST_TITLE_RE = r'(?s)<h1[^>]+class=["\'].*?\bpage-title\b.*?["\'][^>]*>(.+?)</h1>'
596     _PLAYLIST_ENTRY_RE = r'<figure[^>]+class=["\']episode-container episode-page["\'][^>]+data-prid=["\'](.+?)["\']'
597
598     _TESTS = [{
599         'url': 'http://anderetijden.nl/programma/1/Andere-Tijden/aflevering/676/Duitse-soldaten-over-de-Slag-bij-Arnhem',
600         'info_dict': {
601             'id': 'Duitse-soldaten-over-de-Slag-bij-Arnhem',
602             'title': 'Duitse soldaten over de Slag bij Arnhem',
603         },
604         'playlist_count': 3,
605     }]