]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/nrk.py
d066a96db137ee3fb2c36f712803622e73b4aa40
[youtubedl] / youtube_dl / extractor / nrk.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 float_or_none,
10 parse_duration,
11 unified_strdate,
12 )
13
14
15 class NRKIE(InfoExtractor):
16 _VALID_URL = r'(?:nrk:|https?://(?:www\.)?nrk\.no/video/PS\*)(?P<id>\d+)'
17
18 _TESTS = [
19 {
20 'url': 'http://www.nrk.no/video/PS*150533',
21 'md5': 'bccd850baebefe23b56d708a113229c2',
22 'info_dict': {
23 'id': '150533',
24 'ext': 'flv',
25 'title': 'Dompap og andre fugler i Piip-Show',
26 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
27 'duration': 263,
28 }
29 },
30 {
31 'url': 'http://www.nrk.no/video/PS*154915',
32 'md5': '0b1493ba1aae7d9579a5ad5531bc395a',
33 'info_dict': {
34 'id': '154915',
35 'ext': 'flv',
36 'title': 'Slik høres internett ut når du er blind',
37 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
38 'duration': 20,
39 }
40 },
41 ]
42
43 def _real_extract(self, url):
44 video_id = self._match_id(url)
45
46 data = self._download_json(
47 'http://v8.psapi.nrk.no/mediaelement/%s' % video_id,
48 video_id, 'Downloading media JSON')
49
50 if data['usageRights']['isGeoBlocked']:
51 raise ExtractorError(
52 'NRK har ikke rettig-heter til å vise dette programmet utenfor Norge',
53 expected=True)
54
55 video_url = data['mediaUrl'] + '?hdcore=3.5.0&plugin=aasp-3.5.0.151.81'
56
57 duration = parse_duration(data.get('duration'))
58
59 images = data.get('images')
60 if images:
61 thumbnails = images['webImages']
62 thumbnails.sort(key=lambda image: image['pixelWidth'])
63 thumbnail = thumbnails[-1]['imageUrl']
64 else:
65 thumbnail = None
66
67 return {
68 'id': video_id,
69 'url': video_url,
70 'ext': 'flv',
71 'title': data['title'],
72 'description': data['description'],
73 'duration': duration,
74 'thumbnail': thumbnail,
75 }
76
77
78 class NRKPlaylistIE(InfoExtractor):
79 _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video)(?:[^/]+/)+(?P<id>[^/]+)'
80
81 _TESTS = [{
82 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
83 'info_dict': {
84 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
85 'title': 'Gjenopplev den historiske solformørkelsen',
86 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
87 },
88 'playlist_count': 2,
89 }, {
90 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
91 'info_dict': {
92 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
93 'title': 'Rivertonprisen til Karin Fossum',
94 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
95 },
96 'playlist_count': 5,
97 }]
98
99 def _real_extract(self, url):
100 playlist_id = self._match_id(url)
101
102 webpage = self._download_webpage(url, playlist_id)
103
104 entries = [
105 self.url_result('nrk:%s' % video_id, 'NRK')
106 for video_id in re.findall(
107 r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"',
108 webpage)
109 ]
110
111 playlist_title = self._og_search_title(webpage)
112 playlist_description = self._og_search_description(webpage)
113
114 return self.playlist_result(
115 entries, playlist_id, playlist_title, playlist_description)
116
117
118 class NRKTVIE(InfoExtractor):
119 IE_DESC = 'NRK TV and NRK Radio'
120 _VALID_URL = r'(?P<baseurl>https?://(?:tv|radio)\.nrk(?:super)?\.no/)(?:serie/[^/]+|program)/(?P<id>[a-zA-Z]{4}\d{8})(?:/\d{2}-\d{2}-\d{4})?(?:#del=(?P<part_id>\d+))?'
121
122 _TESTS = [
123 {
124 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
125 'md5': 'adf2c5454fa2bf032f47a9f8fb351342',
126 'info_dict': {
127 'id': 'MUHH48000314',
128 'ext': 'flv',
129 'title': '20 spørsmål',
130 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
131 'upload_date': '20140523',
132 'duration': 1741.52,
133 },
134 },
135 {
136 'url': 'https://tv.nrk.no/program/mdfp15000514',
137 'md5': '383650ece2b25ecec996ad7b5bb2a384',
138 'info_dict': {
139 'id': 'mdfp15000514',
140 'ext': 'flv',
141 'title': 'Kunnskapskanalen: Grunnlovsjubiléet - Stor ståhei for ingenting',
142 'description': 'md5:654c12511f035aed1e42bdf5db3b206a',
143 'upload_date': '20140524',
144 'duration': 4605.0,
145 },
146 },
147 {
148 # single playlist video
149 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
150 'md5': 'adbd1dbd813edaf532b0a253780719c2',
151 'info_dict': {
152 'id': 'MSPO40010515-part2',
153 'ext': 'flv',
154 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
155 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
156 'upload_date': '20150106',
157 },
158 'skip': 'Only works from Norway',
159 },
160 {
161 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
162 'playlist': [
163 {
164 'md5': '9480285eff92d64f06e02a5367970a7a',
165 'info_dict': {
166 'id': 'MSPO40010515-part1',
167 'ext': 'flv',
168 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
169 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
170 'upload_date': '20150106',
171 },
172 },
173 {
174 'md5': 'adbd1dbd813edaf532b0a253780719c2',
175 'info_dict': {
176 'id': 'MSPO40010515-part2',
177 'ext': 'flv',
178 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
179 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
180 'upload_date': '20150106',
181 },
182 },
183 ],
184 'info_dict': {
185 'id': 'MSPO40010515',
186 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
187 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
188 'upload_date': '20150106',
189 'duration': 6947.5199999999995,
190 },
191 'skip': 'Only works from Norway',
192 },
193 {
194 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
195 'only_matching': True,
196 }
197 ]
198
199 def _debug_print(self, txt):
200 if self._downloader.params.get('verbose', False):
201 self.to_screen('[debug] %s' % txt)
202
203 def _get_subtitles(self, subtitlesurl, video_id, baseurl):
204 url = "%s%s" % (baseurl, subtitlesurl)
205 self._debug_print('%s: Subtitle url: %s' % (video_id, url))
206 captions = self._download_xml(
207 url, video_id, 'Downloading subtitles')
208 lang = captions.get('lang', 'no')
209 return {lang: [
210 {'ext': 'ttml', 'url': url},
211 ]}
212
213 def _extract_f4m(self, manifest_url, video_id):
214 return self._extract_f4m_formats(
215 manifest_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124', video_id, f4m_id='hds')
216
217 def _real_extract(self, url):
218 mobj = re.match(self._VALID_URL, url)
219 video_id = mobj.group('id')
220 part_id = mobj.group('part_id')
221 baseurl = mobj.group('baseurl')
222
223 webpage = self._download_webpage(url, video_id)
224
225 title = self._html_search_meta(
226 'title', webpage, 'title')
227 description = self._html_search_meta(
228 'description', webpage, 'description')
229
230 thumbnail = self._html_search_regex(
231 r'data-posterimage="([^"]+)"',
232 webpage, 'thumbnail', fatal=False)
233 upload_date = unified_strdate(self._html_search_meta(
234 'rightsfrom', webpage, 'upload date', fatal=False))
235 duration = float_or_none(self._html_search_regex(
236 r'data-duration="([^"]+)"',
237 webpage, 'duration', fatal=False))
238
239 # playlist
240 parts = re.findall(
241 r'<a href="#del=(\d+)"[^>]+data-argument="([^"]+)">([^<]+)</a>', webpage)
242 if parts:
243 entries = []
244 for current_part_id, stream_url, part_title in parts:
245 if part_id and current_part_id != part_id:
246 continue
247 video_part_id = '%s-part%s' % (video_id, current_part_id)
248 formats = self._extract_f4m(stream_url, video_part_id)
249 entries.append({
250 'id': video_part_id,
251 'title': part_title,
252 'description': description,
253 'thumbnail': thumbnail,
254 'upload_date': upload_date,
255 'formats': formats,
256 })
257 if part_id:
258 if entries:
259 return entries[0]
260 else:
261 playlist = self.playlist_result(entries, video_id, title, description)
262 playlist.update({
263 'thumbnail': thumbnail,
264 'upload_date': upload_date,
265 'duration': duration,
266 })
267 return playlist
268
269 formats = []
270
271 f4m_url = re.search(r'data-media="([^"]+)"', webpage)
272 if f4m_url:
273 formats.extend(self._extract_f4m(f4m_url.group(1), video_id))
274
275 m3u8_url = re.search(r'data-hls-media="([^"]+)"', webpage)
276 if m3u8_url:
277 formats.extend(self._extract_m3u8_formats(m3u8_url.group(1), video_id, 'mp4', m3u8_id='hls'))
278 self._sort_formats(formats)
279
280 subtitles_url = self._html_search_regex(
281 r'data-subtitlesurl[ ]*=[ ]*"([^"]+)"',
282 webpage, 'subtitle URL', default=None)
283 subtitles = None
284 if subtitles_url:
285 subtitles = self.extract_subtitles(subtitles_url, video_id, baseurl)
286
287 return {
288 'id': video_id,
289 'title': title,
290 'description': description,
291 'thumbnail': thumbnail,
292 'upload_date': upload_date,
293 'duration': duration,
294 'formats': formats,
295 'subtitles': subtitles,
296 }