]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/nrk.py
Fix W504 and disable W503 (closes #20863)
[yt-dlp.git] / youtube_dl / extractor / nrk.py
CommitLineData
dcdb292f 1# coding: utf-8
d2176c80
S
2from __future__ import unicode_literals
3
4import re
5
6from .common import InfoExtractor
4b3ee098
S
7from ..compat import (
8 compat_str,
9 compat_urllib_parse_unquote,
10)
dfb2e1a3
S
11from ..utils import (
12 ExtractorError,
d8d540cf 13 int_or_none,
79fd7320 14 JSON_LD_RE,
4b3ee098 15 NO_DEFAULT,
d8d540cf 16 parse_age_limit,
76bfaf6d 17 parse_duration,
4b3ee098 18 try_get,
dfb2e1a3 19)
d2176c80
S
20
21
d8d540cf 22class NRKBaseIE(InfoExtractor):
4248dad9 23 _GEO_COUNTRIES = ['NO']
c78dd354 24
93cffb14
S
25 _api_host = None
26
d2176c80 27 def _real_extract(self, url):
4e6a2286 28 video_id = self._match_id(url)
d2176c80 29
93cffb14
S
30 api_hosts = (self._api_host, ) if self._api_host else self._API_HOSTS
31
32 for api_host in api_hosts:
33 data = self._download_json(
34 'http://%s/mediaelement/%s' % (api_host, video_id),
35 video_id, 'Downloading mediaelement JSON',
36 fatal=api_host == api_hosts[-1])
37 if not data:
38 continue
39 self._api_host = api_host
40 break
d8d540cf
S
41
42 title = data.get('fullTitle') or data.get('mainTitle') or data['title']
43 video_id = data.get('id') or video_id
44
45 entries = []
46
c80db5d3 47 conviva = data.get('convivaStatistics') or {}
3089bc74
S
48 live = (data.get('mediaElementType') == 'Live'
49 or data.get('isLive') is True or conviva.get('isLive'))
c80db5d3
S
50
51 def make_title(t):
52 return self._live_title(t) if live else t
53
d8d540cf
S
54 media_assets = data.get('mediaAssets')
55 if media_assets and isinstance(media_assets, list):
56 def video_id_and_title(idx):
57 return ((video_id, title) if len(media_assets) == 1
58 else ('%s-%d' % (video_id, idx), '%s (Part %d)' % (title, idx)))
59 for num, asset in enumerate(media_assets, 1):
60 asset_url = asset.get('url')
61 if not asset_url:
62 continue
ad316425 63 formats = self._extract_akamai_formats(asset_url, video_id)
d8d540cf
S
64 if not formats:
65 continue
66 self._sort_formats(formats)
c80db5d3
S
67
68 # Some f4m streams may not work with hdcore in fragments' URLs
69 for f in formats:
70 extra_param = f.get('extra_param_to_segment_url')
71 if extra_param and 'hdcore' in extra_param:
72 del f['extra_param_to_segment_url']
73
d8d540cf
S
74 entry_id, entry_title = video_id_and_title(num)
75 duration = parse_duration(asset.get('duration'))
76 subtitles = {}
77 for subtitle in ('webVtt', 'timedText'):
78 subtitle_url = asset.get('%sSubtitlesUrl' % subtitle)
79 if subtitle_url:
c8602b2f
S
80 subtitles.setdefault('no', []).append({
81 'url': compat_urllib_parse_unquote(subtitle_url)
82 })
d8d540cf
S
83 entries.append({
84 'id': asset.get('carrierId') or entry_id,
c80db5d3 85 'title': make_title(entry_title),
d8d540cf
S
86 'duration': duration,
87 'subtitles': subtitles,
88 'formats': formats,
89 })
d2176c80 90
d8d540cf
S
91 if not entries:
92 media_url = data.get('mediaUrl')
93 if media_url:
ad316425 94 formats = self._extract_akamai_formats(media_url, video_id)
d8d540cf
S
95 self._sort_formats(formats)
96 duration = parse_duration(data.get('duration'))
97 entries = [{
98 'id': video_id,
c80db5d3 99 'title': make_title(title),
d8d540cf
S
100 'duration': duration,
101 'formats': formats,
102 }]
d2176c80 103
d8d540cf 104 if not entries:
754e6c83
S
105 MESSAGES = {
106 'ProgramRightsAreNotReady': 'Du kan dessverre ikke se eller høre programmet',
107 'ProgramRightsHasExpired': 'Programmet har gått ut',
108 'ProgramIsGeoBlocked': 'NRK har ikke rettigheter til å vise dette programmet utenfor Norge',
109 }
ff400789
S
110 message_type = data.get('messageType', '')
111 # Can be ProgramIsGeoBlocked or ChannelIsGeoBlocked*
112 if 'IsGeoBlocked' in message_type:
113 self.raise_geo_restricted(
4248dad9
S
114 msg=MESSAGES.get('ProgramIsGeoBlocked'),
115 countries=self._GEO_COUNTRIES)
754e6c83
S
116 raise ExtractorError(
117 '%s said: %s' % (self.IE_NAME, MESSAGES.get(
118 message_type, message_type)),
119 expected=True)
874ae035 120
d8d540cf
S
121 series = conviva.get('seriesName') or data.get('seriesTitle')
122 episode = conviva.get('episodeName') or data.get('episodeNumberOrDate')
393d9fc6 123
8fd65fae
OS
124 season_number = None
125 episode_number = None
126 if data.get('mediaElementType') == 'Episode':
127 _season_episode = data.get('scoresStatistics', {}).get('springStreamStream') or \
128 data.get('relativeOriginUrl', '')
129 EPISODENUM_RE = [
7c5329e6
S
130 r'/s(?P<season>\d{,2})e(?P<episode>\d{,2})\.',
131 r'/sesong-(?P<season>\d{,2})/episode-(?P<episode>\d{,2})',
8fd65fae 132 ]
7c5329e6
S
133 season_number = int_or_none(self._search_regex(
134 EPISODENUM_RE, _season_episode, 'season number',
135 default=None, group='season'))
136 episode_number = int_or_none(self._search_regex(
137 EPISODENUM_RE, _season_episode, 'episode number',
138 default=None, group='episode'))
8fd65fae 139
d8d540cf 140 thumbnails = None
d2176c80 141 images = data.get('images')
d8d540cf
S
142 if images and isinstance(images, dict):
143 web_images = images.get('webImages')
144 if isinstance(web_images, list):
145 thumbnails = [{
146 'url': image['imageUrl'],
147 'width': int_or_none(image.get('width')),
148 'height': int_or_none(image.get('height')),
149 } for image in web_images if image.get('imageUrl')]
150
151 description = data.get('description')
8fd65fae 152 category = data.get('mediaAnalytics', {}).get('category')
d8d540cf
S
153
154 common_info = {
155 'description': description,
156 'series': series,
157 'episode': episode,
8fd65fae
OS
158 'season_number': season_number,
159 'episode_number': episode_number,
160 'categories': [category] if category else None,
d8d540cf
S
161 'age_limit': parse_age_limit(data.get('legalAge')),
162 'thumbnails': thumbnails,
dfb2e1a3
S
163 }
164
d8d540cf
S
165 vcodec = 'none' if data.get('mediaType') == 'Audio' else None
166
d8d540cf
S
167 for entry in entries:
168 entry.update(common_info)
169 for f in entry['formats']:
170 f['vcodec'] = vcodec
171
329e3dd5
S
172 points = data.get('shortIndexPoints')
173 if isinstance(points, list):
174 chapters = []
175 for next_num, point in enumerate(points, start=1):
176 if not isinstance(point, dict):
177 continue
178 start_time = parse_duration(point.get('startPoint'))
179 if start_time is None:
180 continue
181 end_time = parse_duration(
182 data.get('duration')
183 if next_num == len(points)
184 else points[next_num].get('startPoint'))
185 if end_time is None:
186 continue
187 chapters.append({
188 'start_time': start_time,
189 'end_time': end_time,
190 'title': point.get('title'),
191 })
192 if chapters and len(entries) == 1:
193 entries[0]['chapters'] = chapters
194
d8d540cf
S
195 return self.playlist_result(entries, video_id, title, description)
196
197
198class NRKIE(NRKBaseIE):
853a71b6
S
199 _VALID_URL = r'''(?x)
200 (?:
201 nrk:|
202 https?://
203 (?:
204 (?:www\.)?nrk\.no/video/PS\*|
983e9b77 205 v8[-.]psapi\.nrk\.no/mediaelement/
853a71b6
S
206 )
207 )
983e9b77 208 (?P<id>[^?#&]+)
853a71b6 209 '''
93cffb14 210 _API_HOSTS = ('psapi.nrk.no', 'v8-psapi.nrk.no')
d8d540cf
S
211 _TESTS = [{
212 # video
213 'url': 'http://www.nrk.no/video/PS*150533',
15699ec8 214 'md5': '706f34cdf1322577589e369e522b50ef',
d8d540cf
S
215 'info_dict': {
216 'id': '150533',
18cf6381 217 'ext': 'mp4',
d8d540cf
S
218 'title': 'Dompap og andre fugler i Piip-Show',
219 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
15699ec8 220 'duration': 262,
d8d540cf
S
221 }
222 }, {
223 # audio
224 'url': 'http://www.nrk.no/video/PS*154915',
225 # MD5 is unstable
226 'info_dict': {
227 'id': '154915',
228 'ext': 'flv',
229 'title': 'Slik høres internett ut når du er blind',
230 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
231 'duration': 20,
232 }
e2628fb6
S
233 }, {
234 'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
235 'only_matching': True,
983e9b77
S
236 }, {
237 'url': 'nrk:clip/7707d5a3-ebe7-434a-87d5-a3ebe7a34a70',
238 'only_matching': True,
853a71b6
S
239 }, {
240 'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
241 'only_matching': True,
d8d540cf
S
242 }]
243
244
245class NRKTVIE(NRKBaseIE):
246 IE_DESC = 'NRK TV and NRK Radio'
966815e1
S
247 _EPISODE_RE = r'(?P<id>[a-zA-Z]{4}\d{8})'
248 _VALID_URL = r'''(?x)
249 https?://
250 (?:tv|radio)\.nrk(?:super)?\.no/
33cc1ea5 251 (?:serie(?:/[^/]+){1,2}|program)/
966815e1
S
252 (?![Ee]pisodes)%s
253 (?:/\d{2}-\d{2}-\d{4})?
254 (?:\#del=(?P<part_id>\d+))?
255 ''' % _EPISODE_RE
93cffb14 256 _API_HOSTS = ('psapi-ne.nrk.no', 'psapi-we.nrk.no')
d8d540cf
S
257 _TESTS = [{
258 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
15699ec8 259 'md5': '9a167e54d04671eb6317a37b7bc8a280',
d8d540cf 260 'info_dict': {
18cf6381 261 'id': 'MUHH48000314AA',
d8d540cf 262 'ext': 'mp4',
18cf6381 263 'title': '20 spørsmål 23.05.2014',
d8d540cf 264 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
4e790117 265 'duration': 1741,
15699ec8 266 'series': '20 spørsmål',
7c5329e6 267 'episode': '23.05.2014',
d8d540cf 268 },
d8d540cf
S
269 }, {
270 'url': 'https://tv.nrk.no/program/mdfp15000514',
271 'info_dict': {
18cf6381 272 'id': 'MDFP15000514CA',
d8d540cf 273 'ext': 'mp4',
18cf6381 274 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
275 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
4e790117 276 'duration': 4605,
7c5329e6
S
277 'series': 'Kunnskapskanalen',
278 'episode': '24.05.2014',
279 },
280 'params': {
281 'skip_download': True,
d8d540cf 282 },
d8d540cf
S
283 }, {
284 # single playlist video
285 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
d8d540cf
S
286 'info_dict': {
287 'id': 'MSPO40010515-part2',
288 'ext': 'flv',
289 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
290 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
d8d540cf 291 },
7c5329e6
S
292 'params': {
293 'skip_download': True,
294 },
295 'expected_warnings': ['Video is geo restricted'],
296 'skip': 'particular part is not supported currently',
d8d540cf
S
297 }, {
298 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
299 'playlist': [{
d8d540cf 300 'info_dict': {
7c5329e6
S
301 'id': 'MSPO40010515AH',
302 'ext': 'mp4',
303 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 1)',
15699ec8 304 'description': 'md5:1f97a41f05a9486ee00c56f35f82993d',
7c5329e6
S
305 'duration': 772,
306 'series': 'Tour de Ski',
307 'episode': '06.01.2015',
308 },
309 'params': {
310 'skip_download': True,
d8d540cf
S
311 },
312 }, {
d8d540cf 313 'info_dict': {
7c5329e6
S
314 'id': 'MSPO40010515BH',
315 'ext': 'mp4',
316 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 2)',
15699ec8 317 'description': 'md5:1f97a41f05a9486ee00c56f35f82993d',
7c5329e6
S
318 'duration': 6175,
319 'series': 'Tour de Ski',
320 'episode': '06.01.2015',
321 },
322 'params': {
323 'skip_download': True,
d8d540cf
S
324 },
325 }],
326 'info_dict': {
327 'id': 'MSPO40010515',
7c5329e6 328 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
15699ec8 329 'description': 'md5:1f97a41f05a9486ee00c56f35f82993d',
7c5329e6
S
330 },
331 'expected_warnings': ['Video is geo restricted'],
332 }, {
333 'url': 'https://tv.nrk.no/serie/anno/KMTE50001317/sesong-3/episode-13',
334 'info_dict': {
335 'id': 'KMTE50001317AA',
336 'ext': 'mp4',
337 'title': 'Anno 13:30',
338 'description': 'md5:11d9613661a8dbe6f9bef54e3a4cbbfa',
339 'duration': 2340,
340 'series': 'Anno',
341 'episode': '13:30',
342 'season_number': 3,
343 'episode_number': 13,
344 },
345 'params': {
346 'skip_download': True,
347 },
348 }, {
349 'url': 'https://tv.nrk.no/serie/nytt-paa-nytt/MUHH46000317/27-01-2017',
350 'info_dict': {
351 'id': 'MUHH46000317AA',
352 'ext': 'mp4',
353 'title': 'Nytt på Nytt 27.01.2017',
354 'description': 'md5:5358d6388fba0ea6f0b6d11c48b9eb4b',
355 'duration': 1796,
356 'series': 'Nytt på nytt',
357 'episode': '27.01.2017',
358 },
359 'params': {
360 'skip_download': True,
d8d540cf 361 },
d8d540cf
S
362 }, {
363 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
364 'only_matching': True,
33cc1ea5
S
365 }, {
366 'url': 'https://tv.nrk.no/serie/lindmo/2018/MUHU11006318/avspiller',
367 'only_matching': True,
d8d540cf
S
368 }]
369
dfb2e1a3 370
79fd7320
S
371class NRKTVEpisodeIE(InfoExtractor):
372 _VALID_URL = r'https?://tv\.nrk\.no/serie/(?P<id>[^/]+/sesong/\d+/episode/\d+)'
373 _TEST = {
374 'url': 'https://tv.nrk.no/serie/backstage/sesong/1/episode/8',
375 'info_dict': {
376 'id': 'MSUI14000816AA',
377 'ext': 'mp4',
378 'title': 'Backstage 8:30',
379 'description': 'md5:de6ca5d5a2d56849e4021f2bf2850df4',
380 'duration': 1320,
381 'series': 'Backstage',
382 'season_number': 1,
383 'episode_number': 8,
384 'episode': '8:30',
385 },
386 'params': {
387 'skip_download': True,
388 },
389 }
390
391 def _real_extract(self, url):
392 display_id = self._match_id(url)
393
394 webpage = self._download_webpage(url, display_id)
395
396 nrk_id = self._parse_json(
397 self._search_regex(JSON_LD_RE, webpage, 'JSON-LD', group='json_ld'),
398 display_id)['@id']
399
400 assert re.match(NRKTVIE._EPISODE_RE, nrk_id)
401 return self.url_result(
402 'nrk:%s' % nrk_id, ie=NRKIE.ie_key(), video_id=nrk_id)
403
404
4b3ee098
S
405class NRKTVSerieBaseIE(InfoExtractor):
406 def _extract_series(self, webpage, display_id, fatal=True):
407 config = self._parse_json(
408 self._search_regex(
15699ec8
S
409 (r'INITIAL_DATA_*\s*=\s*({.+?})\s*;',
410 r'({.+?})\s*,\s*"[^"]+"\s*\)\s*</script>'),
411 webpage, 'config', default='{}' if not fatal else NO_DEFAULT),
4b3ee098
S
412 display_id, fatal=False)
413 if not config:
414 return
15699ec8
S
415 return try_get(
416 config,
417 (lambda x: x['initialState']['series'], lambda x: x['series']),
418 dict)
419
420 def _extract_seasons(self, seasons):
421 if not isinstance(seasons, list):
422 return []
423 entries = []
424 for season in seasons:
425 entries.extend(self._extract_episodes(season))
426 return entries
4b3ee098
S
427
428 def _extract_episodes(self, season):
4b3ee098 429 if not isinstance(season, dict):
15699ec8
S
430 return []
431 return self._extract_entries(season.get('episodes'))
432
433 def _extract_entries(self, entry_list):
434 if not isinstance(entry_list, list):
435 return []
436 entries = []
437 for episode in entry_list:
4b3ee098
S
438 nrk_id = episode.get('prfId')
439 if not nrk_id or not isinstance(nrk_id, compat_str):
440 continue
441 entries.append(self.url_result(
442 'nrk:%s' % nrk_id, ie=NRKIE.ie_key(), video_id=nrk_id))
443 return entries
444
445
446class NRKTVSeasonIE(NRKTVSerieBaseIE):
447 _VALID_URL = r'https?://tv\.nrk\.no/serie/[^/]+/sesong/(?P<id>\d+)'
448 _TEST = {
449 'url': 'https://tv.nrk.no/serie/backstage/sesong/1',
450 'info_dict': {
451 'id': '1',
452 'title': 'Sesong 1',
453 },
454 'playlist_mincount': 30,
455 }
456
457 @classmethod
458 def suitable(cls, url):
459 return (False if NRKTVIE.suitable(url) or NRKTVEpisodeIE.suitable(url)
460 else super(NRKTVSeasonIE, cls).suitable(url))
461
462 def _real_extract(self, url):
463 display_id = self._match_id(url)
464
465 webpage = self._download_webpage(url, display_id)
466
467 series = self._extract_series(webpage, display_id)
468
469 season = next(
470 s for s in series['seasons']
471 if int(display_id) == s.get('seasonNumber'))
472
473 title = try_get(season, lambda x: x['titles']['title'], compat_str)
474 return self.playlist_result(
475 self._extract_episodes(season), display_id, title)
476
477
478class NRKTVSeriesIE(NRKTVSerieBaseIE):
479 _VALID_URL = r'https?://(?:tv|radio)\.nrk(?:super)?\.no/serie/(?P<id>[^/]+)'
480 _ITEM_RE = r'(?:data-season=["\']|id=["\']season-)(?P<id>\d+)'
481 _TESTS = [{
15699ec8 482 # new layout, seasons
4b3ee098
S
483 'url': 'https://tv.nrk.no/serie/backstage',
484 'info_dict': {
485 'id': 'backstage',
486 'title': 'Backstage',
487 'description': 'md5:c3ec3a35736fca0f9e1207b5511143d3',
488 },
489 'playlist_mincount': 60,
490 }, {
15699ec8 491 # new layout, instalments
4b3ee098
S
492 'url': 'https://tv.nrk.no/serie/groenn-glede',
493 'info_dict': {
494 'id': 'groenn-glede',
495 'title': 'Grønn glede',
496 'description': 'md5:7576e92ae7f65da6993cf90ee29e4608',
497 },
15699ec8 498 'playlist_mincount': 10,
4b3ee098 499 }, {
15699ec8
S
500 # old layout
501 'url': 'https://tv.nrksuper.no/serie/labyrint',
4b3ee098
S
502 'info_dict': {
503 'id': 'labyrint',
504 'title': 'Labyrint',
15699ec8 505 'description': 'md5:318b597330fdac5959247c9b69fdb1ec',
4b3ee098
S
506 },
507 'playlist_mincount': 3,
508 }, {
509 'url': 'https://tv.nrk.no/serie/broedrene-dal-og-spektralsteinene',
510 'only_matching': True,
511 }, {
512 'url': 'https://tv.nrk.no/serie/saving-the-human-race',
513 'only_matching': True,
514 }, {
515 'url': 'https://tv.nrk.no/serie/postmann-pat',
516 'only_matching': True,
517 }]
518
519 @classmethod
520 def suitable(cls, url):
521 return (
522 False if any(ie.suitable(url)
523 for ie in (NRKTVIE, NRKTVEpisodeIE, NRKTVSeasonIE))
524 else super(NRKTVSeriesIE, cls).suitable(url))
525
526 def _real_extract(self, url):
527 series_id = self._match_id(url)
528
529 webpage = self._download_webpage(url, series_id)
530
531 # New layout (e.g. https://tv.nrk.no/serie/backstage)
532 series = self._extract_series(webpage, series_id, fatal=False)
533 if series:
534 title = try_get(series, lambda x: x['titles']['title'], compat_str)
535 description = try_get(
536 series, lambda x: x['titles']['subtitle'], compat_str)
537 entries = []
15699ec8
S
538 entries.extend(self._extract_seasons(series.get('seasons')))
539 entries.extend(self._extract_entries(series.get('instalments')))
c976873c 540 entries.extend(self._extract_episodes(series.get('extraMaterial')))
4b3ee098
S
541 return self.playlist_result(entries, series_id, title, description)
542
15699ec8 543 # Old layout (e.g. https://tv.nrksuper.no/serie/labyrint)
4b3ee098
S
544 entries = [
545 self.url_result(
546 'https://tv.nrk.no/program/Episodes/{series}/{season}'.format(
547 series=series_id, season=season_id))
548 for season_id in re.findall(self._ITEM_RE, webpage)
549 ]
550
551 title = self._html_search_meta(
552 'seriestitle', webpage,
553 'title', default=None) or self._og_search_title(
554 webpage, fatal=False)
15699ec8
S
555 if title:
556 title = self._search_regex(
557 r'NRK (?:Super )?TV\s*[-–]\s*(.+)', title, 'title', default=title)
4b3ee098
S
558
559 description = self._html_search_meta(
560 'series_description', webpage,
561 'description', default=None) or self._og_search_description(webpage)
562
563 return self.playlist_result(entries, series_id, title, description)
564
565
c80db5d3
S
566class NRKTVDirekteIE(NRKTVIE):
567 IE_DESC = 'NRK TV Direkte and NRK Radio Direkte'
568 _VALID_URL = r'https?://(?:tv|radio)\.nrk\.no/direkte/(?P<id>[^/?#&]+)'
569
570 _TESTS = [{
571 'url': 'https://tv.nrk.no/direkte/nrk1',
572 'only_matching': True,
573 }, {
574 'url': 'https://radio.nrk.no/direkte/p1_oslo_akershus',
575 'only_matching': True,
576 }]
577
578
966815e1
S
579class NRKPlaylistBaseIE(InfoExtractor):
580 def _extract_description(self, webpage):
581 pass
582
583 def _real_extract(self, url):
584 playlist_id = self._match_id(url)
585
586 webpage = self._download_webpage(url, playlist_id)
587
588 entries = [
589 self.url_result('nrk:%s' % video_id, NRKIE.ie_key())
590 for video_id in re.findall(self._ITEM_RE, webpage)
591 ]
592
593 playlist_title = self. _extract_title(webpage)
594 playlist_description = self._extract_description(webpage)
595
596 return self.playlist_result(
597 entries, playlist_id, playlist_title, playlist_description)
faa1b5c2 598
966815e1
S
599
600class NRKPlaylistIE(NRKPlaylistBaseIE):
601 _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
602 _ITEM_RE = r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"'
a0914154 603 _TESTS = [{
faa1b5c2
S
604 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
605 'info_dict': {
606 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
607 'title': 'Gjenopplev den historiske solformørkelsen',
608 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
609 },
a0914154
S
610 'playlist_count': 2,
611 }, {
612 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
613 'info_dict': {
614 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
615 'title': 'Rivertonprisen til Karin Fossum',
616 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
617 },
15699ec8 618 'playlist_count': 2,
a0914154 619 }]
faa1b5c2 620
966815e1
S
621 def _extract_title(self, webpage):
622 return self._og_search_title(webpage, fatal=False)
faa1b5c2 623
966815e1
S
624 def _extract_description(self, webpage):
625 return self._og_search_description(webpage)
faa1b5c2 626
faa1b5c2 627
966815e1
S
628class NRKTVEpisodesIE(NRKPlaylistBaseIE):
629 _VALID_URL = r'https?://tv\.nrk\.no/program/[Ee]pisodes/[^/]+/(?P<id>\d+)'
630 _ITEM_RE = r'data-episode=["\']%s' % NRKTVIE._EPISODE_RE
631 _TESTS = [{
632 'url': 'https://tv.nrk.no/program/episodes/nytt-paa-nytt/69031',
633 'info_dict': {
634 'id': '69031',
635 'title': 'Nytt på nytt, sesong: 201210',
636 },
637 'playlist_count': 4,
638 }]
faa1b5c2 639
966815e1
S
640 def _extract_title(self, webpage):
641 return self._html_search_regex(
642 r'<h1>([^<]+)</h1>', webpage, 'title', fatal=False)
faa1b5c2
S
643
644
3099b312
S
645class NRKSkoleIE(InfoExtractor):
646 IE_DESC = 'NRK Skole'
971e3b75 647 _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
3099b312
S
648
649 _TESTS = [{
971e3b75
S
650 'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
651 'md5': '6bc936b01f9dd8ed45bc58b252b2d9b6',
3099b312
S
652 'info_dict': {
653 'id': '6021',
971e3b75 654 'ext': 'mp4',
3099b312
S
655 'title': 'Genetikk og eneggede tvillinger',
656 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
657 'duration': 399,
658 },
659 }, {
971e3b75 660 'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
61140904 661 'only_matching': True,
3099b312
S
662 }]
663
664 def _real_extract(self, url):
971e3b75
S
665 video_id = self._match_id(url)
666
667 webpage = self._download_webpage(
668 'https://mimir.nrk.no/plugin/1.0/static?mediaId=%s' % video_id,
669 video_id)
3099b312 670
971e3b75
S
671 nrk_id = self._parse_json(
672 self._search_regex(
673 r'<script[^>]+type=["\']application/json["\'][^>]*>({.+?})</script>',
674 webpage, 'application json'),
675 video_id)['activeMedia']['psId']
3099b312 676
3099b312 677 return self.url_result('nrk:%s' % nrk_id)