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