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