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