Merge pull request #3927 from qrtt1/master
[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 )
13
14
15 class NPOIE(InfoExtractor):
16     IE_NAME = 'npo.nl'
17     _VALID_URL = r'https?://www\.npo\.nl/[^/]+/[^/]+/(?P<id>[^/?]+)'
18
19     _TESTS = [
20         {
21             'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
22             'md5': '4b3f9c429157ec4775f2c9cb7b911016',
23             'info_dict': {
24                 'id': 'VPWON_1220719',
25                 'ext': 'm4v',
26                 'title': 'Nieuwsuur',
27                 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
28                 'upload_date': '20140622',
29             },
30         },
31         {
32             'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
33             'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
34             'info_dict': {
35                 'id': 'VARA_101191800',
36                 'ext': 'm4v',
37                 'title': 'De Mega Mike & Mega Thomas show',
38                 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
39                 'upload_date': '20090227',
40                 'duration': 2400,
41             },
42         },
43         {
44             'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
45             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
46             'info_dict': {
47                 'id': 'VPWON_1169289',
48                 'ext': 'm4v',
49                 'title': 'Tegenlicht',
50                 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
51                 'upload_date': '20130225',
52                 'duration': 3000,
53             },
54         }
55     ]
56
57     def _real_extract(self, url):
58         mobj = re.match(self._VALID_URL, url)
59         video_id = mobj.group('id')
60         return self._get_info(video_id)
61
62     def _get_info(self, video_id):
63         metadata = self._download_json(
64             'http://e.omroep.nl/metadata/aflevering/%s' % video_id,
65             video_id,
66             # We have to remove the javascript callback
67             transform_source=strip_jsonp,
68         )
69         token_page = self._download_webpage(
70             'http://ida.omroep.nl/npoplayer/i.js',
71             video_id,
72             note='Downloading token'
73         )
74         token = self._search_regex(r'npoplayer\.token = "(.+?)"', token_page, 'token')
75
76         formats = []
77         quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
78         for format_id in metadata['pubopties']:
79             format_info = self._download_json(
80                 'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s' % (video_id, format_id, token),
81                 video_id, 'Downloading %s JSON' % format_id)
82             if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
83                 continue
84             streams = format_info.get('streams')
85             if streams:
86                 video_info = self._download_json(
87                     streams[0] + '&type=json',
88                     video_id, 'Downloading %s stream JSON' % format_id)
89             else:
90                 video_info = format_info
91             video_url = video_info.get('url')
92             if not video_url:
93                 continue
94             if format_id == 'adaptive':
95                 formats.extend(self._extract_m3u8_formats(video_url, video_id))
96             else:
97                 formats.append({
98                     'url': video_url,
99                     'format_id': format_id,
100                     'quality': quality(format_id),
101                 })
102         self._sort_formats(formats)
103
104         return {
105             'id': video_id,
106             'title': metadata['titel'],
107             'description': metadata['info'],
108             'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
109             'upload_date': unified_strdate(metadata.get('gidsdatum')),
110             'duration': parse_duration(metadata.get('tijdsduur')),
111             'formats': formats,
112         }
113
114
115 class TegenlichtVproIE(NPOIE):
116     IE_NAME = 'tegenlicht.vpro.nl'
117     _VALID_URL = r'https?://tegenlicht\.vpro\.nl/afleveringen/.*?'
118
119     _TESTS = [
120         {
121             'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
122             'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
123             'info_dict': {
124                 'id': 'VPWON_1169289',
125                 'ext': 'm4v',
126                 'title': 'Tegenlicht',
127                 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
128                 'upload_date': '20130225',
129             },
130         },
131     ]
132
133     def _real_extract(self, url):
134         name = url_basename(url)
135         webpage = self._download_webpage(url, name)
136         urn = self._html_search_meta('mediaurn', webpage)
137         info_page = self._download_json(
138             'http://rs.vpro.nl/v2/api/media/%s.json' % urn, name)
139         return self._get_info(info_page['mid'])