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