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