2 from __future__
import unicode_literals
6 from .common
import InfoExtractor
9 compat_urllib_parse_unquote
,
23 class NRKBaseIE(InfoExtractor
):
24 _GEO_COUNTRIES
= ['NO']
28 def _real_extract(self
, url
):
29 video_id
= self
._match
_id
(url
)
31 api_hosts
= (self
._api
_host
, ) if self
._api
_host
else self
._API
_HOSTS
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])
40 self
._api
_host
= api_host
43 title
= data
.get('fullTitle') or data
.get('mainTitle') or data
['title']
44 video_id
= data
.get('id') or video_id
48 conviva
= data
.get('convivaStatistics') or {}
49 live
= (data
.get('mediaElementType') == 'Live'
50 or data
.get('isLive') is True or conviva
.get('isLive'))
53 return self
._live
_title
(t
) if live
else t
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')
64 formats
= self
._extract
_akamai
_formats
(asset_url
, video_id
)
67 self
._sort
_formats
(formats
)
69 # Some f4m streams may not work with hdcore in fragments' URLs
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']
75 entry_id
, entry_title
= video_id_and_title(num
)
76 duration
= parse_duration(asset
.get('duration'))
78 for subtitle
in ('webVtt', 'timedText'):
79 subtitle_url
= asset
.get('%sSubtitlesUrl' % subtitle
)
81 subtitles
.setdefault('no', []).append({
82 'url': compat_urllib_parse_unquote(subtitle_url
)
85 'id': asset
.get('carrierId') or entry_id
,
86 'title': make_title(entry_title
),
88 'subtitles': subtitles
,
93 media_url
= data
.get('mediaUrl')
95 formats
= self
._extract
_akamai
_formats
(media_url
, video_id
)
96 self
._sort
_formats
(formats
)
97 duration
= parse_duration(data
.get('duration'))
100 'title': make_title(title
),
101 'duration': duration
,
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',
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
)),
123 series
= conviva
.get('seriesName') or data
.get('seriesTitle')
124 episode
= conviva
.get('episodeName') or data
.get('episodeNumberOrDate')
127 episode_number
= None
128 if data
.get('mediaElementType') == 'Episode':
129 _season_episode
= data
.get('scoresStatistics', {}).get('springStreamStream') or \
130 data
.get('relativeOriginUrl', '')
132 r
'/s(?P<season>\d{,2})e(?P<episode>\d{,2})\.',
133 r
'/sesong-(?P<season>\d{,2})/episode-(?P<episode>\d{,2})',
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'))
143 images
= data
.get('images')
144 if images
and isinstance(images
, dict):
145 web_images
= images
.get('webImages')
146 if isinstance(web_images
, list):
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')]
153 description
= data
.get('description')
154 category
= data
.get('mediaAnalytics', {}).get('category')
157 'description': description
,
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
,
167 vcodec
= 'none' if data
.get('mediaType') == 'Audio' else None
169 for entry
in entries
:
170 entry
.update(common_info
)
171 for f
in entry
['formats']:
174 points
= data
.get('shortIndexPoints')
175 if isinstance(points
, list):
177 for next_num
, point
in enumerate(points
, start
=1):
178 if not isinstance(point
, dict):
180 start_time
= parse_duration(point
.get('startPoint'))
181 if start_time
is None:
183 end_time
= parse_duration(
185 if next_num
== len(points
)
186 else points
[next_num
].get('startPoint'))
190 'start_time': start_time
,
191 'end_time': end_time
,
192 'title': point
.get('title'),
194 if chapters
and len(entries
) == 1:
195 entries
[0]['chapters'] = chapters
197 return self
.playlist_result(entries
, video_id
, title
, description
)
200 class NRKIE(NRKBaseIE
):
201 _VALID_URL
= r
'''(?x)
206 (?:www\.)?nrk\.no/video/PS\*|
207 v8[-.]psapi\.nrk\.no/mediaelement/
212 _API_HOSTS
= ('psapi.nrk.no', 'v8-psapi.nrk.no')
215 'url': 'http://www.nrk.no/video/PS*150533',
216 'md5': '706f34cdf1322577589e369e522b50ef',
220 'title': 'Dompap og andre fugler i Piip-Show',
221 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
226 'url': 'http://www.nrk.no/video/PS*154915',
231 'title': 'Slik høres internett ut når du er blind',
232 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
236 'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
237 'only_matching': True,
239 'url': 'nrk:clip/7707d5a3-ebe7-434a-87d5-a3ebe7a34a70',
240 'only_matching': True,
242 'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
243 'only_matching': True,
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)
252 (?:tv|radio)\.nrk(?:super)?\.no/
253 (?:serie(?:/[^/]+){1,2}|program)/
255 (?:/\d{2}-\d{2}-\d{4})?
256 (?:\#del=(?P<part_id>\d+))?
258 _API_HOSTS
= ('psapi-ne.nrk.no', 'psapi-we.nrk.no')
260 'url': 'https://tv.nrk.no/program/MDDP12000117',
261 'md5': '8270824df46ec629b66aeaa5796b36fb',
263 'id': 'MDDP12000117AA',
265 'title': 'Alarm Trolltunga',
266 'description': 'md5:46923a6e6510eefcce23d5ef2a58f2ce',
271 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
272 'md5': '9a167e54d04671eb6317a37b7bc8a280',
274 'id': 'MUHH48000314AA',
276 'title': '20 spørsmål 23.05.2014',
277 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
279 'series': '20 spørsmål',
280 'episode': '23.05.2014',
282 'skip': 'NoProgramRights',
284 'url': 'https://tv.nrk.no/program/mdfp15000514',
286 'id': 'MDFP15000514CA',
288 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
289 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
291 'series': 'Kunnskapskanalen',
292 'episode': '24.05.2014',
295 'skip_download': True,
298 # single playlist video
299 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
301 'id': 'MSPO40010515-part2',
303 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
304 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
307 'skip_download': True,
309 'expected_warnings': ['Video is geo restricted'],
310 'skip': 'particular part is not supported currently',
312 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
315 'id': 'MSPO40010515AH',
317 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 1)',
318 'description': 'md5:1f97a41f05a9486ee00c56f35f82993d',
320 'series': 'Tour de Ski',
321 'episode': '06.01.2015',
324 'skip_download': True,
328 'id': 'MSPO40010515BH',
330 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 2)',
331 'description': 'md5:1f97a41f05a9486ee00c56f35f82993d',
333 'series': 'Tour de Ski',
334 'episode': '06.01.2015',
337 'skip_download': True,
341 'id': 'MSPO40010515',
342 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
343 'description': 'md5:1f97a41f05a9486ee00c56f35f82993d',
345 'expected_warnings': ['Video is geo restricted'],
347 'url': 'https://tv.nrk.no/serie/anno/KMTE50001317/sesong-3/episode-13',
349 'id': 'KMTE50001317AA',
351 'title': 'Anno 13:30',
352 'description': 'md5:11d9613661a8dbe6f9bef54e3a4cbbfa',
357 'episode_number': 13,
360 'skip_download': True,
363 'url': 'https://tv.nrk.no/serie/nytt-paa-nytt/MUHH46000317/27-01-2017',
365 'id': 'MUHH46000317AA',
367 'title': 'Nytt på Nytt 27.01.2017',
368 'description': 'md5:5358d6388fba0ea6f0b6d11c48b9eb4b',
370 'series': 'Nytt på nytt',
371 'episode': '27.01.2017',
374 'skip_download': True,
377 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
378 'only_matching': True,
380 'url': 'https://tv.nrk.no/serie/lindmo/2018/MUHU11006318/avspiller',
381 'only_matching': True,
385 class NRKTVEpisodeIE(InfoExtractor
):
386 _VALID_URL
= r
'https?://tv\.nrk\.no/serie/(?P<id>[^/]+/sesong/\d+/episode/\d+)'
388 'url': 'https://tv.nrk.no/serie/hellums-kro/sesong/1/episode/2',
390 'id': 'MUHH36005220BA',
392 'title': 'Kro, krig og kjærlighet 2:6',
393 'description': 'md5:b32a7dc0b1ed27c8064f58b97bda4350',
395 'series': 'Hellums kro',
402 'skip_download': True,
405 'url': 'https://tv.nrk.no/serie/backstage/sesong/1/episode/8',
407 'id': 'MSUI14000816AA',
409 'title': 'Backstage 8:30',
410 'description': 'md5:de6ca5d5a2d56849e4021f2bf2850df4',
412 'series': 'Backstage',
418 'skip_download': True,
420 'skip': 'ProgramRightsHasExpired',
423 def _real_extract(self
, url
):
424 display_id
= self
._match
_id
(url
)
426 webpage
= self
._download
_webpage
(url
, display_id
)
428 nrk_id
= self
._parse
_json
(
429 self
._search
_regex
(JSON_LD_RE
, webpage
, 'JSON-LD', group
='json_ld'),
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
)
437 class NRKTVSerieBaseIE(InfoExtractor
):
438 def _extract_series(self
, webpage
, display_id
, fatal
=True):
439 config
= self
._parse
_json
(
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
)
449 (lambda x
: x
['initialState']['series'], lambda x
: x
['series']),
452 def _extract_seasons(self
, seasons
):
453 if not isinstance(seasons
, list):
456 for season
in seasons
:
457 entries
.extend(self
._extract
_episodes
(season
))
460 def _extract_episodes(self
, season
):
461 if not isinstance(season
, dict):
463 return self
._extract
_entries
(season
.get('episodes'))
465 def _extract_entries(self
, entry_list
):
466 if not isinstance(entry_list
, list):
469 for episode
in entry_list
:
470 nrk_id
= episode
.get('prfId')
471 if not nrk_id
or not isinstance(nrk_id
, compat_str
):
473 entries
.append(self
.url_result(
474 'nrk:%s' % nrk_id
, ie
=NRKIE
.ie_key(), video_id
=nrk_id
))
478 class NRKTVSeasonIE(NRKTVSerieBaseIE
):
479 _VALID_URL
= r
'https?://tv\.nrk\.no/serie/[^/]+/sesong/(?P<id>\d+)'
481 'url': 'https://tv.nrk.no/serie/backstage/sesong/1',
486 'playlist_mincount': 30,
490 def suitable(cls
, url
):
491 return (False if NRKTVIE
.suitable(url
) or NRKTVEpisodeIE
.suitable(url
)
492 else super(NRKTVSeasonIE
, cls
).suitable(url
))
494 def _real_extract(self
, url
):
495 display_id
= self
._match
_id
(url
)
497 webpage
= self
._download
_webpage
(url
, display_id
)
499 series
= self
._extract
_series
(webpage
, display_id
)
502 s
for s
in series
['seasons']
503 if int(display_id
) == s
.get('seasonNumber'))
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
)
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+)'
514 'url': 'https://tv.nrk.no/serie/blank',
518 'description': 'md5:7664b4e7e77dc6810cd3bca367c25b6e',
520 'playlist_mincount': 30,
522 # new layout, seasons
523 'url': 'https://tv.nrk.no/serie/backstage',
526 'title': 'Backstage',
527 'description': 'md5:c3ec3a35736fca0f9e1207b5511143d3',
529 'playlist_mincount': 60,
531 # new layout, instalments
532 'url': 'https://tv.nrk.no/serie/groenn-glede',
534 'id': 'groenn-glede',
535 'title': 'Grønn glede',
536 'description': 'md5:7576e92ae7f65da6993cf90ee29e4608',
538 'playlist_mincount': 10,
541 'url': 'https://tv.nrksuper.no/serie/labyrint',
545 'description': 'md5:318b597330fdac5959247c9b69fdb1ec',
547 'playlist_mincount': 3,
549 'url': 'https://tv.nrk.no/serie/broedrene-dal-og-spektralsteinene',
550 'only_matching': True,
552 'url': 'https://tv.nrk.no/serie/saving-the-human-race',
553 'only_matching': True,
555 'url': 'https://tv.nrk.no/serie/postmann-pat',
556 'only_matching': True,
560 def suitable(cls, url):
562 False if any(ie.suitable(url)
563 for ie in (NRKTVIE, NRKTVEpisodeIE, NRKTVSeasonIE))
564 else super(NRKTVSeriesIE, cls).suitable(url))
566 def _real_extract(self, url):
567 series_id = self._match_id(url)
569 webpage = self._download_webpage(url, series_id)
571 # New layout (e.g. https://tv.nrk.no/serie/backstage)
572 series = self._extract_series(webpage, series_id, fatal=False)
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)
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)
583 # Old layout (e.g. https://tv.nrksuper.no/serie/labyrint)
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)
591 title = self._html_search_meta(
592 'seriestitle', webpage,
593 'title', default=None) or self._og_search_title(
594 webpage, fatal=False)
596 title = self._search_regex(
597 r'NRK (?:Super )?TV\s*[-–]\s*(.+)', title, 'title', default=title)
599 description = self._html_search_meta(
600 'series_description', webpage,
601 'description', default=None) or self._og_search_description(webpage)
603 return self.playlist_result(entries, series_id, title, description)
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>[^/?#&]+)'
611 'url': 'https://tv.nrk.no/direkte/nrk1',
612 'only_matching': True,
614 'url': 'https://radio.nrk.no/direkte/p1_oslo_akershus',
615 'only_matching': True,
619 class NRKPlaylistBaseIE(InfoExtractor):
620 def _extract_description(self, webpage):
623 def _real_extract(self, url):
624 playlist_id = self._match_id(url)
626 webpage = self._download_webpage(url, playlist_id)
629 self.url_result('nrk:%s' % video_id, NRKIE.ie_key())
630 for video_id in re.findall(self._ITEM_RE, webpage)
633 playlist_title = self. _extract_title(webpage)
634 playlist_description = self._extract_description(webpage)
636 return self.playlist_result(
637 entries, playlist_id, playlist_title, playlist_description)
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="([^
"]+)"'
644 'url
': 'http
://www
.nrk
.no
/troms
/gjenopplev
-den
-historiske
-solformorkelsen
-1.12270763',
646 'id': 'gjenopplev
-den
-historiske
-solformorkelsen
-1.12270763',
647 'title
': 'Gjenopplev den historiske solformørkelsen
',
648 'description
': 'md5
:c2df8ea3bac5654a26fc2834a542feed
',
652 'url
': 'http
://www
.nrk
.no
/kultur
/bok
/rivertonprisen
-til
-karin
-fossum
-1.12266449',
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
.',
661 def _extract_title(self, webpage):
662 return self._og_search_title(webpage, fatal=False)
664 def _extract_description(self, webpage):
665 return self._og_search_description(webpage)
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
672 'url': 'https://tv.nrk.no/program/episodes/nytt-paa-nytt/69031',
675 'title': 'Nytt på nytt, sesong: 201210',
680 def _extract_title(self, webpage):
681 return self._html_search_regex(
682 r'<h1>([^<]+)</h1>', webpage, 'title', fatal=False)
685 class NRKSkoleIE(InfoExtractor):
686 IE_DESC = 'NRK Skole'
687 _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
690 'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
691 'md5': '18c12c3d071953c3bf8d54ef6b2587b7',
695 'title': 'Genetikk og eneggede tvillinger',
696 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
700 'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
701 'only_matching': True,
704 def _real_extract(self, url):
705 video_id = self._match_id(url)
707 webpage = self._download_webpage(
708 'https://mimir.nrk.no/plugin/1.0/static?mediaId=%s' % video_id,
711 nrk_id = self._parse_json(
713 r'<script[^>]+type=["\']application
/json
["\'][^>]*>({.+?})</script>',
714 webpage, 'application json'),
715 video_id)['activeMedia']['psId']
717 return self.url_result('nrk:%s' % nrk_id)