[nrktv] Add support for new season and serie URL schema
[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     NO_DEFAULT,
16     parse_age_limit,
17     parse_duration,
18     try_get,
19 )
20
21
22 class NRKBaseIE(InfoExtractor):
23     _GEO_COUNTRIES = ['NO']
24
25     _api_host = None
26
27     def _real_extract(self, url):
28         video_id = self._match_id(url)
29
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
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
47         conviva = data.get('convivaStatistics') or {}
48         live = (data.get('mediaElementType') == 'Live' or
49                 data.get('isLive') is True or conviva.get('isLive'))
50
51         def make_title(t):
52             return self._live_title(t) if live else t
53
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
63                 formats = self._extract_akamai_formats(asset_url, video_id)
64                 if not formats:
65                     continue
66                 self._sort_formats(formats)
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
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:
80                         subtitles.setdefault('no', []).append({
81                             'url': compat_urllib_parse_unquote(subtitle_url)
82                         })
83                 entries.append({
84                     'id': asset.get('carrierId') or entry_id,
85                     'title': make_title(entry_title),
86                     'duration': duration,
87                     'subtitles': subtitles,
88                     'formats': formats,
89                 })
90
91         if not entries:
92             media_url = data.get('mediaUrl')
93             if media_url:
94                 formats = self._extract_akamai_formats(media_url, video_id)
95                 self._sort_formats(formats)
96                 duration = parse_duration(data.get('duration'))
97                 entries = [{
98                     'id': video_id,
99                     'title': make_title(title),
100                     'duration': duration,
101                     'formats': formats,
102                 }]
103
104         if not entries:
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             }
110             message_type = data.get('messageType', '')
111             # Can be ProgramIsGeoBlocked or ChannelIsGeoBlocked*
112             if 'IsGeoBlocked' in message_type:
113                 self.raise_geo_restricted(
114                     msg=MESSAGES.get('ProgramIsGeoBlocked'),
115                     countries=self._GEO_COUNTRIES)
116             raise ExtractorError(
117                 '%s said: %s' % (self.IE_NAME, MESSAGES.get(
118                     message_type, message_type)),
119                 expected=True)
120
121         series = conviva.get('seriesName') or data.get('seriesTitle')
122         episode = conviva.get('episodeName') or data.get('episodeNumberOrDate')
123
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 = [
130                 r'/s(?P<season>\d{,2})e(?P<episode>\d{,2})\.',
131                 r'/sesong-(?P<season>\d{,2})/episode-(?P<episode>\d{,2})',
132             ]
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'))
139
140         thumbnails = None
141         images = data.get('images')
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')
152         category = data.get('mediaAnalytics', {}).get('category')
153
154         common_info = {
155             'description': description,
156             'series': series,
157             'episode': episode,
158             'season_number': season_number,
159             'episode_number': episode_number,
160             'categories': [category] if category else None,
161             'age_limit': parse_age_limit(data.get('legalAge')),
162             'thumbnails': thumbnails,
163         }
164
165         vcodec = 'none' if data.get('mediaType') == 'Audio' else None
166
167         for entry in entries:
168             entry.update(common_info)
169             for f in entry['formats']:
170                 f['vcodec'] = vcodec
171
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
195         return self.playlist_result(entries, video_id, title, description)
196
197
198 class NRKIE(NRKBaseIE):
199     _VALID_URL = r'''(?x)
200                         (?:
201                             nrk:|
202                             https?://
203                                 (?:
204                                     (?:www\.)?nrk\.no/video/PS\*|
205                                     v8[-.]psapi\.nrk\.no/mediaelement/
206                                 )
207                             )
208                             (?P<id>[^?#&]+)
209                         '''
210     _API_HOSTS = ('psapi.nrk.no', 'v8-psapi.nrk.no')
211     _TESTS = [{
212         # video
213         'url': 'http://www.nrk.no/video/PS*150533',
214         'md5': '2f7f6eeb2aacdd99885f355428715cfa',
215         'info_dict': {
216             'id': '150533',
217             'ext': 'mp4',
218             'title': 'Dompap og andre fugler i Piip-Show',
219             'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
220             'duration': 263,
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         }
233     }, {
234         'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
235         'only_matching': True,
236     }, {
237         'url': 'nrk:clip/7707d5a3-ebe7-434a-87d5-a3ebe7a34a70',
238         'only_matching': True,
239     }, {
240         'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
241         'only_matching': True,
242     }]
243
244
245 class NRKTVIE(NRKBaseIE):
246     IE_DESC = 'NRK TV and NRK Radio'
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/
251                             (?:serie/[^/]+|program)/
252                             (?![Ee]pisodes)%s
253                             (?:/\d{2}-\d{2}-\d{4})?
254                             (?:\#del=(?P<part_id>\d+))?
255                     ''' % _EPISODE_RE
256     _API_HOSTS = ('psapi-ne.nrk.no', 'psapi-we.nrk.no')
257     _TESTS = [{
258         'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
259         'md5': '4e9ca6629f09e588ed240fb11619922a',
260         'info_dict': {
261             'id': 'MUHH48000314AA',
262             'ext': 'mp4',
263             'title': '20 spørsmål 23.05.2014',
264             'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
265             'duration': 1741,
266             'series': '20 spørsmål - TV',
267             'episode': '23.05.2014',
268         },
269     }, {
270         'url': 'https://tv.nrk.no/program/mdfp15000514',
271         'info_dict': {
272             'id': 'MDFP15000514CA',
273             'ext': 'mp4',
274             'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
275             'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
276             'duration': 4605,
277             'series': 'Kunnskapskanalen',
278             'episode': '24.05.2014',
279         },
280         'params': {
281             'skip_download': True,
282         },
283     }, {
284         # single playlist video
285         'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
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',
291         },
292         'params': {
293             'skip_download': True,
294         },
295         'expected_warnings': ['Video is geo restricted'],
296         'skip': 'particular part is not supported currently',
297     }, {
298         'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
299         'playlist': [{
300             'info_dict': {
301                 'id': 'MSPO40010515AH',
302                 'ext': 'mp4',
303                 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 1)',
304                 'description': 'md5:c03aba1e917561eface5214020551b7a',
305                 'duration': 772,
306                 'series': 'Tour de Ski',
307                 'episode': '06.01.2015',
308             },
309             'params': {
310                 'skip_download': True,
311             },
312         }, {
313             'info_dict': {
314                 'id': 'MSPO40010515BH',
315                 'ext': 'mp4',
316                 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 2)',
317                 'description': 'md5:c03aba1e917561eface5214020551b7a',
318                 'duration': 6175,
319                 'series': 'Tour de Ski',
320                 'episode': '06.01.2015',
321             },
322             'params': {
323                 'skip_download': True,
324             },
325         }],
326         'info_dict': {
327             'id': 'MSPO40010515',
328             'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
329             'description': 'md5:c03aba1e917561eface5214020551b7a',
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,
361         },
362     }, {
363         'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
364         'only_matching': True,
365     }]
366
367
368 class NRKTVEpisodeIE(InfoExtractor):
369     _VALID_URL = r'https?://tv\.nrk\.no/serie/(?P<id>[^/]+/sesong/\d+/episode/\d+)'
370     _TEST = {
371         'url': 'https://tv.nrk.no/serie/backstage/sesong/1/episode/8',
372         'info_dict': {
373             'id': 'MSUI14000816AA',
374             'ext': 'mp4',
375             'title': 'Backstage 8:30',
376             'description': 'md5:de6ca5d5a2d56849e4021f2bf2850df4',
377             'duration': 1320,
378             'series': 'Backstage',
379             'season_number': 1,
380             'episode_number': 8,
381             'episode': '8:30',
382         },
383         'params': {
384             'skip_download': True,
385         },
386     }
387
388     def _real_extract(self, url):
389         display_id = self._match_id(url)
390
391         webpage = self._download_webpage(url, display_id)
392
393         nrk_id = self._parse_json(
394             self._search_regex(JSON_LD_RE, webpage, 'JSON-LD', group='json_ld'),
395             display_id)['@id']
396
397         assert re.match(NRKTVIE._EPISODE_RE, nrk_id)
398         return self.url_result(
399             'nrk:%s' % nrk_id, ie=NRKIE.ie_key(), video_id=nrk_id)
400
401
402 class NRKTVSerieBaseIE(InfoExtractor):
403     def _extract_series(self, webpage, display_id, fatal=True):
404         config = self._parse_json(
405             self._search_regex(
406                 r'({.+?})\s*,\s*"[^"]+"\s*\)\s*</script>', webpage, 'config',
407                 default='{}' if not fatal else NO_DEFAULT),
408             display_id, fatal=False)
409         if not config:
410             return
411         return try_get(config, lambda x: x['series'], dict)
412
413     def _extract_episodes(self, season):
414         entries = []
415         if not isinstance(season, dict):
416             return entries
417         episodes = season.get('episodes')
418         if not isinstance(episodes, list):
419             return entries
420         for episode in episodes:
421             nrk_id = episode.get('prfId')
422             if not nrk_id or not isinstance(nrk_id, compat_str):
423                 continue
424             entries.append(self.url_result(
425                 'nrk:%s' % nrk_id, ie=NRKIE.ie_key(), video_id=nrk_id))
426         return entries
427
428
429 class NRKTVSeasonIE(NRKTVSerieBaseIE):
430     _VALID_URL = r'https?://tv\.nrk\.no/serie/[^/]+/sesong/(?P<id>\d+)'
431     _TEST = {
432         'url': 'https://tv.nrk.no/serie/backstage/sesong/1',
433         'info_dict': {
434             'id': '1',
435             'title': 'Sesong 1',
436         },
437         'playlist_mincount': 30,
438     }
439
440     @classmethod
441     def suitable(cls, url):
442         return (False if NRKTVIE.suitable(url) or NRKTVEpisodeIE.suitable(url)
443                 else super(NRKTVSeasonIE, cls).suitable(url))
444
445     def _real_extract(self, url):
446         display_id = self._match_id(url)
447
448         webpage = self._download_webpage(url, display_id)
449
450         series = self._extract_series(webpage, display_id)
451
452         season = next(
453             s for s in series['seasons']
454             if int(display_id) == s.get('seasonNumber'))
455
456         title = try_get(season, lambda x: x['titles']['title'], compat_str)
457         return self.playlist_result(
458             self._extract_episodes(season), display_id, title)
459
460
461 class NRKTVSeriesIE(NRKTVSerieBaseIE):
462     _VALID_URL = r'https?://(?:tv|radio)\.nrk(?:super)?\.no/serie/(?P<id>[^/]+)'
463     _ITEM_RE = r'(?:data-season=["\']|id=["\']season-)(?P<id>\d+)'
464     _TESTS = [{
465         # new layout
466         'url': 'https://tv.nrk.no/serie/backstage',
467         'info_dict': {
468             'id': 'backstage',
469             'title': 'Backstage',
470             'description': 'md5:c3ec3a35736fca0f9e1207b5511143d3',
471         },
472         'playlist_mincount': 60,
473     }, {
474         # old layout
475         'url': 'https://tv.nrk.no/serie/groenn-glede',
476         'info_dict': {
477             'id': 'groenn-glede',
478             'title': 'Grønn glede',
479             'description': 'md5:7576e92ae7f65da6993cf90ee29e4608',
480         },
481         'playlist_mincount': 9,
482     }, {
483         'url': 'http://tv.nrksuper.no/serie/labyrint',
484         'info_dict': {
485             'id': 'labyrint',
486             'title': 'Labyrint',
487             'description': 'md5:58afd450974c89e27d5a19212eee7115',
488         },
489         'playlist_mincount': 3,
490     }, {
491         'url': 'https://tv.nrk.no/serie/broedrene-dal-og-spektralsteinene',
492         'only_matching': True,
493     }, {
494         'url': 'https://tv.nrk.no/serie/saving-the-human-race',
495         'only_matching': True,
496     }, {
497         'url': 'https://tv.nrk.no/serie/postmann-pat',
498         'only_matching': True,
499     }]
500
501     @classmethod
502     def suitable(cls, url):
503         return (
504             False if any(ie.suitable(url)
505                          for ie in (NRKTVIE, NRKTVEpisodeIE, NRKTVSeasonIE))
506             else super(NRKTVSeriesIE, cls).suitable(url))
507
508     def _real_extract(self, url):
509         series_id = self._match_id(url)
510
511         webpage = self._download_webpage(url, series_id)
512
513         # New layout (e.g. https://tv.nrk.no/serie/backstage)
514         series = self._extract_series(webpage, series_id, fatal=False)
515         if series:
516             title = try_get(series, lambda x: x['titles']['title'], compat_str)
517             description = try_get(
518                 series, lambda x: x['titles']['subtitle'], compat_str)
519             entries = []
520             for season in series['seasons']:
521                 entries.extend(self._extract_episodes(season))
522             return self.playlist_result(entries, series_id, title, description)
523
524         # Old layout (e.g. https://tv.nrk.no/serie/groenn-glede)
525         entries = [
526             self.url_result(
527                 'https://tv.nrk.no/program/Episodes/{series}/{season}'.format(
528                     series=series_id, season=season_id))
529             for season_id in re.findall(self._ITEM_RE, webpage)
530         ]
531
532         title = self._html_search_meta(
533             'seriestitle', webpage,
534             'title', default=None) or self._og_search_title(
535             webpage, fatal=False)
536
537         description = self._html_search_meta(
538             'series_description', webpage,
539             'description', default=None) or self._og_search_description(webpage)
540
541         return self.playlist_result(entries, series_id, title, description)
542
543
544 class NRKTVDirekteIE(NRKTVIE):
545     IE_DESC = 'NRK TV Direkte and NRK Radio Direkte'
546     _VALID_URL = r'https?://(?:tv|radio)\.nrk\.no/direkte/(?P<id>[^/?#&]+)'
547
548     _TESTS = [{
549         'url': 'https://tv.nrk.no/direkte/nrk1',
550         'only_matching': True,
551     }, {
552         'url': 'https://radio.nrk.no/direkte/p1_oslo_akershus',
553         'only_matching': True,
554     }]
555
556
557 class NRKPlaylistBaseIE(InfoExtractor):
558     def _extract_description(self, webpage):
559         pass
560
561     def _real_extract(self, url):
562         playlist_id = self._match_id(url)
563
564         webpage = self._download_webpage(url, playlist_id)
565
566         entries = [
567             self.url_result('nrk:%s' % video_id, NRKIE.ie_key())
568             for video_id in re.findall(self._ITEM_RE, webpage)
569         ]
570
571         playlist_title = self. _extract_title(webpage)
572         playlist_description = self._extract_description(webpage)
573
574         return self.playlist_result(
575             entries, playlist_id, playlist_title, playlist_description)
576
577
578 class NRKPlaylistIE(NRKPlaylistBaseIE):
579     _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
580     _ITEM_RE = r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"'
581     _TESTS = [{
582         'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
583         'info_dict': {
584             'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
585             'title': 'Gjenopplev den historiske solformørkelsen',
586             'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
587         },
588         'playlist_count': 2,
589     }, {
590         'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
591         'info_dict': {
592             'id': 'rivertonprisen-til-karin-fossum-1.12266449',
593             'title': 'Rivertonprisen til Karin Fossum',
594             'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
595         },
596         'playlist_count': 5,
597     }]
598
599     def _extract_title(self, webpage):
600         return self._og_search_title(webpage, fatal=False)
601
602     def _extract_description(self, webpage):
603         return self._og_search_description(webpage)
604
605
606 class NRKTVEpisodesIE(NRKPlaylistBaseIE):
607     _VALID_URL = r'https?://tv\.nrk\.no/program/[Ee]pisodes/[^/]+/(?P<id>\d+)'
608     _ITEM_RE = r'data-episode=["\']%s' % NRKTVIE._EPISODE_RE
609     _TESTS = [{
610         'url': 'https://tv.nrk.no/program/episodes/nytt-paa-nytt/69031',
611         'info_dict': {
612             'id': '69031',
613             'title': 'Nytt på nytt, sesong: 201210',
614         },
615         'playlist_count': 4,
616     }]
617
618     def _extract_title(self, webpage):
619         return self._html_search_regex(
620             r'<h1>([^<]+)</h1>', webpage, 'title', fatal=False)
621
622
623 class NRKSkoleIE(InfoExtractor):
624     IE_DESC = 'NRK Skole'
625     _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
626
627     _TESTS = [{
628         'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
629         'md5': '6bc936b01f9dd8ed45bc58b252b2d9b6',
630         'info_dict': {
631             'id': '6021',
632             'ext': 'mp4',
633             'title': 'Genetikk og eneggede tvillinger',
634             'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
635             'duration': 399,
636         },
637     }, {
638         'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
639         'only_matching': True,
640     }]
641
642     def _real_extract(self, url):
643         video_id = self._match_id(url)
644
645         webpage = self._download_webpage(
646             'https://mimir.nrk.no/plugin/1.0/static?mediaId=%s' % video_id,
647             video_id)
648
649         nrk_id = self._parse_json(
650             self._search_regex(
651                 r'<script[^>]+type=["\']application/json["\'][^>]*>({.+?})</script>',
652                 webpage, 'application json'),
653             video_id)['activeMedia']['psId']
654
655         return self.url_result('nrk:%s' % nrk_id)