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