2 from __future__
import unicode_literals
6 from .common
import InfoExtractor
9 compat_urllib_parse_unquote
,
24 class NRKBaseIE(InfoExtractor
):
25 _GEO_COUNTRIES
= ['NO']
28 class NRKIE(NRKBaseIE
):
34 (?:www\.)?nrk\.no/video/(?:PS\*|[^_]+_)|
35 v8[-.]psapi\.nrk\.no/mediaelement/
43 'url': 'http://www.nrk.no/video/PS*150533',
44 'md5': '706f34cdf1322577589e369e522b50ef',
48 'title': 'Dompap og andre fugler i Piip-Show',
49 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
54 'url': 'http://www.nrk.no/video/PS*154915',
59 'title': 'Slik høres internett ut når du er blind',
60 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
64 'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
65 'only_matching': True,
67 'url': 'nrk:clip/7707d5a3-ebe7-434a-87d5-a3ebe7a34a70',
68 'only_matching': True,
70 'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
71 'only_matching': True,
73 'url': 'https://www.nrk.no/video/dompap-og-andre-fugler-i-piip-show_150533',
74 'only_matching': True,
76 'url': 'https://www.nrk.no/video/humor/kommentatorboksen-reiser-til-sjos_d1fda11f-a4ad-437a-a374-0398bc84e999',
77 'only_matching': True,
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')
85 playable
= manifest
['playable']
88 for asset
in playable
['assets']:
89 if not isinstance(asset
, dict):
91 if asset
.get('encrypted'):
93 format_url
= url_or_none(asset
.get('url'))
96 if asset
.get('format') == 'HLS' or determine_ext(format_url
) == 'm3u8':
97 formats
.extend(self
._extract
_m
3u8_formats
(
98 format_url
, video_id
, 'mp4', entry_protocol
='m3u8_native',
99 m3u8_id
='hls', fatal
=False))
100 self
._sort
_formats
(formats
)
102 data
= self
._download
_json
(
103 'http://psapi.nrk.no/playback/metadata/%s' % video_id
,
104 video_id
, 'Downloading metadata JSON')
106 preplay
= data
['preplay']
107 titles
= preplay
['titles']
108 title
= titles
['title']
109 alt_title
= titles
.get('subtitle')
111 description
= preplay
.get('description')
112 duration
= parse_duration(playable
.get('duration')) or parse_duration(data
.get('duration'))
115 for image
in try_get(
116 preplay
, lambda x
: x
['poster']['images'], list) or []:
117 if not isinstance(image
, dict):
119 image_url
= url_or_none(image
.get('url'))
124 'width': int_or_none(image
.get('pixelWidth')),
125 'height': int_or_none(image
.get('pixelHeight')),
131 'alt_title': alt_title
,
132 'description': description
,
133 'duration': duration
,
134 'thumbnails': thumbnails
,
138 def _real_extract(self
, url
):
139 video_id
= self
._match
_id
(url
)
140 return self
._extract
_from
_playback
(video_id
)
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)
148 (?:tv|radio)\.nrk(?:super)?\.no/
149 (?:serie(?:/[^/]+){1,2}|program)/
151 (?:/\d{2}-\d{2}-\d{4})?
152 (?:\#del=(?P<part_id>\d+))?
154 _API_HOSTS
= ('psapi-ne.nrk.no', 'psapi-we.nrk.no')
156 'url': 'https://tv.nrk.no/program/MDDP12000117',
157 'md5': '8270824df46ec629b66aeaa5796b36fb',
159 'id': 'MDDP12000117AA',
161 'title': 'Alarm Trolltunga',
162 'description': 'md5:46923a6e6510eefcce23d5ef2a58f2ce',
167 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
168 'md5': '9a167e54d04671eb6317a37b7bc8a280',
170 'id': 'MUHH48000314AA',
172 'title': '20 spørsmål 23.05.2014',
173 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
175 'series': '20 spørsmål',
176 'episode': '23.05.2014',
178 'skip': 'NoProgramRights',
180 'url': 'https://tv.nrk.no/program/mdfp15000514',
182 'id': 'MDFP15000514CA',
184 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
185 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
187 'series': 'Kunnskapskanalen',
188 'episode': '24.05.2014',
191 'skip_download': True,
194 # single playlist video
195 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
197 'id': 'MSPO40010515-part2',
199 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
200 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
203 'skip_download': True,
205 'expected_warnings': ['Video is geo restricted'],
206 'skip': 'particular part is not supported currently',
208 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
211 'id': 'MSPO40010515AH',
213 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 1)',
214 'description': 'md5:1f97a41f05a9486ee00c56f35f82993d',
216 'series': 'Tour de Ski',
217 'episode': '06.01.2015',
220 'skip_download': True,
224 'id': 'MSPO40010515BH',
226 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 2)',
227 'description': 'md5:1f97a41f05a9486ee00c56f35f82993d',
229 'series': 'Tour de Ski',
230 'episode': '06.01.2015',
233 'skip_download': True,
237 'id': 'MSPO40010515',
238 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
239 'description': 'md5:1f97a41f05a9486ee00c56f35f82993d',
241 'expected_warnings': ['Video is geo restricted'],
243 'url': 'https://tv.nrk.no/serie/anno/KMTE50001317/sesong-3/episode-13',
245 'id': 'KMTE50001317AA',
247 'title': 'Anno 13:30',
248 'description': 'md5:11d9613661a8dbe6f9bef54e3a4cbbfa',
253 'episode_number': 13,
256 'skip_download': True,
259 'url': 'https://tv.nrk.no/serie/nytt-paa-nytt/MUHH46000317/27-01-2017',
261 'id': 'MUHH46000317AA',
263 'title': 'Nytt på Nytt 27.01.2017',
264 'description': 'md5:5358d6388fba0ea6f0b6d11c48b9eb4b',
266 'series': 'Nytt på nytt',
267 'episode': '27.01.2017',
270 'skip_download': True,
273 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
274 'only_matching': True,
276 'url': 'https://tv.nrk.no/serie/lindmo/2018/MUHU11006318/avspiller',
277 'only_matching': True,
282 def _extract_from_mediaelement(self
, video_id
):
283 api_hosts
= (self
._api
_host
, ) if self
._api
_host
else self
._API
_HOSTS
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])
292 self
._api
_host
= api_host
295 title
= data
.get('fullTitle') or data
.get('mainTitle') or data
['title']
296 video_id
= data
.get('id') or video_id
300 conviva
= data
.get('convivaStatistics') or {}
301 live
= (data
.get('mediaElementType') == 'Live'
302 or data
.get('isLive') is True or conviva
.get('isLive'))
305 return self
._live
_title
(t
) if live
else t
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')
316 formats
= self
._extract
_akamai
_formats
(asset_url
, video_id
)
319 self
._sort
_formats
(formats
)
321 # Some f4m streams may not work with hdcore in fragments' URLs
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']
327 entry_id
, entry_title
= video_id_and_title(num
)
328 duration
= parse_duration(asset
.get('duration'))
330 for subtitle
in ('webVtt', 'timedText'):
331 subtitle_url
= asset
.get('%sSubtitlesUrl' % subtitle
)
333 subtitles
.setdefault('no', []).append({
334 'url': compat_urllib_parse_unquote(subtitle_url
)
337 'id': asset
.get('carrierId') or entry_id
,
338 'title': make_title(entry_title
),
339 'duration': duration
,
340 'subtitles': subtitles
,
345 media_url
= data
.get('mediaUrl')
347 formats
= self
._extract
_akamai
_formats
(media_url
, video_id
)
348 self
._sort
_formats
(formats
)
349 duration
= parse_duration(data
.get('duration'))
352 'title': make_title(title
),
353 'duration': duration
,
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',
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
)),
375 series
= conviva
.get('seriesName') or data
.get('seriesTitle')
376 episode
= conviva
.get('episodeName') or data
.get('episodeNumberOrDate')
379 episode_number
= None
380 if data
.get('mediaElementType') == 'Episode':
381 _season_episode
= data
.get('scoresStatistics', {}).get('springStreamStream') or \
382 data
.get('relativeOriginUrl', '')
384 r
'/s(?P<season>\d{,2})e(?P<episode>\d{,2})\.',
385 r
'/sesong-(?P<season>\d{,2})/episode-(?P<episode>\d{,2})',
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'))
395 images
= data
.get('images')
396 if images
and isinstance(images
, dict):
397 web_images
= images
.get('webImages')
398 if isinstance(web_images
, list):
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')]
405 description
= data
.get('description')
406 category
= data
.get('mediaAnalytics', {}).get('category')
409 'description': description
,
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
,
419 vcodec
= 'none' if data
.get('mediaType') == 'Audio' else None
421 for entry
in entries
:
422 entry
.update(common_info
)
423 for f
in entry
['formats']:
426 points
= data
.get('shortIndexPoints')
427 if isinstance(points
, list):
429 for next_num
, point
in enumerate(points
, start
=1):
430 if not isinstance(point
, dict):
432 start_time
= parse_duration(point
.get('startPoint'))
433 if start_time
is None:
435 end_time
= parse_duration(
437 if next_num
== len(points
)
438 else points
[next_num
].get('startPoint'))
442 'start_time': start_time
,
443 'end_time': end_time
,
444 'title': point
.get('title'),
446 if chapters
and len(entries
) == 1:
447 entries
[0]['chapters'] = chapters
449 return self
.playlist_result(entries
, video_id
, title
, description
)
451 def _real_extract(self
, url
):
452 video_id
= self
._match
_id
(url
)
453 return self
._extract
_from
_mediaelement
(video_id
)
456 class NRKTVEpisodeIE(InfoExtractor
):
457 _VALID_URL
= r
'https?://tv\.nrk\.no/serie/(?P<id>[^/]+/sesong/\d+/episode/\d+)'
459 'url': 'https://tv.nrk.no/serie/hellums-kro/sesong/1/episode/2',
461 'id': 'MUHH36005220BA',
463 'title': 'Kro, krig og kjærlighet 2:6',
464 'description': 'md5:b32a7dc0b1ed27c8064f58b97bda4350',
466 'series': 'Hellums kro',
473 'skip_download': True,
476 'url': 'https://tv.nrk.no/serie/backstage/sesong/1/episode/8',
478 'id': 'MSUI14000816AA',
480 'title': 'Backstage 8:30',
481 'description': 'md5:de6ca5d5a2d56849e4021f2bf2850df4',
483 'series': 'Backstage',
489 'skip_download': True,
491 'skip': 'ProgramRightsHasExpired',
494 def _real_extract(self
, url
):
495 display_id
= self
._match
_id
(url
)
497 webpage
= self
._download
_webpage
(url
, display_id
)
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,
504 assert re.match(NRKTVIE._EPISODE_RE, nrk_id)
507 '_type
': 'url_transparent
',
509 'url
': 'nrk
:%s' % nrk_id,
510 'ie_key
': NRKIE.ie_key(),
515 class NRKTVSerieBaseIE(InfoExtractor):
516 def _extract_series(self, webpage, display_id, fatal=True):
517 config = self._parse_json(
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)
527 (lambda x: x['initialState']['series'], lambda x: x['series']),
530 def _extract_seasons(self, seasons):
531 if not isinstance(seasons, list):
534 for season in seasons:
535 entries.extend(self._extract_episodes(season))
538 def _extract_episodes(self, season):
539 if not isinstance(season, dict):
541 return self._extract_entries(season.get('episodes'))
543 def _extract_entries(self, entry_list):
544 if not isinstance(entry_list, list):
547 for episode in entry_list:
548 nrk_id = episode.get('prfId')
549 if not nrk_id or not isinstance(nrk_id, compat_str):
551 entries.append(self.url_result(
552 'nrk:%s' % nrk_id, ie=NRKIE.ie_key(), video_id=nrk_id))
556 class NRKTVSeasonIE(NRKTVSerieBaseIE):
557 _VALID_URL = r'https?://tv\.nrk\.no/serie/[^/]+/sesong/(?P<id>\d+)'
559 'url': 'https://tv.nrk.no/serie/backstage/sesong/1',
564 'playlist_mincount': 30,
568 def suitable(cls, url):
569 return (False if NRKTVIE.suitable(url) or NRKTVEpisodeIE.suitable(url)
570 else super(NRKTVSeasonIE, cls).suitable(url))
572 def _real_extract(self, url):
573 display_id = self._match_id(url)
575 webpage = self._download_webpage(url, display_id)
577 series = self._extract_series(webpage, display_id)
580 s for s in series['seasons']
581 if int(display_id) == s.get('seasonNumber'))
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)
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+)'
592 'url': 'https://tv.nrk.no/serie/blank',
596 'description': 'md5:7664b4e7e77dc6810cd3bca367c25b6e',
598 'playlist_mincount': 30,
600 # new layout, seasons
601 'url': 'https://tv.nrk.no/serie/backstage',
604 'title': 'Backstage',
605 'description': 'md5:c3ec3a35736fca0f9e1207b5511143d3',
607 'playlist_mincount': 60,
609 # new layout, instalments
610 'url': 'https://tv.nrk.no/serie/groenn-glede',
612 'id': 'groenn-glede',
613 'title': 'Grønn glede',
614 'description': 'md5:7576e92ae7f65da6993cf90ee29e4608',
616 'playlist_mincount': 10,
619 'url': 'https://tv.nrksuper.no/serie/labyrint',
623 'description': 'md5:318b597330fdac5959247c9b69fdb1ec',
625 'playlist_mincount': 3,
627 'url': 'https://tv.nrk.no/serie/broedrene-dal-og-spektralsteinene',
628 'only_matching': True,
630 'url': 'https://tv.nrk.no/serie/saving-the-human-race',
631 'only_matching': True,
633 'url': 'https://tv.nrk.no/serie/postmann-pat',
634 'only_matching': True,
638 def suitable(cls, url):
640 False if any(ie.suitable(url)
641 for ie in (NRKTVIE, NRKTVEpisodeIE, NRKTVSeasonIE))
642 else super(NRKTVSeriesIE, cls).suitable(url))
644 def _real_extract(self, url):
645 series_id = self._match_id(url)
647 webpage = self._download_webpage(url, series_id)
649 # New layout (e.g. https://tv.nrk.no/serie/backstage)
650 series = self._extract_series(webpage, series_id, fatal=False)
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)
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)
661 # Old layout (e.g. https://tv.nrksuper.no/serie/labyrint)
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)
669 title = self._html_search_meta(
670 'seriestitle', webpage,
671 'title', default=None) or self._og_search_title(
672 webpage, fatal=False)
674 title = self._search_regex(
675 r'NRK (?:Super )?TV\s*[-–]\s*(.+)', title, 'title', default=title)
677 description = self._html_search_meta(
678 'series_description', webpage,
679 'description', default=None) or self._og_search_description(webpage)
681 return self.playlist_result(entries, series_id, title, description)
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>[^/?#&]+)'
689 'url': 'https://tv.nrk.no/direkte/nrk1',
690 'only_matching': True,
692 'url': 'https://radio.nrk.no/direkte/p1_oslo_akershus',
693 'only_matching': True,
697 class NRKPlaylistBaseIE(InfoExtractor):
698 def _extract_description(self, webpage):
701 def _real_extract(self, url):
702 playlist_id = self._match_id(url)
704 webpage = self._download_webpage(url, playlist_id)
707 self.url_result('nrk:%s' % video_id, NRKIE.ie_key())
708 for video_id in re.findall(self._ITEM_RE, webpage)
711 playlist_title = self. _extract_title(webpage)
712 playlist_description = self._extract_description(webpage)
714 return self.playlist_result(
715 entries, playlist_id, playlist_title, playlist_description)
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="([^
"]+)"'
722 'url
': 'http
://www
.nrk
.no
/troms
/gjenopplev
-den
-historiske
-solformorkelsen
-1.12270763',
724 'id': 'gjenopplev
-den
-historiske
-solformorkelsen
-1.12270763',
725 'title
': 'Gjenopplev den historiske solformørkelsen
',
726 'description
': 'md5
:c2df8ea3bac5654a26fc2834a542feed
',
730 'url
': 'http
://www
.nrk
.no
/kultur
/bok
/rivertonprisen
-til
-karin
-fossum
-1.12266449',
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
.',
739 def _extract_title(self, webpage):
740 return self._og_search_title(webpage, fatal=False)
742 def _extract_description(self, webpage):
743 return self._og_search_description(webpage)
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
750 'url': 'https://tv.nrk.no/program/episodes/nytt-paa-nytt/69031',
753 'title': 'Nytt på nytt, sesong: 201210',
758 def _extract_title(self, webpage):
759 return self._html_search_regex(
760 r'<h1>([^<]+)</h1>', webpage, 'title', fatal=False)
763 class NRKSkoleIE(InfoExtractor):
764 IE_DESC = 'NRK Skole'
765 _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
768 'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
769 'md5': '18c12c3d071953c3bf8d54ef6b2587b7',
773 'title': 'Genetikk og eneggede tvillinger',
774 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
778 'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
779 'only_matching': True,
782 def _real_extract(self, url):
783 video_id = self._match_id(url)
785 webpage = self._download_webpage(
786 'https://mimir.nrk.no/plugin/1.0/static?mediaId=%s' % video_id,
789 nrk_id = self._parse_json(
791 r'<script[^>]+type=["\']application
/json
["\'][^>]*>({.+?})</script>',
792 webpage, 'application json'),
793 video_id)['activeMedia']['psId']
795 return self.url_result('nrk:%s' % nrk_id)