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