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 media_assets = data.get('mediaAssets')
30 if media_assets and isinstance(media_assets, list):
31 def video_id_and_title(idx):
32 return ((video_id, title) if len(media_assets) == 1
33 else ('%s-%d' % (video_id, idx), '%s (Part %d)' % (title, idx)))
34 for num, asset in enumerate(media_assets, 1):
35 asset_url = asset.get('url')
38 formats = self._extract_akamai_formats(asset_url, video_id)
41 self._sort_formats(formats)
42 entry_id, entry_title = video_id_and_title(num)
43 duration = parse_duration(asset.get('duration'))
45 for subtitle in ('webVtt', 'timedText'):
46 subtitle_url = asset.get('%sSubtitlesUrl' % subtitle)
48 subtitles.setdefault('no', []).append({
49 'url': compat_urllib_parse_unquote(subtitle_url)
52 'id': asset.get('carrierId') or entry_id,
55 'subtitles': subtitles,
60 media_url = data.get('mediaUrl')
62 formats = self._extract_akamai_formats(media_url, video_id)
63 self._sort_formats(formats)
64 duration = parse_duration(data.get('duration'))
73 if data.get('usageRights', {}).get('isGeoBlocked'):
75 'NRK har ikke rettigheter til å vise dette programmet utenfor Norge',
78 conviva = data.get('convivaStatistics') or {}
79 series = conviva.get('seriesName') or data.get('seriesTitle')
80 episode = conviva.get('episodeName') or data.get('episodeNumberOrDate')
83 images = data.get('images')
84 if images and isinstance(images, dict):
85 web_images = images.get('webImages')
86 if isinstance(web_images, list):
88 'url': image['imageUrl'],
89 'width': int_or_none(image.get('width')),
90 'height': int_or_none(image.get('height')),
91 } for image in web_images if image.get('imageUrl')]
93 description = data.get('description')
96 'description': description,
99 'age_limit': parse_age_limit(data.get('legalAge')),
100 'thumbnails': thumbnails,
103 vcodec = 'none' if data.get('mediaType') == 'Audio' else None
105 # TODO: extract chapters when https://github.com/rg3/youtube-dl/pull/9409 is merged
107 for entry in entries:
108 entry.update(common_info)
109 for f in entry['formats']:
112 return self.playlist_result(entries, video_id, title, description)
115 class NRKIE(NRKBaseIE):
116 _VALID_URL = r'''(?x)
121 (?:www\.)?nrk\.no/video/PS\*|
122 v8-psapi\.nrk\.no/mediaelement/
127 _API_HOST = 'v8.psapi.nrk.no'
130 'url': 'http://www.nrk.no/video/PS*150533',
131 'md5': '2f7f6eeb2aacdd99885f355428715cfa',
135 'title': 'Dompap og andre fugler i Piip-Show',
136 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
141 'url': 'http://www.nrk.no/video/PS*154915',
146 'title': 'Slik høres internett ut når du er blind',
147 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
151 'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
152 'only_matching': True,
154 'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
155 'only_matching': True,
159 class NRKTVIE(NRKBaseIE):
160 IE_DESC = 'NRK TV and NRK Radio'
161 _VALID_URL = r'https?://(?:tv|radio)\.nrk(?:super)?\.no/(?:serie/[^/]+|program)/(?P<id>[a-zA-Z]{4}\d{8})(?:/\d{2}-\d{2}-\d{4})?(?:#del=(?P<part_id>\d+))?'
162 _API_HOST = 'psapi-we.nrk.no'
165 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
166 'md5': '4e9ca6629f09e588ed240fb11619922a',
168 'id': 'MUHH48000314AA',
170 'title': '20 spørsmål 23.05.2014',
171 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
175 'url': 'https://tv.nrk.no/program/mdfp15000514',
176 'md5': '43d0be26663d380603a9cf0c24366531',
178 'id': 'MDFP15000514CA',
180 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
181 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
185 # single playlist video
186 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
187 'md5': 'adbd1dbd813edaf532b0a253780719c2',
189 'id': 'MSPO40010515-part2',
191 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
192 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
194 'skip': 'Only works from Norway',
196 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
198 'md5': '9480285eff92d64f06e02a5367970a7a',
200 'id': 'MSPO40010515-part1',
202 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
203 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
206 'md5': 'adbd1dbd813edaf532b0a253780719c2',
208 'id': 'MSPO40010515-part2',
210 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
211 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
215 'id': 'MSPO40010515',
216 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
217 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
220 'skip': 'Only works from Norway',
222 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
223 'only_matching': True,
227 class NRKPlaylistIE(InfoExtractor):
228 _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
231 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
233 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
234 'title': 'Gjenopplev den historiske solformørkelsen',
235 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
239 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
241 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
242 'title': 'Rivertonprisen til Karin Fossum',
243 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
248 def _real_extract(self, url):
249 playlist_id = self._match_id(url)
251 webpage = self._download_webpage(url, playlist_id)
254 self.url_result('nrk:%s' % video_id, 'NRK')
255 for video_id in re.findall(
256 r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"',
260 playlist_title = self._og_search_title(webpage)
261 playlist_description = self._og_search_description(webpage)
263 return self.playlist_result(
264 entries, playlist_id, playlist_title, playlist_description)
267 class NRKSkoleIE(InfoExtractor):
268 IE_DESC = 'NRK Skole'
269 _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
272 'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
273 'md5': '6bc936b01f9dd8ed45bc58b252b2d9b6',
277 'title': 'Genetikk og eneggede tvillinger',
278 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
282 'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
283 'only_matching': True,
286 def _real_extract(self, url):
287 video_id = self._match_id(url)
289 webpage = self._download_webpage(
290 'https://mimir.nrk.no/plugin/1.0/static?mediaId=%s' % video_id,
293 nrk_id = self._parse_json(
295 r'<script[^>]+type=["\']application/json["\'][^>]*>({.+?})</script>',
296 webpage, 'application json'),
297 video_id)['activeMedia']['psId']
299 return self.url_result('nrk:%s' % nrk_id)