Merge remote-tracking branch 'Dineshs91/f4m-2.0'
[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 )
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     ]
71
72     def _real_extract(self, url):
73         mobj = re.match(self._VALID_URL, url)
74         video_id = mobj.group('id')
75         return self._get_info(video_id)
76
77     def _get_info(self, video_id):
78         metadata = self._download_json(
79             'http://e.omroep.nl/metadata/aflevering/%s' % video_id,
80             video_id,
81             # We have to remove the javascript callback
82             transform_source=strip_jsonp,
83         )
84         token_page = self._download_webpage(
85             'http://ida.omroep.nl/npoplayer/i.js',
86             video_id,
87             note='Downloading token'
88         )
89         token = self._search_regex(r'npoplayer\.token = "(.+?)"', token_page, 'token')
90
91         formats = []
92
93         pubopties = metadata.get('pubopties')
94         if pubopties:
95             quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
96             for format_id in pubopties:
97                 format_info = self._download_json(
98                     'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s'
99                     % (video_id, format_id, token),
100                     video_id, 'Downloading %s JSON' % format_id)
101                 if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
102                     continue
103                 streams = format_info.get('streams')
104                 if streams:
105                     video_info = self._download_json(
106                         streams[0] + '&type=json',
107                         video_id, 'Downloading %s stream JSON' % format_id)
108                 else:
109                     video_info = format_info
110                 video_url = video_info.get('url')
111                 if not video_url:
112                     continue
113                 if format_id == 'adaptive':
114                     formats.extend(self._extract_m3u8_formats(video_url, video_id))
115                 else:
116                     formats.append({
117                         'url': video_url,
118                         'format_id': format_id,
119                         'quality': quality(format_id),
120                     })
121
122         streams = metadata.get('streams')
123         if streams:
124             for i, stream in enumerate(streams):
125                 stream_url = stream.get('url')
126                 if not stream_url:
127                     continue
128                 asx = self._download_xml(
129                     stream_url, video_id,
130                     'Downloading stream %d ASX playlist' % i,
131                     transform_source=fix_xml_ampersands)
132                 ref = asx.find('./ENTRY/Ref')
133                 if ref is None:
134                     continue
135                 video_url = ref.get('href')
136                 if not video_url:
137                     continue
138                 formats.append({
139                     'url': video_url,
140                     'ext': stream.get('formaat', 'asf'),
141                     'quality': stream.get('kwaliteit'),
142                 })
143
144         self._sort_formats(formats)
145
146         return {
147             'id': video_id,
148             'title': metadata['titel'],
149             'description': metadata['info'],
150             'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
151             'upload_date': unified_strdate(metadata.get('gidsdatum')),
152             'duration': parse_duration(metadata.get('tijdsduur')),
153             'formats': formats,
154         }
155
156
157 class TegenlichtVproIE(NPOIE):
158     IE_NAME = 'tegenlicht.vpro.nl'
159     _VALID_URL = r'https?://tegenlicht\.vpro\.nl/afleveringen/.*?'
160
161     _TESTS = [
162         {
163             'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
164             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
165             'info_dict': {
166                 'id': 'VPWON_1169289',
167                 'ext': 'm4v',
168                 'title': 'Tegenlicht',
169                 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
170                 'upload_date': '20130225',
171             },
172         },
173     ]
174
175     def _real_extract(self, url):
176         name = url_basename(url)
177         webpage = self._download_webpage(url, name)
178         urn = self._html_search_meta('mediaurn', webpage)
179         info_page = self._download_json(
180             'http://rs.vpro.nl/v2/api/media/%s.json' % urn, name)
181         return self._get_info(info_page['mid'])