[npo] Make extension check less strict and add test (#4680)
[youtube-dl] / youtube_dl / extractor / npo.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     unified_strdate,
8     parse_duration,
9     qualities,
10     strip_jsonp,
11     url_basename,
12     fix_xml_ampersands,
13     determine_ext,
14 )
15
16
17 class NPOIE(InfoExtractor):
18     IE_NAME = 'npo.nl'
19     _VALID_URL = r'https?://www\.npo\.nl/[^/]+/[^/]+/(?P<id>[^/?]+)'
20
21     _TESTS = [
22         {
23             'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
24             'md5': '4b3f9c429157ec4775f2c9cb7b911016',
25             'info_dict': {
26                 'id': 'VPWON_1220719',
27                 'ext': 'm4v',
28                 'title': 'Nieuwsuur',
29                 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
30                 'upload_date': '20140622',
31             },
32         },
33         {
34             'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
35             'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
36             'info_dict': {
37                 'id': 'VARA_101191800',
38                 'ext': 'm4v',
39                 'title': 'De Mega Mike & Mega Thomas show',
40                 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
41                 'upload_date': '20090227',
42                 'duration': 2400,
43             },
44         },
45         {
46             'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
47             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
48             'info_dict': {
49                 'id': 'VPWON_1169289',
50                 'ext': 'm4v',
51                 'title': 'Tegenlicht',
52                 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
53                 'upload_date': '20130225',
54                 'duration': 3000,
55             },
56         },
57         {
58             'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706',
59             'info_dict': {
60                 'id': 'WO_VPRO_043706',
61                 'ext': 'wmv',
62                 'title': 'De nieuwe mens - Deel 1',
63                 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
64                 'duration': 4680,
65             },
66             'params': {
67                 # mplayer mms download
68                 'skip_download': True,
69             }
70         },
71         # non asf in streams
72         {
73             'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
74             'md5': 'b3da13de374cbe2d5332a7e910bef97f',
75             'info_dict': {
76                 'id': 'WO_NOS_762771',
77                 'ext': 'mp4',
78                 'title': 'Hoe gaat Europa verder na Parijs?',
79             },
80         },
81     ]
82
83     def _real_extract(self, url):
84         mobj = re.match(self._VALID_URL, url)
85         video_id = mobj.group('id')
86         return self._get_info(video_id)
87
88     def _get_info(self, video_id):
89         metadata = self._download_json(
90             'http://e.omroep.nl/metadata/aflevering/%s' % video_id,
91             video_id,
92             # We have to remove the javascript callback
93             transform_source=strip_jsonp,
94         )
95         token_page = self._download_webpage(
96             'http://ida.omroep.nl/npoplayer/i.js',
97             video_id,
98             note='Downloading token'
99         )
100         token = self._search_regex(r'npoplayer\.token = "(.+?)"', token_page, 'token')
101
102         formats = []
103
104         pubopties = metadata.get('pubopties')
105         if pubopties:
106             quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
107             for format_id in pubopties:
108                 format_info = self._download_json(
109                     'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s'
110                     % (video_id, format_id, token),
111                     video_id, 'Downloading %s JSON' % format_id)
112                 if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
113                     continue
114                 streams = format_info.get('streams')
115                 if streams:
116                     video_info = self._download_json(
117                         streams[0] + '&type=json',
118                         video_id, 'Downloading %s stream JSON' % format_id)
119                 else:
120                     video_info = format_info
121                 video_url = video_info.get('url')
122                 if not video_url:
123                     continue
124                 if format_id == 'adaptive':
125                     formats.extend(self._extract_m3u8_formats(video_url, video_id))
126                 else:
127                     formats.append({
128                         'url': video_url,
129                         'format_id': format_id,
130                         'quality': quality(format_id),
131                     })
132
133         streams = metadata.get('streams')
134         if streams:
135             for i, stream in enumerate(streams):
136                 stream_url = stream.get('url')
137                 if not stream_url:
138                     continue
139                 if not '.asf' in stream_url:
140                     formats.append({
141                         'url': stream_url,
142                         'quality': stream.get('kwaliteit'),
143                     })
144                     continue
145                 asx = self._download_xml(
146                     stream_url, video_id,
147                     'Downloading stream %d ASX playlist' % i,
148                     transform_source=fix_xml_ampersands)
149                 ref = asx.find('./ENTRY/Ref')
150                 if ref is None:
151                     continue
152                 video_url = ref.get('href')
153                 if not video_url:
154                     continue
155                 formats.append({
156                     'url': video_url,
157                     'ext': stream.get('formaat', 'asf'),
158                     'quality': stream.get('kwaliteit'),
159                 })
160
161         self._sort_formats(formats)
162
163         return {
164             'id': video_id,
165             'title': metadata['titel'],
166             'description': metadata['info'],
167             'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
168             'upload_date': unified_strdate(metadata.get('gidsdatum')),
169             'duration': parse_duration(metadata.get('tijdsduur')),
170             'formats': formats,
171         }
172
173
174 class TegenlichtVproIE(NPOIE):
175     IE_NAME = 'tegenlicht.vpro.nl'
176     _VALID_URL = r'https?://tegenlicht\.vpro\.nl/afleveringen/.*?'
177
178     _TESTS = [
179         {
180             'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
181             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
182             'info_dict': {
183                 'id': 'VPWON_1169289',
184                 'ext': 'm4v',
185                 'title': 'Tegenlicht',
186                 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
187                 'upload_date': '20130225',
188             },
189         },
190     ]
191
192     def _real_extract(self, url):
193         name = url_basename(url)
194         webpage = self._download_webpage(url, name)
195         urn = self._html_search_meta('mediaurn', webpage)
196         info_page = self._download_json(
197             'http://rs.vpro.nl/v2/api/media/%s.json' % urn, name)
198         return self._get_info(info_page['mid'])