2 from __future__ import unicode_literals
6 from .common import InfoExtractor
7 from ..compat import compat_urllib_parse_unquote
16 class NRKBaseIE(InfoExtractor):
17 _GEO_COUNTRIES = ['NO']
18 def _real_extract(self, url):
19 video_id = self._match_id(url)
21 data = self._download_json(
22 'http://%s/mediaelement/%s' % (self._API_HOST, video_id),
23 video_id, 'Downloading mediaelement JSON')
25 title = data.get('fullTitle') or data.get('mainTitle') or data['title']
26 video_id = data.get('id') or video_id
30 conviva = data.get('convivaStatistics') or {}
31 live = (data.get('mediaElementType') == 'Live' or
32 data.get('isLive') is True or conviva.get('isLive'))
35 return self._live_title(t) if live else t
37 media_assets = data.get('mediaAssets')
38 if media_assets and isinstance(media_assets, list):
39 def video_id_and_title(idx):
40 return ((video_id, title) if len(media_assets) == 1
41 else ('%s-%d' % (video_id, idx), '%s (Part %d)' % (title, idx)))
42 for num, asset in enumerate(media_assets, 1):
43 asset_url = asset.get('url')
46 formats = self._extract_akamai_formats(asset_url, video_id)
49 self._sort_formats(formats)
51 # Some f4m streams may not work with hdcore in fragments' URLs
53 extra_param = f.get('extra_param_to_segment_url')
54 if extra_param and 'hdcore' in extra_param:
55 del f['extra_param_to_segment_url']
57 entry_id, entry_title = video_id_and_title(num)
58 duration = parse_duration(asset.get('duration'))
60 for subtitle in ('webVtt', 'timedText'):
61 subtitle_url = asset.get('%sSubtitlesUrl' % subtitle)
63 subtitles.setdefault('no', []).append({
64 'url': compat_urllib_parse_unquote(subtitle_url)
67 'id': asset.get('carrierId') or entry_id,
68 'title': make_title(entry_title),
70 'subtitles': subtitles,
75 media_url = data.get('mediaUrl')
77 formats = self._extract_akamai_formats(media_url, video_id)
78 self._sort_formats(formats)
79 duration = parse_duration(data.get('duration'))
82 'title': make_title(title),
89 'ProgramRightsAreNotReady': 'Du kan dessverre ikke se eller høre programmet',
90 'ProgramRightsHasExpired': 'Programmet har gått ut',
91 'ProgramIsGeoBlocked': 'NRK har ikke rettigheter til å vise dette programmet utenfor Norge',
93 message_type = data.get('messageType', '')
94 # Can be ProgramIsGeoBlocked or ChannelIsGeoBlocked*
95 if 'IsGeoBlocked' in message_type:
96 self.raise_geo_restricted(
97 msg=MESSAGES.get('ProgramIsGeoBlocked'),
98 countries=self._GEO_COUNTRIES)
100 '%s said: %s' % (self.IE_NAME, MESSAGES.get(
101 message_type, message_type)),
104 series = conviva.get('seriesName') or data.get('seriesTitle')
105 episode = conviva.get('episodeName') or data.get('episodeNumberOrDate')
108 episode_number = None
109 if data.get('mediaElementType') == 'Episode':
110 _season_episode = data.get('scoresStatistics', {}).get('springStreamStream') or \
111 data.get('relativeOriginUrl', '')
113 r'/s(?P<season>\d{,2})e(?P<episode>\d{,2})\.',
114 r'/sesong-(?P<season>\d{,2})/episode-(?P<episode>\d{,2})',
116 season_number = int_or_none(self._search_regex(
117 EPISODENUM_RE, _season_episode, 'season number',
118 default=None, group='season'))
119 episode_number = int_or_none(self._search_regex(
120 EPISODENUM_RE, _season_episode, 'episode number',
121 default=None, group='episode'))
124 images = data.get('images')
125 if images and isinstance(images, dict):
126 web_images = images.get('webImages')
127 if isinstance(web_images, list):
129 'url': image['imageUrl'],
130 'width': int_or_none(image.get('width')),
131 'height': int_or_none(image.get('height')),
132 } for image in web_images if image.get('imageUrl')]
134 description = data.get('description')
135 category = data.get('mediaAnalytics', {}).get('category')
138 'description': description,
141 'season_number': season_number,
142 'episode_number': episode_number,
143 'categories': [category] if category else None,
144 'age_limit': parse_age_limit(data.get('legalAge')),
145 'thumbnails': thumbnails,
148 vcodec = 'none' if data.get('mediaType') == 'Audio' else None
150 # TODO: extract chapters when https://github.com/rg3/youtube-dl/pull/9409 is merged
152 for entry in entries:
153 entry.update(common_info)
154 for f in entry['formats']:
157 return self.playlist_result(entries, video_id, title, description)
160 class NRKIE(NRKBaseIE):
161 _VALID_URL = r'''(?x)
166 (?:www\.)?nrk\.no/video/PS\*|
167 v8-psapi\.nrk\.no/mediaelement/
172 _API_HOST = 'v8.psapi.nrk.no'
175 'url': 'http://www.nrk.no/video/PS*150533',
176 'md5': '2f7f6eeb2aacdd99885f355428715cfa',
180 'title': 'Dompap og andre fugler i Piip-Show',
181 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
186 'url': 'http://www.nrk.no/video/PS*154915',
191 'title': 'Slik høres internett ut når du er blind',
192 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
196 'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
197 'only_matching': True,
199 'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
200 'only_matching': True,
204 class NRKTVIE(NRKBaseIE):
205 IE_DESC = 'NRK TV and NRK Radio'
206 _EPISODE_RE = r'(?P<id>[a-zA-Z]{4}\d{8})'
207 _VALID_URL = r'''(?x)
209 (?:tv|radio)\.nrk(?:super)?\.no/
210 (?:serie/[^/]+|program)/
212 (?:/\d{2}-\d{2}-\d{4})?
213 (?:\#del=(?P<part_id>\d+))?
215 _API_HOST = 'psapi-we.nrk.no'
218 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
219 'md5': '4e9ca6629f09e588ed240fb11619922a',
221 'id': 'MUHH48000314AA',
223 'title': '20 spørsmål 23.05.2014',
224 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
226 'series': '20 spørsmål - TV',
227 'episode': '23.05.2014',
230 'url': 'https://tv.nrk.no/program/mdfp15000514',
232 'id': 'MDFP15000514CA',
234 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
235 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
237 'series': 'Kunnskapskanalen',
238 'episode': '24.05.2014',
241 'skip_download': True,
244 # single playlist video
245 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
247 'id': 'MSPO40010515-part2',
249 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
250 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
253 'skip_download': True,
255 'expected_warnings': ['Video is geo restricted'],
256 'skip': 'particular part is not supported currently',
258 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
261 'id': 'MSPO40010515AH',
263 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 1)',
264 'description': 'md5:c03aba1e917561eface5214020551b7a',
266 'series': 'Tour de Ski',
267 'episode': '06.01.2015',
270 'skip_download': True,
274 'id': 'MSPO40010515BH',
276 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015 (Part 2)',
277 'description': 'md5:c03aba1e917561eface5214020551b7a',
279 'series': 'Tour de Ski',
280 'episode': '06.01.2015',
283 'skip_download': True,
287 'id': 'MSPO40010515',
288 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
289 'description': 'md5:c03aba1e917561eface5214020551b7a',
291 'expected_warnings': ['Video is geo restricted'],
293 'url': 'https://tv.nrk.no/serie/anno/KMTE50001317/sesong-3/episode-13',
295 'id': 'KMTE50001317AA',
297 'title': 'Anno 13:30',
298 'description': 'md5:11d9613661a8dbe6f9bef54e3a4cbbfa',
303 'episode_number': 13,
306 'skip_download': True,
309 'url': 'https://tv.nrk.no/serie/nytt-paa-nytt/MUHH46000317/27-01-2017',
311 'id': 'MUHH46000317AA',
313 'title': 'Nytt på Nytt 27.01.2017',
314 'description': 'md5:5358d6388fba0ea6f0b6d11c48b9eb4b',
316 'series': 'Nytt på nytt',
317 'episode': '27.01.2017',
320 'skip_download': True,
323 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
324 'only_matching': True,
328 class NRKTVDirekteIE(NRKTVIE):
329 IE_DESC = 'NRK TV Direkte and NRK Radio Direkte'
330 _VALID_URL = r'https?://(?:tv|radio)\.nrk\.no/direkte/(?P<id>[^/?#&]+)'
333 'url': 'https://tv.nrk.no/direkte/nrk1',
334 'only_matching': True,
336 'url': 'https://radio.nrk.no/direkte/p1_oslo_akershus',
337 'only_matching': True,
341 class NRKPlaylistBaseIE(InfoExtractor):
342 def _extract_description(self, webpage):
345 def _real_extract(self, url):
346 playlist_id = self._match_id(url)
348 webpage = self._download_webpage(url, playlist_id)
351 self.url_result('nrk:%s' % video_id, NRKIE.ie_key())
352 for video_id in re.findall(self._ITEM_RE, webpage)
355 playlist_title = self. _extract_title(webpage)
356 playlist_description = self._extract_description(webpage)
358 return self.playlist_result(
359 entries, playlist_id, playlist_title, playlist_description)
362 class NRKPlaylistIE(NRKPlaylistBaseIE):
363 _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
364 _ITEM_RE = r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"'
366 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
368 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
369 'title': 'Gjenopplev den historiske solformørkelsen',
370 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
374 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
376 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
377 'title': 'Rivertonprisen til Karin Fossum',
378 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
383 def _extract_title(self, webpage):
384 return self._og_search_title(webpage, fatal=False)
386 def _extract_description(self, webpage):
387 return self._og_search_description(webpage)
390 class NRKTVEpisodesIE(NRKPlaylistBaseIE):
391 _VALID_URL = r'https?://tv\.nrk\.no/program/[Ee]pisodes/[^/]+/(?P<id>\d+)'
392 _ITEM_RE = r'data-episode=["\']%s' % NRKTVIE._EPISODE_RE
394 'url': 'https://tv.nrk.no/program/episodes/nytt-paa-nytt/69031',
397 'title': 'Nytt på nytt, sesong: 201210',
402 def _extract_title(self, webpage):
403 return self._html_search_regex(
404 r'<h1>([^<]+)</h1>', webpage, 'title', fatal=False)
407 class NRKTVSeriesIE(InfoExtractor):
408 _VALID_URL = r'https?://(?:tv|radio)\.nrk(?:super)?\.no/serie/(?P<id>[^/]+)'
409 _ITEM_RE = r'(?:data-season=["\']|id=["\']season-)(?P<id>\d+)'
411 'url': 'https://tv.nrk.no/serie/groenn-glede',
413 'id': 'groenn-glede',
414 'title': 'Grønn glede',
415 'description': 'md5:7576e92ae7f65da6993cf90ee29e4608',
417 'playlist_mincount': 9,
419 'url': 'http://tv.nrksuper.no/serie/labyrint',
423 'description': 'md5:58afd450974c89e27d5a19212eee7115',
425 'playlist_mincount': 3,
427 'url': 'https://tv.nrk.no/serie/broedrene-dal-og-spektralsteinene',
428 'only_matching': True,
430 'url': 'https://tv.nrk.no/serie/saving-the-human-race',
431 'only_matching': True,
433 'url': 'https://tv.nrk.no/serie/postmann-pat',
434 'only_matching': True,
438 def suitable(cls, url):
439 return False if NRKTVIE.suitable(url) else super(NRKTVSeriesIE, cls).suitable(url)
441 def _real_extract(self, url):
442 series_id = self._match_id(url)
444 webpage = self._download_webpage(url, series_id)
448 'https://tv.nrk.no/program/Episodes/{series}/{season}'.format(
449 series=series_id, season=season_id))
450 for season_id in re.findall(self._ITEM_RE, webpage)
453 title = self._html_search_meta(
454 'seriestitle', webpage,
455 'title', default=None) or self._og_search_title(
456 webpage, fatal=False)
458 description = self._html_search_meta(
459 'series_description', webpage,
460 'description', default=None) or self._og_search_description(webpage)
462 return self.playlist_result(entries, series_id, title, description)
465 class NRKSkoleIE(InfoExtractor):
466 IE_DESC = 'NRK Skole'
467 _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
470 'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
471 'md5': '6bc936b01f9dd8ed45bc58b252b2d9b6',
475 'title': 'Genetikk og eneggede tvillinger',
476 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
480 'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
481 'only_matching': True,
484 def _real_extract(self, url):
485 video_id = self._match_id(url)
487 webpage = self._download_webpage(
488 'https://mimir.nrk.no/plugin/1.0/static?mediaId=%s' % video_id,
491 nrk_id = self._parse_json(
493 r'<script[^>]+type=["\']application/json["\'][^>]*>({.+?})</script>',
494 webpage, 'application json'),
495 video_id)['activeMedia']['psId']
497 return self.url_result('nrk:%s' % nrk_id)