1 from __future__ 
import unicode_literals
 
   5 from .common 
import InfoExtractor
 
   6 from ..compat 
import compat_HTTPError
 
  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 
  28         for i 
in range(5, len(token_l
) - 4): 
  29             if token_l
[i
].isdigit(): 
  34         if first 
is None or second 
is None: 
  38         token_l
[first
], token_l
[second
] = token_l
[second
], token_l
[first
] 
  40         return ''.join(token_l
) 
  43 class NPOIE(NPOBaseIE
): 
  45     IE_DESC 
= 'npo.nl and ntr.nl' 
  52                                 npo\.nl/(?!live|radio)(?:[^/]+/){2}| 
  53                                 ntr\.nl/(?:[^/]+/){2,}| 
  54                                 omroepwnl\.nl/video/fragment/[^/]+__ 
  62             'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719', 
  63             'md5': '4b3f9c429157ec4775f2c9cb7b911016', 
  65                 'id': 'VPWON_1220719', 
  68                 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.', 
  69                 'upload_date': '20140622', 
  73             'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800', 
  74             'md5': 'da50a5787dbfc1603c4ad80f31c5120b', 
  76                 'id': 'VARA_101191800', 
  78                 'title': 'De Mega Mike & Mega Thomas show: The best of.', 
  79                 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4', 
  80                 'upload_date': '20090227', 
  85             'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289', 
  86             'md5': 'f8065e4e5a7824068ed3c7e783178f2c', 
  88                 'id': 'VPWON_1169289', 
  90                 'title': 'Tegenlicht: De toekomst komt uit Afrika', 
  91                 'description': 'md5:52cf4eefbc96fffcbdc06d024147abea', 
  92                 'upload_date': '20130225', 
  97             'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706', 
  99                 'id': 'WO_VPRO_043706', 
 101                 'title': 'De nieuwe mens - Deel 1', 
 102                 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b', 
 106                 # mplayer mms download 
 107                 'skip_download': True, 
 112             'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771', 
 113             'md5': 'b3da13de374cbe2d5332a7e910bef97f', 
 115                 'id': 'WO_NOS_762771', 
 117                 'title': 'Hoe gaat Europa verder na Parijs?', 
 121             'url': 'http://www.ntr.nl/Aap-Poot-Pies/27/detail/Aap-poot-pies/VPWON_1233944#content', 
 122             'md5': '01c6a2841675995da1f0cf776f03a9c3', 
 124                 'id': 'VPWON_1233944', 
 126                 'title': 'Aap, poot, pies', 
 127                 'description': 'md5:c9c8005d1869ae65b858e82c01a91fde', 
 128                 'upload_date': '20150508', 
 133             'url': 'http://www.omroepwnl.nl/video/fragment/vandaag-de-dag-verkiezingen__POMS_WNL_853698', 
 134             'md5': 'd30cd8417b8b9bca1fdff27428860d08', 
 136                 'id': 'POW_00996502', 
 138                 'title': '''"Dit is wel een 'landslide'..."''', 
 139                 'description': 'md5:f8d66d537dfb641380226e31ca57b8e8', 
 140                 'upload_date': '20150508', 
 146     def _real_extract(self
, url
): 
 147         video_id 
= self
._match
_id
(url
) 
 148         return self
._get
_info
(video_id
) 
 150     def _get_info(self
, video_id
): 
 151         metadata 
= self
._download
_json
( 
 152             'http://e.omroep.nl/metadata/%s' % video_id
, 
 154             # We have to remove the javascript callback 
 155             transform_source
=strip_jsonp
, 
 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
 
 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
 
 170         token 
= self
._get
_token
(video_id
) 
 174         pubopties 
= metadata
.get('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): 
 184                 streams 
= format_info
.get('streams') 
 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') 
 194                                 raise ExtractorError(error
, expected
=True) 
 197                     video_info 
= format_info
 
 198                 video_url 
= video_info
.get('url') 
 201                 if format_id 
== 'adaptive': 
 202                     formats
.extend(self
._extract
_m
3u8_formats
(video_url
, video_id
, 'mp4')) 
 206                         'format_id': format_id
, 
 207                         'quality': quality(format_id
), 
 210         streams 
= metadata
.get('streams') 
 212             for i
, stream 
in enumerate(streams
): 
 213                 stream_url 
= stream
.get('url') 
 216                 if '.asf' not in stream_url
: 
 219                         'quality': stream
.get('kwaliteit'), 
 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') 
 229                 video_url 
= ref
.get('href') 
 234                     'ext': stream
.get('formaat', 'asf'), 
 235                     'quality': stream
.get('kwaliteit'), 
 238         self
._sort
_formats
(formats
) 
 241         if metadata
.get('tt888') == 'ja': 
 244                 'url': 'http://e.omroep.nl/tt888/%s' % video_id
, 
 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')), 
 255             'subtitles': subtitles
, 
 259 class NPOLiveIE(NPOBaseIE
): 
 260     IE_NAME 
= 'npo.nl:live' 
 261     _VALID_URL 
= r
'https?://(?:www\.)?npo\.nl/live/(?P<id>.+)' 
 264         'url': 'http://www.npo.nl/live/npo-1', 
 266             'id': 'LI_NEDERLAND1_136692', 
 267             'display_id': 'npo-1', 
 269             'title': 're:^Nederland 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
 270             'description': 'Livestream', 
 274             'skip_download': True, 
 278     def _real_extract(self
, url
): 
 279         display_id 
= self
._match
_id
(url
) 
 281         webpage 
= self
._download
_webpage
(url
, display_id
) 
 283         live_id 
= self
._search
_regex
( 
 284             r
'data-prid="([^"]+)"', webpage
, 'live id') 
 286         metadata 
= self
._download
_json
( 
 287             'http://e.omroep.nl/metadata/%s' % live_id
, 
 288             display_id
, transform_source
=strip_jsonp
) 
 290         token 
= self
._get
_token
(display_id
) 
 294         streams 
= metadata
.get('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']: 
 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): 
 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) 
 314                 if stream_type 
== 'hds': 
 315                     f4m_formats 
= self
._extract
_f
4m
_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
_m
3u8_formats
(stream_url
, display_id
, 'mp4')) 
 328         self
._sort
_formats
(formats
) 
 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'], 
 341 class NPORadioIE(InfoExtractor
): 
 342     IE_NAME 
= 'npo.nl:radio' 
 343     _VALID_URL 
= r
'https?://(?:www\.)?npo\.nl/radio/(?P<id>[^/]+)/?$' 
 346         'url': 'http://www.npo.nl/radio/radio-1', 
 350             'title': 're:^NPO Radio 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
 354             'skip_download': True, 
 359     def _html_get_attribute_regex(attribute
): 
 360         return r
'{0}\s*=\s*\'([^
\']+)\''.format(attribute) 
 362     def _real_extract(self, url): 
 363         video_id = self._match_id(url) 
 365         webpage = self._download_webpage(url, video_id) 
 367         title = self._html_search_regex( 
 368             self._html_get_attribute_regex('data
-channel
'), webpage, 'title
') 
 370         stream = self._parse_json( 
 371             self._html_search_regex(self._html_get_attribute_regex('data
-streams
'), webpage, 'data
-streams
'), 
 374         codec = stream.get('codec
') 
 378             'url
': stream['url
'], 
 379             'title
': self._live_title(title), 
 386 class NPORadioFragmentIE(InfoExtractor): 
 387     IE_NAME = 'npo
.nl
:radio
:fragment
' 
 388     _VALID_URL = r'https?
://(?
:www\
.)?npo\
.nl
/radio
/[^
/]+/fragment
/(?P
<id>\d
+)' 
 391         'url
': 'http
://www
.npo
.nl
/radio
/radio
-5/fragment
/174356', 
 392         'md5
': 'dd8cc470dad764d0fdc70a9a1e2d18c2
', 
 396             'title
': 'Jubileumconcert Willeke Alberti
', 
 400     def _real_extract(self, url): 
 401         audio_id = self._match_id(url) 
 403         webpage = self._download_webpage(url, audio_id) 
 405         title = self._html_search_regex( 
 406             r'href
="/radio/[^/]+/fragment/%s" title
="([^"]+)"' % audio_id, 
 409         audio_url = self._search_regex( 
 410             r"data
-streams
='([^']+)'", webpage, 'audio url
') 
 419 class SchoolTVIE(InfoExtractor): 
 421     _VALID_URL = r'https?
://(?
:www\
.)?schooltv\
.nl
/video
/(?P
<id>[^
/?
#&]+)' 
 424         'url': 'http://www.schooltv.nl/video/ademhaling-de-hele-dag-haal-je-adem-maar-wat-gebeurt-er-dan-eigenlijk-in-je-lichaam/', 
 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?', 
 430             'description': 'md5:abfa0ff690adb73fd0297fd033aaa631' 
 433             # Skip because of m3u8 download 
 434             'skip_download': True 
 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') 
 444             '_type
': 'url_transparent
', 
 446             'url
': 'npo
:%s' % video_id, 
 447             'display_id
': display_id 
 451 class NPOPlaylistBaseIE(NPOIE): 
 452     def _real_extract(self, url): 
 453         playlist_id = self._match_id(url) 
 455         webpage = self._download_webpage(url, playlist_id) 
 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)) 
 462         playlist_title = self._html_search_regex( 
 463             self._PLAYLIST_TITLE_RE, webpage, 'playlist title
', 
 464             default=None) or self._og_search_title(webpage) 
 466         return self.playlist_result(entries, playlist_id, playlist_title) 
 469 class VPROIE(NPOPlaylistBaseIE): 
 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="([^"]+)"' 
 478             'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html', 
 479             'md5': 'f8065e4e5a7824068ed3c7e783178f2c', 
 481                 'id': 'VPWON_1169289', 
 483                 'title': 'De toekomst komt uit Afrika', 
 484                 'description': 'md5:52cf4eefbc96fffcbdc06d024147abea', 
 485                 'upload_date': '20130225', 
 487             'skip': 'Video gone', 
 490             'url': 'http://www.vpro.nl/programmas/2doc/2015/sergio-herman.html', 
 492                 'id': 'sergio-herman', 
 493                 'title': 'sergio herman: fucking perfect', 
 498             # playlist with youtube embed 
 499             'url': 'http://www.vpro.nl/programmas/2doc/2015/education-education.html', 
 501                 'id': 'education-education', 
 502                 'title': 'education education', 
 507             'url': 'http://www.2doc.nl/documentaires/series/2doc/2015/oktober/de-tegenprestatie.html', 
 509                 'id': 'de-tegenprestatie', 
 510                 'title': 'De Tegenprestatie', 
 514             'url': 'http://www.2doc.nl/speel~VARA_101375237~mh17-het-verdriet-van-nederland~.html', 
 516                 'id': 'VARA_101375237', 
 518                 'title': 'MH17: Het verdriet van Nederland', 
 519                 'description': 'md5:09e1a37c1fdb144621e22479691a9f18', 
 520                 'upload_date': '20150716', 
 523                 # Skip because of m3u8 download 
 524                 'skip_download': True 
 530 class WNLIE(NPOPlaylistBaseIE): 
 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
+' 
 537         'url
': 'http
://www
.omroepwnl
.nl
/video
/detail
/vandaag
-de
-dag
-6-mei__060515
', 
 539             'id': 'vandaag
-de
-dag
-6-mei
', 
 540             'title
': 'Vandaag de Dag 
6 mei
', 
 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=["\'](.+?
)["\']' 
 553         'url': 'http://anderetijden.nl/programma/1/Andere-Tijden/aflevering/676/Duitse-soldaten-over-de-Slag-bij-Arnhem', 
 555             'id': 'Duitse-soldaten-over-de-Slag-bij-Arnhem', 
 556             'title': 'Duitse soldaten over de Slag bij Arnhem',