2 from __future__ import unicode_literals
6 from .common import InfoExtractor
7 from ..compat import compat_urllib_parse_unquote
16 class NRKBaseIE(InfoExtractor):
17 def _real_extract(self, url):
18 video_id = self._match_id(url)
20 data = self._download_json(
21 'http://%s/mediaelement/%s' % (self._API_HOST, video_id),
22 video_id, 'Downloading mediaelement JSON')
24 title = data.get('fullTitle') or data.get('mainTitle') or data['title']
25 video_id = data.get('id') or video_id
29 conviva = data.get('convivaStatistics') or {}
30 live = (data.get('mediaElementType') == 'Live' or
31 data.get('isLive') is True or conviva.get('isLive'))
34 return self._live_title(t) if live else t
36 media_assets = data.get('mediaAssets')
37 if media_assets and isinstance(media_assets, list):
38 def video_id_and_title(idx):
39 return ((video_id, title) if len(media_assets) == 1
40 else ('%s-%d' % (video_id, idx), '%s (Part %d)' % (title, idx)))
41 for num, asset in enumerate(media_assets, 1):
42 asset_url = asset.get('url')
45 formats = self._extract_akamai_formats(asset_url, video_id)
48 self._sort_formats(formats)
50 # Some f4m streams may not work with hdcore in fragments' URLs
52 extra_param = f.get('extra_param_to_segment_url')
53 if extra_param and 'hdcore' in extra_param:
54 del f['extra_param_to_segment_url']
56 entry_id, entry_title = video_id_and_title(num)
57 duration = parse_duration(asset.get('duration'))
59 for subtitle in ('webVtt', 'timedText'):
60 subtitle_url = asset.get('%sSubtitlesUrl' % subtitle)
62 subtitles.setdefault('no', []).append({
63 'url': compat_urllib_parse_unquote(subtitle_url)
66 'id': asset.get('carrierId') or entry_id,
67 'title': make_title(entry_title),
69 'subtitles': subtitles,
74 media_url = data.get('mediaUrl')
76 formats = self._extract_akamai_formats(media_url, video_id)
77 self._sort_formats(formats)
78 duration = parse_duration(data.get('duration'))
81 'title': make_title(title),
88 'ProgramRightsAreNotReady': 'Du kan dessverre ikke se eller høre programmet',
89 'ProgramRightsHasExpired': 'Programmet har gått ut',
90 'ProgramIsGeoBlocked': 'NRK har ikke rettigheter til å vise dette programmet utenfor Norge',
92 message_type = data.get('messageType', '')
93 # Can be ProgramIsGeoBlocked or ChannelIsGeoBlocked*
94 if 'IsGeoBlocked' in message_type:
95 self.raise_geo_restricted(
96 msg=MESSAGES.get('ProgramIsGeoBlocked'), countries=['NO'])
98 '%s said: %s' % (self.IE_NAME, MESSAGES.get(
99 message_type, message_type)),
102 series = conviva.get('seriesName') or data.get('seriesTitle')
103 episode = conviva.get('episodeName') or data.get('episodeNumberOrDate')
106 episode_number = None
107 if data.get('mediaElementType') == 'Episode':
108 _season_episode = data.get('scoresStatistics', {}).get('springStreamStream') or \
109 data.get('relativeOriginUrl', '')
111 r'/s(?P<season>\d{,2})e(?P<episode>\d{,2})\.',
112 r'/sesong-(?P<season>\d{,2})/episode-(?P<episode>\d{,2})',
114 season_number = int_or_none(self._search_regex(
115 EPISODENUM_RE, _season_episode, 'season number',
116 default=None, group='season'))
117 episode_number = int_or_none(self._search_regex(
118 EPISODENUM_RE, _season_episode, 'episode number',
119 default=None, group='episode'))
122 images = data.get('images')
123 if images and isinstance(images, dict):
124 web_images = images.get('webImages')
125 if isinstance(web_images, list):
127 'url': image['imageUrl'],
128 'width': int_or_none(image.get('width')),
129 'height': int_or_none(image.get('height')),
130 } for image in web_images if image.get('imageUrl')]
132 description = data.get('description')
133 category = data.get('mediaAnalytics', {}).get('category')
136 'description': description,
139 'season_number': season_number,
140 'episode_number': episode_number,
141 'categories': [category] if category else None,
142 'age_limit': parse_age_limit(data.get('legalAge')),
143 'thumbnails': thumbnails,
146 vcodec = 'none' if data.get('mediaType') == 'Audio' else None
148 # TODO: extract chapters when https://github.com/rg3/youtube-dl/pull/9409 is merged
150 for entry in entries:
151 entry.update(common_info)
152 for f in entry['formats']:
155 return self.playlist_result(entries, video_id, title, description)
158 class NRKIE(NRKBaseIE):
159 _VALID_URL = r'''(?x)
164 (?:www\.)?nrk\.no/video/PS\*|
165 v8-psapi\.nrk\.no/mediaelement/
170 _API_HOST = 'v8.psapi.nrk.no'
173 'url': 'http://www.nrk.no/video/PS*150533',
174 'md5': '2f7f6eeb2aacdd99885f355428715cfa',
178 'title': 'Dompap og andre fugler i Piip-Show',
179 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
184 'url': 'http://www.nrk.no/video/PS*154915',
189 'title': 'Slik høres internett ut når du er blind',
190 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
194 'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
195 'only_matching': True,
197 'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
198 'only_matching': True,
202 class NRKTVIE(NRKBaseIE):
203 IE_DESC = 'NRK TV and NRK Radio'
204 _EPISODE_RE = r'(?P<id>[a-zA-Z]{4}\d{8})'
205 _VALID_URL = r'''(?x)
207 (?:tv|radio)\.nrk(?:super)?\.no/
208 (?:serie/[^/]+|program)/
210 (?:/\d{2}-\d{2}-\d{4})?
211 (?:\#del=(?P<part_id>\d+))?
213 _API_HOST = 'psapi-we.nrk.no'
216 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
217 'md5': '4e9ca6629f09e588ed240fb11619922a',
219 'id': 'MUHH48000314AA',
221 'title': '20 spørsmål 23.05.2014',
222 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
224 'series': '20 spørsmål - TV',
225 'episode': '23.05.2014',
228 'url': 'https://tv.nrk.no/program/mdfp15000514',
230 'id': 'MDFP15000514CA',
232 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
233 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
235 'series': 'Kunnskapskanalen',
236 'episode': '24.05.2014',
239 'skip_download': True,
242 # single playlist video
243 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
245 'id': 'MSPO40010515-part2',
247 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
248 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
251 'skip_download': True,
253 'expected_warnings': ['Video is geo restricted'],
254 'skip': 'particular part is not supported currently',
256 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
259 'id': 'MSPO40010515AH',
261 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 1)',
262 'description': 'md5:c03aba1e917561eface5214020551b7a',
264 'series': 'Tour de Ski',
265 'episode': '06.01.2015',
268 'skip_download': True,
272 'id': 'MSPO40010515BH',
274 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 2)',
275 'description': 'md5:c03aba1e917561eface5214020551b7a',
277 'series': 'Tour de Ski',
278 'episode': '06.01.2015',
281 'skip_download': True,
285 'id': 'MSPO40010515',
286 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
287 'description': 'md5:c03aba1e917561eface5214020551b7a',
289 'expected_warnings': ['Video is geo restricted'],
291 'url': 'https://tv.nrk.no/serie/anno/KMTE50001317/sesong-3/episode-13',
293 'id': 'KMTE50001317AA',
295 'title': 'Anno 13:30',
296 'description': 'md5:11d9613661a8dbe6f9bef54e3a4cbbfa',
301 'episode_number': 13,
304 'skip_download': True,
307 'url': 'https://tv.nrk.no/serie/nytt-paa-nytt/MUHH46000317/27-01-2017',
309 'id': 'MUHH46000317AA',
311 'title': 'Nytt på Nytt 27.01.2017',
312 'description': 'md5:5358d6388fba0ea6f0b6d11c48b9eb4b',
314 'series': 'Nytt på nytt',
315 'episode': '27.01.2017',
318 'skip_download': True,
321 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
322 'only_matching': True,
326 class NRKTVDirekteIE(NRKTVIE):
327 IE_DESC = 'NRK TV Direkte and NRK Radio Direkte'
328 _VALID_URL = r'https?://(?:tv|radio)\.nrk\.no/direkte/(?P<id>[^/?#&]+)'
331 'url': 'https://tv.nrk.no/direkte/nrk1',
332 'only_matching': True,
334 'url': 'https://radio.nrk.no/direkte/p1_oslo_akershus',
335 'only_matching': True,
339 class NRKPlaylistBaseIE(InfoExtractor):
340 def _extract_description(self, webpage):
343 def _real_extract(self, url):
344 playlist_id = self._match_id(url)
346 webpage = self._download_webpage(url, playlist_id)
349 self.url_result('nrk:%s' % video_id, NRKIE.ie_key())
350 for video_id in re.findall(self._ITEM_RE, webpage)
353 playlist_title = self. _extract_title(webpage)
354 playlist_description = self._extract_description(webpage)
356 return self.playlist_result(
357 entries, playlist_id, playlist_title, playlist_description)
360 class NRKPlaylistIE(NRKPlaylistBaseIE):
361 _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
362 _ITEM_RE = r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"'
364 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
366 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
367 'title': 'Gjenopplev den historiske solformørkelsen',
368 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
372 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
374 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
375 'title': 'Rivertonprisen til Karin Fossum',
376 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
381 def _extract_title(self, webpage):
382 return self._og_search_title(webpage, fatal=False)
384 def _extract_description(self, webpage):
385 return self._og_search_description(webpage)
388 class NRKTVEpisodesIE(NRKPlaylistBaseIE):
389 _VALID_URL = r'https?://tv\.nrk\.no/program/[Ee]pisodes/[^/]+/(?P<id>\d+)'
390 _ITEM_RE = r'data-episode=["\']%s' % NRKTVIE._EPISODE_RE
392 'url': 'https://tv.nrk.no/program/episodes/nytt-paa-nytt/69031',
395 'title': 'Nytt på nytt, sesong: 201210',
400 def _extract_title(self, webpage):
401 return self._html_search_regex(
402 r'<h1>([^<]+)</h1>', webpage, 'title', fatal=False)
405 class NRKTVSeriesIE(InfoExtractor):
406 _VALID_URL = r'https?://(?:tv|radio)\.nrk(?:super)?\.no/serie/(?P<id>[^/]+)'
407 _ITEM_RE = r'(?:data-season=["\']|id=["\']season-)(?P<id>\d+)'
409 'url': 'https://tv.nrk.no/serie/groenn-glede',
411 'id': 'groenn-glede',
412 'title': 'Grønn glede',
413 'description': 'md5:7576e92ae7f65da6993cf90ee29e4608',
415 'playlist_mincount': 9,
417 'url': 'http://tv.nrksuper.no/serie/labyrint',
421 'description': 'md5:58afd450974c89e27d5a19212eee7115',
423 'playlist_mincount': 3,
425 'url': 'https://tv.nrk.no/serie/broedrene-dal-og-spektralsteinene',
426 'only_matching': True,
428 'url': 'https://tv.nrk.no/serie/saving-the-human-race',
429 'only_matching': True,
431 'url': 'https://tv.nrk.no/serie/postmann-pat',
432 'only_matching': True,
436 def suitable(cls, url):
437 return False if NRKTVIE.suitable(url) else super(NRKTVSeriesIE, cls).suitable(url)
439 def _real_extract(self, url):
440 series_id = self._match_id(url)
442 webpage = self._download_webpage(url, series_id)
446 'https://tv.nrk.no/program/Episodes/{series}/{season}'.format(
447 series=series_id, season=season_id))
448 for season_id in re.findall(self._ITEM_RE, webpage)
451 title = self._html_search_meta(
452 'seriestitle', webpage,
453 'title', default=None) or self._og_search_title(
454 webpage, fatal=False)
456 description = self._html_search_meta(
457 'series_description', webpage,
458 'description', default=None) or self._og_search_description(webpage)
460 return self.playlist_result(entries, series_id, title, description)
463 class NRKSkoleIE(InfoExtractor):
464 IE_DESC = 'NRK Skole'
465 _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
468 'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
469 'md5': '6bc936b01f9dd8ed45bc58b252b2d9b6',
473 'title': 'Genetikk og eneggede tvillinger',
474 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
478 'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
479 'only_matching': True,
482 def _real_extract(self, url):
483 video_id = self._match_id(url)
485 webpage = self._download_webpage(
486 'https://mimir.nrk.no/plugin/1.0/static?mediaId=%s' % video_id,
489 nrk_id = self._parse_json(
491 r'<script[^>]+type=["\']application/json["\'][^>]*>({.+?})</script>',
492 webpage, 'application json'),
493 video_id)['activeMedia']['psId']
495 return self.url_result('nrk:%s' % nrk_id)