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