[npo] Remove unused imports
[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     parse_duration,
9     qualities,
10     strip_jsonp,
11     unified_strdate,
12 )
13
14
15 class NPOBaseIE(InfoExtractor):
16     def _get_token(self, video_id):
17         token_page = self._download_webpage(
18             'http://ida.omroep.nl/npoplayer/i.js',
19             video_id, note='Downloading token')
20         token = self._search_regex(
21             r'npoplayer\.token = "(.+?)"', token_page, 'token')
22         # Decryption algorithm extracted from http://npoplayer.omroep.nl/csjs/npoplayer-min.js
23         token_l = list(token)
24         first = second = None
25         for i in range(5, len(token_l) - 4):
26             if token_l[i].isdigit():
27                 if first is None:
28                     first = i
29                 elif second is None:
30                     second = i
31         if first is None or second is None:
32             first = 12
33             second = 13
34
35         token_l[first], token_l[second] = token_l[second], token_l[first]
36
37         return ''.join(token_l)
38
39
40 class NPOIE(NPOBaseIE):
41     IE_NAME = 'npo'
42     IE_DESC = 'npo.nl and ntr.nl'
43     _VALID_URL = r'''(?x)
44                     (?:
45                         npo:|
46                         https?://
47                             (?:www\.)?
48                             (?:
49                                 npo\.nl/(?!live|radio)(?:[^/]+/){2}|
50                                 ntr\.nl/(?:[^/]+/){2,}|
51                                 omroepwnl\.nl/video/fragment/[^/]+__
52                             )
53                         )
54                         (?P<id>[^/?#]+)
55                 '''
56
57     _TESTS = [
58         {
59             'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
60             'md5': '4b3f9c429157ec4775f2c9cb7b911016',
61             'info_dict': {
62                 'id': 'VPWON_1220719',
63                 'ext': 'm4v',
64                 'title': 'Nieuwsuur',
65                 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
66                 'upload_date': '20140622',
67             },
68         },
69         {
70             'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
71             'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
72             'info_dict': {
73                 'id': 'VARA_101191800',
74                 'ext': 'm4v',
75                 'title': 'De Mega Mike & Mega Thomas show',
76                 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
77                 'upload_date': '20090227',
78                 'duration': 2400,
79             },
80         },
81         {
82             'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
83             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
84             'info_dict': {
85                 'id': 'VPWON_1169289',
86                 'ext': 'm4v',
87                 'title': 'Tegenlicht',
88                 'description': 'md5:52cf4eefbc96fffcbdc06d024147abea',
89                 'upload_date': '20130225',
90                 'duration': 3000,
91             },
92         },
93         {
94             'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706',
95             'info_dict': {
96                 'id': 'WO_VPRO_043706',
97                 'ext': 'wmv',
98                 'title': 'De nieuwe mens - Deel 1',
99                 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
100                 'duration': 4680,
101             },
102             'params': {
103                 # mplayer mms download
104                 'skip_download': True,
105             }
106         },
107         # non asf in streams
108         {
109             'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
110             'md5': 'b3da13de374cbe2d5332a7e910bef97f',
111             'info_dict': {
112                 'id': 'WO_NOS_762771',
113                 'ext': 'mp4',
114                 'title': 'Hoe gaat Europa verder na Parijs?',
115             },
116         },
117         {
118             'url': 'http://www.ntr.nl/Aap-Poot-Pies/27/detail/Aap-poot-pies/VPWON_1233944#content',
119             'md5': '01c6a2841675995da1f0cf776f03a9c3',
120             'info_dict': {
121                 'id': 'VPWON_1233944',
122                 'ext': 'm4v',
123                 'title': 'Aap, poot, pies',
124                 'description': 'md5:c9c8005d1869ae65b858e82c01a91fde',
125                 'upload_date': '20150508',
126                 'duration': 599,
127             },
128         },
129         {
130             'url': 'http://www.omroepwnl.nl/video/fragment/vandaag-de-dag-verkiezingen__POMS_WNL_853698',
131             'md5': 'd30cd8417b8b9bca1fdff27428860d08',
132             'info_dict': {
133                 'id': 'POW_00996502',
134                 'ext': 'm4v',
135                 'title': '''"Dit is wel een 'landslide'..."''',
136                 'description': 'md5:f8d66d537dfb641380226e31ca57b8e8',
137                 'upload_date': '20150508',
138                 'duration': 462,
139             },
140         }
141     ]
142
143     def _real_extract(self, url):
144         video_id = self._match_id(url)
145         return self._get_info(video_id)
146
147     def _get_info(self, video_id):
148         metadata = self._download_json(
149             'http://e.omroep.nl/metadata/%s' % video_id,
150             video_id,
151             # We have to remove the javascript callback
152             transform_source=strip_jsonp,
153         )
154
155         # For some videos actual video id (prid) is different (e.g. for
156         # http://www.omroepwnl.nl/video/fragment/vandaag-de-dag-verkiezingen__POMS_WNL_853698
157         # video id is POMS_WNL_853698 but prid is POW_00996502)
158         video_id = metadata.get('prid') or video_id
159
160         token = self._get_token(video_id)
161
162         formats = []
163
164         pubopties = metadata.get('pubopties')
165         if pubopties:
166             quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
167             for format_id in pubopties:
168                 format_info = self._download_json(
169                     'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s'
170                     % (video_id, format_id, token),
171                     video_id, 'Downloading %s JSON' % format_id)
172                 if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
173                     continue
174                 streams = format_info.get('streams')
175                 if streams:
176                     video_info = self._download_json(
177                         streams[0] + '&type=json',
178                         video_id, 'Downloading %s stream JSON' % format_id)
179                 else:
180                     video_info = format_info
181                 video_url = video_info.get('url')
182                 if not video_url:
183                     continue
184                 if format_id == 'adaptive':
185                     formats.extend(self._extract_m3u8_formats(video_url, video_id))
186                 else:
187                     formats.append({
188                         'url': video_url,
189                         'format_id': format_id,
190                         'quality': quality(format_id),
191                     })
192
193         streams = metadata.get('streams')
194         if streams:
195             for i, stream in enumerate(streams):
196                 stream_url = stream.get('url')
197                 if not stream_url:
198                     continue
199                 if '.asf' not in stream_url:
200                     formats.append({
201                         'url': stream_url,
202                         'quality': stream.get('kwaliteit'),
203                     })
204                     continue
205                 asx = self._download_xml(
206                     stream_url, video_id,
207                     'Downloading stream %d ASX playlist' % i,
208                     transform_source=fix_xml_ampersands)
209                 ref = asx.find('./ENTRY/Ref')
210                 if ref is None:
211                     continue
212                 video_url = ref.get('href')
213                 if not video_url:
214                     continue
215                 formats.append({
216                     'url': video_url,
217                     'ext': stream.get('formaat', 'asf'),
218                     'quality': stream.get('kwaliteit'),
219                 })
220
221         self._sort_formats(formats)
222
223         subtitles = {}
224         if metadata.get('tt888') == 'ja':
225             subtitles['nl'] = [{
226                 'ext': 'vtt',
227                 'url': 'http://e.omroep.nl/tt888/%s' % video_id,
228             }]
229
230         return {
231             'id': video_id,
232             # prefer aflevering_titel if any since titel may be too generic, e.g.
233             # http://tegenlicht.vpro.nl/afleveringen/2014-2015/access-to-africa.html
234             'title': metadata.get('aflevering_titel') or metadata['titel'],
235             'description': metadata.get('info'),
236             'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
237             'upload_date': unified_strdate(metadata.get('gidsdatum')),
238             'duration': parse_duration(metadata.get('tijdsduur')),
239             'formats': formats,
240             'subtitles': subtitles,
241         }
242
243
244 class NPOLiveIE(NPOBaseIE):
245     IE_NAME = 'npo.nl:live'
246     _VALID_URL = r'https?://(?:www\.)?npo\.nl/live/(?P<id>.+)'
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()
283                 # smooth streaming is not supported
284                 if stream_type in ['ss', 'ms']:
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,
295                     'Unable to download %s URL' % stream_type,
296                     transform_source=strip_jsonp, fatal=False)
297                 if not stream_url:
298                     continue
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,
310                         'preference': -10,
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
326 class 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
371 class 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
404 class VPROIE(NPOIE):
405     _VALID_URL = r'https?://(?:www\.)?(?:tegenlicht\.)?vpro\.nl/(?:[^/]+/){2,}(?P<id>[^/]+)\.html'
406
407     _TESTS = [
408         {
409             'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
410             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
411             'info_dict': {
412                 'id': 'VPWON_1169289',
413                 'ext': 'm4v',
414                 'title': 'De toekomst komt uit Afrika',
415                 'description': 'md5:52cf4eefbc96fffcbdc06d024147abea',
416                 'upload_date': '20130225',
417             },
418         },
419         {
420             'url': 'http://www.vpro.nl/programmas/2doc/2015/sergio-herman.html',
421             'info_dict': {
422                 'id': 'sergio-herman',
423                 'title': 'Sergio Herman: Fucking perfect',
424             },
425             'playlist_count': 2,
426         },
427         {
428             # playlist with youtube embed
429             'url': 'http://www.vpro.nl/programmas/2doc/2015/education-education.html',
430             'info_dict': {
431                 'id': 'education-education',
432                 'title': '2Doc',
433             },
434             'playlist_count': 2,
435         }
436     ]
437
438     def _real_extract(self, url):
439         playlist_id = self._match_id(url)
440
441         webpage = self._download_webpage(url, playlist_id)
442
443         entries = [
444             self.url_result('npo:%s' % video_id if not video_id.startswith('http') else video_id)
445             for video_id in re.findall(r'data-media-id="([^"]+)"', webpage)
446         ]
447
448         playlist_title = self._search_regex(
449             r'<title>\s*([^>]+?)\s*-\s*Teledoc\s*-\s*VPRO\s*</title>',
450             webpage, 'playlist title', default=None) or self._og_search_title(webpage)
451
452         return self.playlist_result(entries, playlist_id, playlist_title)
453
454
455 class WNLIE(InfoExtractor):
456     _VALID_URL = r'https?://(?:www\.)?omroepwnl\.nl/video/detail/(?P<id>[^/]+)__\d+'
457
458     _TEST = {
459         'url': 'http://www.omroepwnl.nl/video/detail/vandaag-de-dag-6-mei__060515',
460         'info_dict': {
461             'id': 'vandaag-de-dag-6-mei',
462             'title': 'Vandaag de Dag 6 mei',
463         },
464         'playlist_count': 4,
465     }
466
467     def _real_extract(self, url):
468         playlist_id = self._match_id(url)
469
470         webpage = self._download_webpage(url, playlist_id)
471
472         entries = [
473             self.url_result('npo:%s' % video_id, 'NPO')
474             for video_id, part in re.findall(
475                 r'<a[^>]+href="([^"]+)"[^>]+class="js-mid"[^>]*>(Deel \d+)', webpage)
476         ]
477
478         playlist_title = self._html_search_regex(
479             r'(?s)<h1[^>]+class="subject"[^>]*>(.+?)</h1>',
480             webpage, 'playlist title')
481
482         return self.playlist_result(entries, playlist_id, playlist_title)