1 from __future__ 
import unicode_literals
 
   3 from .common 
import InfoExtractor
 
  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         return self
._search
_regex
( 
  20             r
'npoplayer\.token = "(.+?)"', token_page
, 'token') 
  23 class NPOIE(NPOBaseIE
): 
  25     _VALID_URL 
= r
'https?://(?:www\.)?npo\.nl/(?!live|radio)[^/]+/[^/]+/(?P<id>[^/?]+)' 
  29             'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719', 
  30             'md5': '4b3f9c429157ec4775f2c9cb7b911016', 
  32                 'id': 'VPWON_1220719', 
  35                 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.', 
  36                 'upload_date': '20140622', 
  40             'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800', 
  41             'md5': 'da50a5787dbfc1603c4ad80f31c5120b', 
  43                 'id': 'VARA_101191800', 
  45                 'title': 'De Mega Mike & Mega Thomas show', 
  46                 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4', 
  47                 'upload_date': '20090227', 
  52             'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289', 
  53             'md5': 'f8065e4e5a7824068ed3c7e783178f2c', 
  55                 'id': 'VPWON_1169289', 
  57                 'title': 'Tegenlicht', 
  58                 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1', 
  59                 'upload_date': '20130225', 
  64             'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706', 
  66                 'id': 'WO_VPRO_043706', 
  68                 'title': 'De nieuwe mens - Deel 1', 
  69                 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b', 
  73                 # mplayer mms download 
  74                 'skip_download': True, 
  79             'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771', 
  80             'md5': 'b3da13de374cbe2d5332a7e910bef97f', 
  82                 'id': 'WO_NOS_762771', 
  84                 'title': 'Hoe gaat Europa verder na Parijs?', 
  89     def _real_extract(self
, url
): 
  90         video_id 
= self
._match
_id
(url
) 
  91         return self
._get
_info
(video_id
) 
  93     def _get_info(self
, video_id
): 
  94         metadata 
= self
._download
_json
( 
  95             'http://e.omroep.nl/metadata/aflevering/%s' % video_id
, 
  97             # We have to remove the javascript callback 
  98             transform_source
=strip_jsonp
, 
 101         token 
= self
._get
_token
(video_id
) 
 105         pubopties 
= metadata
.get('pubopties') 
 107             quality 
= qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std']) 
 108             for format_id 
in pubopties
: 
 109                 format_info 
= self
._download
_json
( 
 110                     'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s' 
 111                     % (video_id
, format_id
, token
), 
 112                     video_id
, 'Downloading %s JSON' % format_id
) 
 113                 if format_info
.get('error_code', 0) or format_info
.get('errorcode', 0): 
 115                 streams 
= format_info
.get('streams') 
 117                     video_info 
= self
._download
_json
( 
 118                         streams
[0] + '&type=json', 
 119                         video_id
, 'Downloading %s stream JSON' % format_id
) 
 121                     video_info 
= format_info
 
 122                 video_url 
= video_info
.get('url') 
 125                 if format_id 
== 'adaptive': 
 126                     formats
.extend(self
._extract
_m
3u8_formats
(video_url
, video_id
)) 
 130                         'format_id': format_id
, 
 131                         'quality': quality(format_id
), 
 134         streams 
= metadata
.get('streams') 
 136             for i
, stream 
in enumerate(streams
): 
 137                 stream_url 
= stream
.get('url') 
 140                 if '.asf' not in stream_url
: 
 143                         'quality': stream
.get('kwaliteit'), 
 146                 asx 
= self
._download
_xml
( 
 147                     stream_url
, video_id
, 
 148                     'Downloading stream %d ASX playlist' % i
, 
 149                     transform_source
=fix_xml_ampersands
) 
 150                 ref 
= asx
.find('./ENTRY/Ref') 
 153                 video_url 
= ref
.get('href') 
 158                     'ext': stream
.get('formaat', 'asf'), 
 159                     'quality': stream
.get('kwaliteit'), 
 162         self
._sort
_formats
(formats
) 
 165         if metadata
.get('tt888') == 'ja': 
 168                 'url': 'http://e.omroep.nl/tt888/%s' % video_id
, 
 173             'title': metadata
['titel'], 
 174             'description': metadata
['info'], 
 175             'thumbnail': metadata
.get('images', [{'url': None}])[-1]['url'], 
 176             'upload_date': unified_strdate(metadata
.get('gidsdatum')), 
 177             'duration': parse_duration(metadata
.get('tijdsduur')), 
 179             'subtitles': subtitles
, 
 183 class NPOLiveIE(NPOBaseIE
): 
 184     IE_NAME 
= 'npo.nl:live' 
 185     _VALID_URL 
= r
'https?://(?:www\.)?npo\.nl/live/(?P<id>.+)' 
 188         'url': 'http://www.npo.nl/live/npo-1', 
 190             'id': 'LI_NEDERLAND1_136692', 
 191             'display_id': 'npo-1', 
 193             'title': 're:^Nederland 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
 194             'description': 'Livestream', 
 198             'skip_download': True, 
 202     def _real_extract(self
, url
): 
 203         display_id 
= self
._match
_id
(url
) 
 205         webpage 
= self
._download
_webpage
(url
, display_id
) 
 207         live_id 
= self
._search
_regex
( 
 208             r
'data-prid="([^"]+)"', webpage
, 'live id') 
 210         metadata 
= self
._download
_json
( 
 211             'http://e.omroep.nl/metadata/%s' % live_id
, 
 212             display_id
, transform_source
=strip_jsonp
) 
 214         token 
= self
._get
_token
(display_id
) 
 218         streams 
= metadata
.get('streams') 
 220             for stream 
in streams
: 
 221                 stream_type 
= stream
.get('type').lower() 
 222                 # smooth streaming is not supported 
 223                 if stream_type 
in ['ss', 'ms']: 
 225                 stream_info 
= self
._download
_json
( 
 226                     'http://ida.omroep.nl/aapi/?stream=%s&token=%s&type=jsonp' 
 227                     % (stream
.get('url'), token
), 
 228                     display_id
, 'Downloading %s JSON' % stream_type
) 
 229                 if stream_info
.get('error_code', 0) or stream_info
.get('errorcode', 0): 
 231                 stream_url 
= self
._download
_json
( 
 232                     stream_info
['stream'], display_id
, 
 233                     'Downloading %s URL' % stream_type
, 
 234                     'Unable to download %s URL' % stream_type
, 
 235                     transform_source
=strip_jsonp
, fatal
=False) 
 238                 if stream_type 
== 'hds': 
 239                     f4m_formats 
= self
._extract
_f
4m
_formats
(stream_url
, display_id
) 
 240                     # f4m downloader downloads only piece of live stream 
 241                     for f4m_format 
in f4m_formats
: 
 242                         f4m_format
['preference'] = -1 
 243                     formats
.extend(f4m_formats
) 
 244                 elif stream_type 
== 'hls': 
 245                     formats
.extend(self
._extract
_m
3u8_formats
(stream_url
, display_id
, 'mp4')) 
 252         self
._sort
_formats
(formats
) 
 256             'display_id': display_id
, 
 257             'title': self
._live
_title
(metadata
['titel']), 
 258             'description': metadata
['info'], 
 259             'thumbnail': metadata
.get('images', [{'url': None}])[-1]['url'], 
 265 class NPORadioIE(InfoExtractor
): 
 266     IE_NAME 
= 'npo.nl:radio' 
 267     _VALID_URL 
= r
'https?://(?:www\.)?npo\.nl/radio/(?P<id>[^/]+)/?$' 
 270         'url': 'http://www.npo.nl/radio/radio-1', 
 274             'title': 're:^NPO Radio 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
 278             'skip_download': True, 
 283     def _html_get_attribute_regex(attribute
): 
 284         return r
'{0}\s*=\s*\'([^
\']+)\''.format(attribute) 
 286     def _real_extract(self, url): 
 287         video_id = self._match_id(url) 
 289         webpage = self._download_webpage(url, video_id) 
 291         title = self._html_search_regex( 
 292             self._html_get_attribute_regex('data
-channel
'), webpage, 'title
') 
 294         stream = self._parse_json( 
 295             self._html_search_regex(self._html_get_attribute_regex('data
-streams
'), webpage, 'data
-streams
'), 
 298         codec = stream.get('codec
') 
 302             'url
': stream['url
'], 
 303             'title
': self._live_title(title), 
 310 class NPORadioFragmentIE(InfoExtractor): 
 311     IE_NAME = 'npo
.nl
:radio
:fragment
' 
 312     _VALID_URL = r'https?
://(?
:www\
.)?npo\
.nl
/radio
/[^
/]+/fragment
/(?P
<id>\d
+)' 
 315         'url
': 'http
://www
.npo
.nl
/radio
/radio
-5/fragment
/174356', 
 316         'md5
': 'dd8cc470dad764d0fdc70a9a1e2d18c2
', 
 320             'title
': 'Jubileumconcert Willeke Alberti
', 
 324     def _real_extract(self, url): 
 325         audio_id = self._match_id(url) 
 327         webpage = self._download_webpage(url, audio_id) 
 329         title = self._html_search_regex( 
 330             r'href
="/radio/[^/]+/fragment/%s" title
="([^"]+)"' % audio_id, 
 333         audio_url = self._search_regex( 
 334             r"data
-streams
='([^']+)'", webpage, 'audio url
') 
 343 class TegenlichtVproIE(NPOIE): 
 344     IE_NAME = 'tegenlicht
.vpro
.nl
' 
 345     _VALID_URL = r'https?
://tegenlicht\
.vpro\
.nl
/afleveringen
/.*?
' 
 349             'url
': 'http
://tegenlicht
.vpro
.nl
/afleveringen
/2012-2013/de
-toekomst
-komt
-uit
-afrika
.html
', 
 350             'md5
': 'f8065e4e5a7824068ed3c7e783178f2c
', 
 352                 'id': 'VPWON_1169289
', 
 354                 'title
': 'Tegenlicht
', 
 355                 'description
': 'md5
:d6476bceb17a8c103c76c3b708f05dd1
', 
 356                 'upload_date
': '20130225', 
 361     def _real_extract(self, url): 
 362         name = url_basename(url) 
 363         webpage = self._download_webpage(url, name) 
 364         urn = self._html_search_meta('mediaurn
', webpage) 
 365         info_page = self._download_json( 
 366             'http
://rs
.vpro
.nl
/v2
/api
/media
/%s.json
' % urn, name) 
 367         return self._get_info(info_page['mid
'])