Merge remote-tracking branch 'h-collector/master'
[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         return self._search_regex(
20             r'npoplayer\.token = "(.+?)"', token_page, 'token')
21
22
23 class NPOIE(NPOBaseIE):
24     IE_NAME = 'npo.nl'
25     _VALID_URL = r'https?://www\.npo\.nl/[^/]+/[^/]+/(?P<id>[^/?]+)'
26
27     _TESTS = [
28         {
29             'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
30             'md5': '4b3f9c429157ec4775f2c9cb7b911016',
31             'info_dict': {
32                 'id': 'VPWON_1220719',
33                 'ext': 'm4v',
34                 'title': 'Nieuwsuur',
35                 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
36                 'upload_date': '20140622',
37             },
38         },
39         {
40             'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
41             'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
42             'info_dict': {
43                 'id': 'VARA_101191800',
44                 'ext': 'm4v',
45                 'title': 'De Mega Mike & Mega Thomas show',
46                 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
47                 'upload_date': '20090227',
48                 'duration': 2400,
49             },
50         },
51         {
52             'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
53             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
54             'info_dict': {
55                 'id': 'VPWON_1169289',
56                 'ext': 'm4v',
57                 'title': 'Tegenlicht',
58                 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
59                 'upload_date': '20130225',
60                 'duration': 3000,
61             },
62         },
63         {
64             'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706',
65             'info_dict': {
66                 'id': 'WO_VPRO_043706',
67                 'ext': 'wmv',
68                 'title': 'De nieuwe mens - Deel 1',
69                 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
70                 'duration': 4680,
71             },
72             'params': {
73                 # mplayer mms download
74                 'skip_download': True,
75             }
76         },
77         # non asf in streams
78         {
79             'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
80             'md5': 'b3da13de374cbe2d5332a7e910bef97f',
81             'info_dict': {
82                 'id': 'WO_NOS_762771',
83                 'ext': 'mp4',
84                 'title': 'Hoe gaat Europa verder na Parijs?',
85             },
86         },
87     ]
88
89     def _real_extract(self, url):
90         video_id = self._match_id(url)
91         return self._get_info(video_id)
92
93     def _get_info(self, video_id):
94         metadata = self._download_json(
95             'http://e.omroep.nl/metadata/aflevering/%s' % video_id,
96             video_id,
97             # We have to remove the javascript callback
98             transform_source=strip_jsonp,
99         )
100
101         token = self._get_token(video_id)
102
103         formats = []
104
105         pubopties = metadata.get('pubopties')
106         if 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):
114                     continue
115                 streams = format_info.get('streams')
116                 if streams:
117                     video_info = self._download_json(
118                         streams[0] + '&type=json',
119                         video_id, 'Downloading %s stream JSON' % format_id)
120                 else:
121                     video_info = format_info
122                 video_url = video_info.get('url')
123                 if not video_url:
124                     continue
125                 if format_id == 'adaptive':
126                     formats.extend(self._extract_m3u8_formats(video_url, video_id))
127                 else:
128                     formats.append({
129                         'url': video_url,
130                         'format_id': format_id,
131                         'quality': quality(format_id),
132                     })
133
134         streams = metadata.get('streams')
135         if streams:
136             for i, stream in enumerate(streams):
137                 stream_url = stream.get('url')
138                 if not stream_url:
139                     continue
140                 if '.asf' not in stream_url:
141                     formats.append({
142                         'url': stream_url,
143                         'quality': stream.get('kwaliteit'),
144                     })
145                     continue
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')
151                 if ref is None:
152                     continue
153                 video_url = ref.get('href')
154                 if not video_url:
155                     continue
156                 formats.append({
157                     'url': video_url,
158                     'ext': stream.get('formaat', 'asf'),
159                     'quality': stream.get('kwaliteit'),
160                 })
161
162         self._sort_formats(formats)
163
164         return {
165             'id': video_id,
166             'title': metadata['titel'],
167             'description': metadata['info'],
168             'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
169             'upload_date': unified_strdate(metadata.get('gidsdatum')),
170             'duration': parse_duration(metadata.get('tijdsduur')),
171             'formats': formats,
172         }
173
174
175 class NPOLiveIE(NPOBaseIE):
176     IE_NAME = 'npo.nl:live'
177     _VALID_URL = r'https?://www\.npo\.nl/live/(?P<id>.+)'
178
179     _TEST = {
180         'url': 'http://www.npo.nl/live/npo-1',
181         'info_dict': {
182             'id': 'LI_NEDERLAND1_136692',
183             'display_id': 'npo-1',
184             'ext': 'mp4',
185             'title': 're:^Nederland 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
186             'description': 'Livestream',
187             'is_live': True,
188         },
189         'params': {
190             'skip_download': True,
191         }
192     }
193
194     def _real_extract(self, url):
195         display_id = self._match_id(url)
196
197         webpage = self._download_webpage(url, display_id)
198
199         live_id = self._search_regex(
200             r'data-prid="([^"]+)"', webpage, 'live id')
201
202         metadata = self._download_json(
203             'http://e.omroep.nl/metadata/%s' % live_id,
204             display_id, transform_source=strip_jsonp)
205
206         token = self._get_token(display_id)
207
208         formats = []
209
210         streams = metadata.get('streams')
211         if streams:
212             for stream in streams:
213                 stream_type = stream.get('type').lower()
214                 if stream_type == 'ss':
215                     continue
216                 stream_info = self._download_json(
217                     'http://ida.omroep.nl/aapi/?stream=%s&token=%s&type=jsonp'
218                     % (stream.get('url'), token),
219                     display_id, 'Downloading %s JSON' % stream_type)
220                 if stream_info.get('error_code', 0) or stream_info.get('errorcode', 0):
221                     continue
222                 stream_url = self._download_json(
223                     stream_info['stream'], display_id,
224                     'Downloading %s URL' % stream_type,
225                     transform_source=strip_jsonp)
226                 if stream_type == 'hds':
227                     f4m_formats = self._extract_f4m_formats(stream_url, display_id)
228                     # f4m downloader downloads only piece of live stream
229                     for f4m_format in f4m_formats:
230                         f4m_format['preference'] = -1
231                     formats.extend(f4m_formats)
232                 elif stream_type == 'hls':
233                     formats.extend(self._extract_m3u8_formats(stream_url, display_id, 'mp4'))
234                 else:
235                     formats.append({
236                         'url': stream_url,
237                     })
238
239         self._sort_formats(formats)
240
241         return {
242             'id': live_id,
243             'display_id': display_id,
244             'title': self._live_title(metadata['titel']),
245             'description': metadata['info'],
246             'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
247             'formats': formats,
248             'is_live': True,
249         }
250
251
252 class TegenlichtVproIE(NPOIE):
253     IE_NAME = 'tegenlicht.vpro.nl'
254     _VALID_URL = r'https?://tegenlicht\.vpro\.nl/afleveringen/.*?'
255
256     _TESTS = [
257         {
258             'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
259             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
260             'info_dict': {
261                 'id': 'VPWON_1169289',
262                 'ext': 'm4v',
263                 'title': 'Tegenlicht',
264                 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
265                 'upload_date': '20130225',
266             },
267         },
268     ]
269
270     def _real_extract(self, url):
271         name = url_basename(url)
272         webpage = self._download_webpage(url, name)
273         urn = self._html_search_meta('mediaurn', webpage)
274         info_page = self._download_json(
275             'http://rs.vpro.nl/v2/api/media/%s.json' % urn, name)
276         return self._get_info(info_page['mid'])