[airmozilla] Add new extractor
[youtube-dl] / youtube_dl / extractor / npo.py
1 from __future__ import unicode_literals
2
3 from .subtitles import SubtitlesInfoExtractor
4 from .common import InfoExtractor
5 from ..utils import (
6     fix_xml_ampersands,
7     parse_duration,
8     qualities,
9     strip_jsonp,
10     unified_strdate,
11     url_basename,
12 )
13
14
15 class NPOBaseIE(SubtitlesInfoExtractor):
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         return self._search_regex(
21             r'npoplayer\.token = "(.+?)"', token_page, 'token')
22
23
24 class NPOIE(NPOBaseIE):
25     IE_NAME = 'npo.nl'
26     _VALID_URL = r'https?://(?:www\.)?npo\.nl/(?!live|radio)[^/]+/[^/]+/(?P<id>[^/?]+)'
27
28     _TESTS = [
29         {
30             'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
31             'md5': '4b3f9c429157ec4775f2c9cb7b911016',
32             'info_dict': {
33                 'id': 'VPWON_1220719',
34                 'ext': 'm4v',
35                 'title': 'Nieuwsuur',
36                 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
37                 'upload_date': '20140622',
38             },
39         },
40         {
41             'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
42             'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
43             'info_dict': {
44                 'id': 'VARA_101191800',
45                 'ext': 'm4v',
46                 'title': 'De Mega Mike & Mega Thomas show',
47                 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
48                 'upload_date': '20090227',
49                 'duration': 2400,
50             },
51         },
52         {
53             'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
54             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
55             'info_dict': {
56                 'id': 'VPWON_1169289',
57                 'ext': 'm4v',
58                 'title': 'Tegenlicht',
59                 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
60                 'upload_date': '20130225',
61                 'duration': 3000,
62             },
63         },
64         {
65             'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706',
66             'info_dict': {
67                 'id': 'WO_VPRO_043706',
68                 'ext': 'wmv',
69                 'title': 'De nieuwe mens - Deel 1',
70                 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
71                 'duration': 4680,
72             },
73             'params': {
74                 # mplayer mms download
75                 'skip_download': True,
76             }
77         },
78         # non asf in streams
79         {
80             'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
81             'md5': 'b3da13de374cbe2d5332a7e910bef97f',
82             'info_dict': {
83                 'id': 'WO_NOS_762771',
84                 'ext': 'mp4',
85                 'title': 'Hoe gaat Europa verder na Parijs?',
86             },
87         },
88     ]
89
90     def _real_extract(self, url):
91         video_id = self._match_id(url)
92         return self._get_info(video_id)
93
94     def _get_info(self, video_id):
95         metadata = self._download_json(
96             'http://e.omroep.nl/metadata/aflevering/%s' % video_id,
97             video_id,
98             # We have to remove the javascript callback
99             transform_source=strip_jsonp,
100         )
101
102         token = self._get_token(video_id)
103
104         formats = []
105
106         pubopties = metadata.get('pubopties')
107         if pubopties:
108             quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
109             for format_id in pubopties:
110                 format_info = self._download_json(
111                     'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s'
112                     % (video_id, format_id, token),
113                     video_id, 'Downloading %s JSON' % format_id)
114                 if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
115                     continue
116                 streams = format_info.get('streams')
117                 if streams:
118                     video_info = self._download_json(
119                         streams[0] + '&type=json',
120                         video_id, 'Downloading %s stream JSON' % format_id)
121                 else:
122                     video_info = format_info
123                 video_url = video_info.get('url')
124                 if not video_url:
125                     continue
126                 if format_id == 'adaptive':
127                     formats.extend(self._extract_m3u8_formats(video_url, video_id))
128                 else:
129                     formats.append({
130                         'url': video_url,
131                         'format_id': format_id,
132                         'quality': quality(format_id),
133                     })
134
135         streams = metadata.get('streams')
136         if streams:
137             for i, stream in enumerate(streams):
138                 stream_url = stream.get('url')
139                 if not stream_url:
140                     continue
141                 if '.asf' not in stream_url:
142                     formats.append({
143                         'url': stream_url,
144                         'quality': stream.get('kwaliteit'),
145                     })
146                     continue
147                 asx = self._download_xml(
148                     stream_url, video_id,
149                     'Downloading stream %d ASX playlist' % i,
150                     transform_source=fix_xml_ampersands)
151                 ref = asx.find('./ENTRY/Ref')
152                 if ref is None:
153                     continue
154                 video_url = ref.get('href')
155                 if not video_url:
156                     continue
157                 formats.append({
158                     'url': video_url,
159                     'ext': stream.get('formaat', 'asf'),
160                     'quality': stream.get('kwaliteit'),
161                 })
162
163         self._sort_formats(formats)
164
165         subtitles = {}
166         if metadata.get('tt888') == 'ja':
167             subtitles['nl'] = 'http://e.omroep.nl/tt888/%s' % video_id
168
169         if self._downloader.params.get('listsubtitles', False):
170             self._list_available_subtitles(video_id, subtitles)
171             return
172
173         subtitles = self.extract_subtitles(video_id, subtitles)
174
175         return {
176             'id': video_id,
177             'title': metadata['titel'],
178             'description': metadata['info'],
179             'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
180             'upload_date': unified_strdate(metadata.get('gidsdatum')),
181             'duration': parse_duration(metadata.get('tijdsduur')),
182             'formats': formats,
183             'subtitles': subtitles,
184         }
185
186
187 class NPOLiveIE(NPOBaseIE):
188     IE_NAME = 'npo.nl:live'
189     _VALID_URL = r'https?://(?:www\.)?npo\.nl/live/(?P<id>.+)'
190
191     _TEST = {
192         'url': 'http://www.npo.nl/live/npo-1',
193         'info_dict': {
194             'id': 'LI_NEDERLAND1_136692',
195             'display_id': 'npo-1',
196             'ext': 'mp4',
197             'title': 're:^Nederland 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
198             'description': 'Livestream',
199             'is_live': True,
200         },
201         'params': {
202             'skip_download': True,
203         }
204     }
205
206     def _real_extract(self, url):
207         display_id = self._match_id(url)
208
209         webpage = self._download_webpage(url, display_id)
210
211         live_id = self._search_regex(
212             r'data-prid="([^"]+)"', webpage, 'live id')
213
214         metadata = self._download_json(
215             'http://e.omroep.nl/metadata/%s' % live_id,
216             display_id, transform_source=strip_jsonp)
217
218         token = self._get_token(display_id)
219
220         formats = []
221
222         streams = metadata.get('streams')
223         if streams:
224             for stream in streams:
225                 stream_type = stream.get('type').lower()
226                 if stream_type == 'ss':
227                     continue
228                 stream_info = self._download_json(
229                     'http://ida.omroep.nl/aapi/?stream=%s&token=%s&type=jsonp'
230                     % (stream.get('url'), token),
231                     display_id, 'Downloading %s JSON' % stream_type)
232                 if stream_info.get('error_code', 0) or stream_info.get('errorcode', 0):
233                     continue
234                 stream_url = self._download_json(
235                     stream_info['stream'], display_id,
236                     'Downloading %s URL' % stream_type,
237                     transform_source=strip_jsonp)
238                 if stream_type == 'hds':
239                     f4m_formats = self._extract_f4m_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_m3u8_formats(stream_url, display_id, 'mp4'))
246                 else:
247                     formats.append({
248                         'url': stream_url,
249                     })
250
251         self._sort_formats(formats)
252
253         return {
254             'id': live_id,
255             'display_id': display_id,
256             'title': self._live_title(metadata['titel']),
257             'description': metadata['info'],
258             'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
259             'formats': formats,
260             'is_live': True,
261         }
262
263
264 class NPORadioIE(InfoExtractor):
265     IE_NAME = 'npo.nl:radio'
266     _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/(?P<id>[^/]+)/?$'
267
268     _TEST = {
269         'url': 'http://www.npo.nl/radio/radio-1',
270         'info_dict': {
271             'id': 'radio-1',
272             'ext': 'mp3',
273             'title': 're:^NPO Radio 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
274             'is_live': True,
275         },
276         'params': {
277             'skip_download': True,
278         }
279     }
280
281     @staticmethod
282     def _html_get_attribute_regex(attribute):
283         return r'{0}\s*=\s*\'([^\']+)\''.format(attribute)
284
285     def _real_extract(self, url):
286         video_id = self._match_id(url)
287
288         webpage = self._download_webpage(url, video_id)
289
290         title = self._html_search_regex(
291             self._html_get_attribute_regex('data-channel'), webpage, 'title')
292
293         stream = self._parse_json(
294             self._html_search_regex(self._html_get_attribute_regex('data-streams'), webpage, 'data-streams'),
295             video_id)
296
297         codec = stream.get('codec')
298
299         return {
300             'id': video_id,
301             'url': stream['url'],
302             'title': self._live_title(title),
303             'acodec': codec,
304             'ext': codec,
305             'is_live': True,
306         }
307
308
309 class NPORadioFragmentIE(InfoExtractor):
310     IE_NAME = 'npo.nl:radio:fragment'
311     _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/[^/]+/fragment/(?P<id>\d+)'
312
313     _TEST = {
314         'url': 'http://www.npo.nl/radio/radio-5/fragment/174356',
315         'md5': 'dd8cc470dad764d0fdc70a9a1e2d18c2',
316         'info_dict': {
317             'id': '174356',
318             'ext': 'mp3',
319             'title': 'Jubileumconcert Willeke Alberti',
320         },
321     }
322
323     def _real_extract(self, url):
324         audio_id = self._match_id(url)
325
326         webpage = self._download_webpage(url, audio_id)
327
328         title = self._html_search_regex(
329             r'href="/radio/[^/]+/fragment/%s" title="([^"]+)"' % audio_id,
330             webpage, 'title')
331
332         audio_url = self._search_regex(
333             r"data-streams='([^']+)'", webpage, 'audio url')
334
335         return {
336             'id': audio_id,
337             'url': audio_url,
338             'title': title,
339         }
340
341
342 class TegenlichtVproIE(NPOIE):
343     IE_NAME = 'tegenlicht.vpro.nl'
344     _VALID_URL = r'https?://tegenlicht\.vpro\.nl/afleveringen/.*?'
345
346     _TESTS = [
347         {
348             'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
349             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
350             'info_dict': {
351                 'id': 'VPWON_1169289',
352                 'ext': 'm4v',
353                 'title': 'Tegenlicht',
354                 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
355                 'upload_date': '20130225',
356             },
357         },
358     ]
359
360     def _real_extract(self, url):
361         name = url_basename(url)
362         webpage = self._download_webpage(url, name)
363         urn = self._html_search_meta('mediaurn', webpage)
364         info_page = self._download_json(
365             'http://rs.vpro.nl/v2/api/media/%s.json' % urn, name)
366         return self._get_info(info_page['mid'])