1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5 import random
6 import re
7
8 from .common import InfoExtractor
9 from ..compat import compat_str
10 from ..utils import (
11 determine_ext,
12 ExtractorError,
13 int_or_none,
14 parse_duration,
15 str_or_none,
16 try_get,
17 urljoin,
18 url_or_none,
19 )
20
21
22 class NRKBaseIE(InfoExtractor):
23 _GEO_COUNTRIES = ['NO']
24 _CDN_REPL_REGEX = r'''(?x)://
25 (?:
26 nrkod\d{1,2}-httpcache0-47115-cacheod0\.dna\.ip-only\.net/47115-cacheod0|
27 nrk-od-no\.telenorcdn\.net|
28 minicdn-od\.nrk\.no/od/nrkhd-osl-rr\.netwerk\.no/no
29 )/'''
30
31 def _extract_nrk_formats(self, asset_url, video_id):
32 if re.match(r'https?://[^/]+\.akamaihd\.net/i/', asset_url):
33 return self._extract_akamai_formats(asset_url, video_id)
34 asset_url = re.sub(r'(?:bw_(?:low|high)=\d+|no_audio_only)&?', '', asset_url)
35 formats = self._extract_m3u8_formats(
36 asset_url, video_id, 'mp4', 'm3u8_native', fatal=False)
37 if not formats and re.search(self._CDN_REPL_REGEX, asset_url):
38 formats = self._extract_m3u8_formats(
39 re.sub(self._CDN_REPL_REGEX, '://nrk-od-%02d.akamaized.net/no/' % random.randint(0, 99), asset_url),
40 video_id, 'mp4', 'm3u8_native', fatal=False)
41 return formats
42
43 def _raise_error(self, data):
44 MESSAGES = {
45 'ProgramRightsAreNotReady': 'Du kan dessverre ikke se eller høre programmet',
46 'ProgramRightsHasExpired': 'Programmet har gått ut',
47 'NoProgramRights': 'Ikke tilgjengelig',
48 'ProgramIsGeoBlocked': 'NRK har ikke rettigheter til å vise dette programmet utenfor Norge',
49 }
50 message_type = data.get('messageType', '')
51 # Can be ProgramIsGeoBlocked or ChannelIsGeoBlocked*
52 if 'IsGeoBlocked' in message_type or try_get(data, lambda x: x['usageRights']['isGeoBlocked']) is True:
53 self.raise_geo_restricted(
54 msg=MESSAGES.get('ProgramIsGeoBlocked'),
55 countries=self._GEO_COUNTRIES)
56 message = data.get('endUserMessage') or MESSAGES.get(message_type, message_type)
57 raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
58
59 def _call_api(self, path, video_id, item=None, note=None, fatal=True, query=None):
60 return self._download_json(
61 urljoin('https://psapi.nrk.no/', path),
62 video_id, note or 'Downloading %s JSON' % item,
63 fatal=fatal, query=query)
64
65
66 class NRKIE(NRKBaseIE):
67 _VALID_URL = r'''(?x)
68 (?:
69 nrk:|
70 https?://
71 (?:
72 (?:www\.)?nrk\.no/video/(?:PS\*|[^_]+_)|
73 v8[-.]psapi\.nrk\.no/mediaelement/
74 )
75 )
76 (?P<id>[^?\#&]+)
77 '''
78
79 _TESTS = [{
80 # video
81 'url': 'http://www.nrk.no/video/PS*150533',
82 'md5': 'f46be075326e23ad0e524edfcb06aeb6',
83 'info_dict': {
84 'id': '150533',
85 'ext': 'mp4',
86 'title': 'Dompap og andre fugler i Piip-Show',
87 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
88 'duration': 262,
89 }
90 }, {
91 # audio
92 'url': 'http://www.nrk.no/video/PS*154915',
93 # MD5 is unstable
94 'info_dict': {
95 'id': '154915',
96 'ext': 'mp4',
97 'title': 'Slik høres internett ut når du er blind',
98 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
99 'duration': 20,
100 }
101 }, {
102 'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
103 'only_matching': True,
104 }, {
105 'url': 'nrk:clip/7707d5a3-ebe7-434a-87d5-a3ebe7a34a70',
106 'only_matching': True,
107 }, {
108 'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
109 'only_matching': True,
110 }, {
111 'url': 'https://www.nrk.no/video/dompap-og-andre-fugler-i-piip-show_150533',
112 'only_matching': True,
113 }, {
114 'url': 'https://www.nrk.no/video/humor/kommentatorboksen-reiser-til-sjos_d1fda11f-a4ad-437a-a374-0398bc84e999',
115 'only_matching': True,
116 }, {
117 # podcast
118 'url': 'nrk:l_96f4f1b0-de54-4e6a-b4f1-b0de54fe6af8',
119 'only_matching': True,
120 }, {
121 'url': 'nrk:podcast/l_96f4f1b0-de54-4e6a-b4f1-b0de54fe6af8',
122 'only_matching': True,
123 }, {
124 # clip
125 'url': 'nrk:150533',
126 'only_matching': True,
127 }, {
128 'url': 'nrk:clip/150533',
129 'only_matching': True,
130 }, {
131 # program
132 'url': 'nrk:MDDP12000117',
133 'only_matching': True,
134 }, {
135 'url': 'nrk:program/ENRK10100318',
136 'only_matching': True,
137 }, {
138 # direkte
139 'url': 'nrk:nrk1',
140 'only_matching': True,
141 }, {
142 'url': 'nrk:channel/nrk1',
143 'only_matching': True,
144 }]
145
146 def _real_extract(self, url):
147 video_id = self._match_id(url).split('/')[-1]
148
149 path_templ = 'playback/%s/' + video_id
150
151 def call_playback_api(item, query=None):
152 return self._call_api(path_templ % item, video_id, item, query=query)
153 # known values for preferredCdn: akamai, iponly, minicdn and telenor
154 manifest = call_playback_api('manifest', {'preferredCdn': 'akamai'})
155
156 video_id = try_get(manifest, lambda x: x['id'], compat_str) or video_id
157
158 if manifest.get('playability') == 'nonPlayable':
159 self._raise_error(manifest['nonPlayable'])
160
161 playable = manifest['playable']
162
163 formats = []
164 for asset in playable['assets']:
165 if not isinstance(asset, dict):
166 continue
167 if asset.get('encrypted'):
168 continue
169 format_url = url_or_none(asset.get('url'))
170 if not format_url:
171 continue
172 asset_format = (asset.get('format') or '').lower()
173 if asset_format == 'hls' or determine_ext(format_url) == 'm3u8':
174 formats.extend(self._extract_nrk_formats(format_url, video_id))
175 elif asset_format == 'mp3':
176 formats.append({
177 'url': format_url,
178 'format_id': asset_format,
179 'vcodec': 'none',
180 })
181 self._sort_formats(formats)
182
183 data = call_playback_api('metadata')
184
185 preplay = data['preplay']
186 titles = preplay['titles']
187 title = titles['title']
188 alt_title = titles.get('subtitle')
189
190 description = preplay.get('description')
191 duration = parse_duration(playable.get('duration')) or parse_duration(data.get('duration'))
192
193 thumbnails = []
194 for image in try_get(
195 preplay, lambda x: x['poster']['images'], list) or []:
196 if not isinstance(image, dict):
197 continue
198 image_url = url_or_none(image.get('url'))
199 if not image_url:
200 continue
201 thumbnails.append({
202 'url': image_url,
203 'width': int_or_none(image.get('pixelWidth')),
204 'height': int_or_none(image.get('pixelHeight')),
205 })
206
207 subtitles = {}
208 for sub in try_get(playable, lambda x: x['subtitles'], list) or []:
209 if not isinstance(sub, dict):
210 continue
211 sub_url = url_or_none(sub.get('webVtt'))
212 if not sub_url:
213 continue
214 sub_key = str_or_none(sub.get('language')) or 'nb'
215 sub_type = str_or_none(sub.get('type'))
216 if sub_type:
217 sub_key += '-%s' % sub_type
218 subtitles.setdefault(sub_key, []).append({
219 'url': sub_url,
220 })
221
222 legal_age = try_get(
223 data, lambda x: x['legalAge']['body']['rating']['code'], compat_str)
224 # https://en.wikipedia.org/wiki/Norwegian_Media_Authority
225 age_limit = None
226 if legal_age:
227 if legal_age == 'A':
228 age_limit = 0
229 elif legal_age.isdigit():
230 age_limit = int_or_none(legal_age)
231
232 is_series = try_get(data, lambda x: x['_links']['series']['name']) == 'series'
233
234 info = {
235 'id': video_id,
236 'title': title,
237 'alt_title': alt_title,
238 'description': description,
239 'duration': duration,
240 'thumbnails': thumbnails,
241 'age_limit': age_limit,
242 'formats': formats,
243 'subtitles': subtitles,
244 }
245
246 if is_series:
247 series = season_id = season_number = episode = episode_number = None
248 programs = self._call_api(
249 'programs/%s' % video_id, video_id, 'programs', fatal=False)
250 if programs and isinstance(programs, dict):
251 series = str_or_none(programs.get('seriesTitle'))
252 season_id = str_or_none(programs.get('seasonId'))
253 season_number = int_or_none(programs.get('seasonNumber'))
254 episode = str_or_none(programs.get('episodeTitle'))
255 episode_number = int_or_none(programs.get('episodeNumber'))
256 if not series:
257 series = title
258 if alt_title:
259 title += ' - %s' % alt_title
260 if not season_number:
261 season_number = int_or_none(self._search_regex(
262 r'Sesong\s+(\d+)', description or '', 'season number',
263 default=None))
264 if not episode:
265 episode = alt_title if is_series else None
266 if not episode_number:
267 episode_number = int_or_none(self._search_regex(
268 r'^(\d+)\.', episode or '', 'episode number',
269 default=None))
270 if not episode_number:
271 episode_number = int_or_none(self._search_regex(
272 r'\((\d+)\s*:\s*\d+\)', description or '',
273 'episode number', default=None))
274 info.update({
275 'title': title,
276 'series': series,
277 'season_id': season_id,
278 'season_number': season_number,
279 'episode': episode,
280 'episode_number': episode_number,
281 })
282
283 return info
284
285
286 class NRKTVIE(InfoExtractor):
287 IE_DESC = 'NRK TV and NRK Radio'
288 _EPISODE_RE = r'(?P<id>[a-zA-Z]{4}\d{8})'
289 _VALID_URL = r'https?://(?:tv|radio)\.nrk(?:super)?\.no/(?:[^/]+/)*%s' % _EPISODE_RE
290 _TESTS = [{
291 'url': 'https://tv.nrk.no/program/MDDP12000117',
292 'md5': 'c4a5960f1b00b40d47db65c1064e0ab1',
293 'info_dict': {
294 'id': 'MDDP12000117',
295 'ext': 'mp4',
296 'title': 'Alarm Trolltunga',
297 'description': 'md5:46923a6e6510eefcce23d5ef2a58f2ce',
298 'duration': 2223.44,
299 'age_limit': 6,
300 'subtitles': {
301 'nb-nor': [{
302 'ext': 'vtt',
303 }],
304 'nb-ttv': [{
305 'ext': 'vtt',
306 }]
307 },
308 },
309 }, {
310 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
311 'md5': '8d40dab61cea8ab0114e090b029a0565',
312 'info_dict': {
313 'id': 'MUHH48000314',
314 'ext': 'mp4',
315 'title': '20 spørsmål - 23. mai 2014',
316 'alt_title': '23. mai 2014',
317 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
318 'duration': 1741,
319 'series': '20 spørsmål',
320 'episode': '23. mai 2014',
321 'age_limit': 0,
322 },
323 }, {
324 'url': 'https://tv.nrk.no/program/mdfp15000514',
325 'info_dict': {
326 'id': 'MDFP15000514',
327 'ext': 'mp4',
328 'title': 'Kunnskapskanalen - Grunnlovsjubiléet - Stor ståhei for ingenting',
329 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
330 'duration': 4605.08,
331 'series': 'Kunnskapskanalen',
332 'episode': 'Grunnlovsjubiléet - Stor ståhei for ingenting',
333 'age_limit': 0,
334 },
335 'params': {
336 'skip_download': True,
337 },
338 }, {
339 # single playlist video
340 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
341 'info_dict': {
342 'id': 'MSPO40010515',
343 'ext': 'mp4',
344 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
345 'description': 'md5:c03aba1e917561eface5214020551b7a',
346 'age_limit': 0,
347 },
348 'params': {
349 'skip_download': True,
350 },
351 'expected_warnings': ['Failed to download m3u8 information'],
352 'skip': 'particular part is not supported currently',
353 }, {
354 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
355 'info_dict': {
356 'id': 'MSPO40010515',
357 'ext': 'mp4',
358 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
359 'description': 'md5:c03aba1e917561eface5214020551b7a',
360 'age_limit': 0,
361 },
362 'expected_warnings': ['Failed to download m3u8 information'],
363 'skip': 'Ikke tilgjengelig utenfor Norge',
364 }, {
365 'url': 'https://tv.nrk.no/serie/anno/KMTE50001317/sesong-3/episode-13',
366 'info_dict': {
367 'id': 'KMTE50001317',
368 'ext': 'mp4',
369 'title': 'Anno - 13. episode',
370 'description': 'md5:11d9613661a8dbe6f9bef54e3a4cbbfa',
371 'duration': 2340,
372 'series': 'Anno',
373 'episode': '13. episode',
374 'season_number': 3,
375 'episode_number': 13,
376 'age_limit': 0,
377 },
378 'params': {
379 'skip_download': True,
380 },
381 }, {
382 'url': 'https://tv.nrk.no/serie/nytt-paa-nytt/MUHH46000317/27-01-2017',
383 'info_dict': {
384 'id': 'MUHH46000317',
385 'ext': 'mp4',
386 'title': 'Nytt på Nytt 27.01.2017',
387 'description': 'md5:5358d6388fba0ea6f0b6d11c48b9eb4b',
388 'duration': 1796,
389 'series': 'Nytt på nytt',
390 'episode': '27.01.2017',
391 'age_limit': 0,
392 },
393 'params': {
394 'skip_download': True,
395 },
396 'skip': 'ProgramRightsHasExpired',
397 }, {
398 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
399 'only_matching': True,
400 }, {
401 'url': 'https://tv.nrk.no/serie/lindmo/2018/MUHU11006318/avspiller',
402 'only_matching': True,
403 }, {
404 'url': 'https://radio.nrk.no/serie/dagsnytt/sesong/201507/NPUB21019315',
405 'only_matching': True,
406 }]
407
408 def _real_extract(self, url):
409 video_id = self._match_id(url)
410 return self.url_result(
411 'nrk:%s' % video_id, ie=NRKIE.ie_key(), video_id=video_id)
412
413
414 class NRKTVEpisodeIE(InfoExtractor):
415 _VALID_URL = r'https?://tv\.nrk\.no/serie/(?P<id>[^/]+/sesong/(?P<season_number>\d+)/episode/(?P<episode_number>\d+))'
416 _TESTS = [{
417 'url': 'https://tv.nrk.no/serie/hellums-kro/sesong/1/episode/2',
418 'info_dict': {
419 'id': 'MUHH36005220',
420 'ext': 'mp4',
421 'title': 'Hellums kro - 2. Kro, krig og kjærlighet',
422 'description': 'md5:ad92ddffc04cea8ce14b415deef81787',
423 'duration': 1563.92,
424 'series': 'Hellums kro',
425 'season_number': 1,
426 'episode_number': 2,
427 'episode': '2. Kro, krig og kjærlighet',
428 'age_limit': 6,
429 },
430 'params': {
431 'skip_download': True,
432 },
433 }, {
434 'url': 'https://tv.nrk.no/serie/backstage/sesong/1/episode/8',
435 'info_dict': {
436 'id': 'MSUI14000816',
437 'ext': 'mp4',
438 'title': 'Backstage - 8. episode',
439 'description': 'md5:de6ca5d5a2d56849e4021f2bf2850df4',
440 'duration': 1320,
441 'series': 'Backstage',
442 'season_number': 1,
443 'episode_number': 8,
444 'episode': '8. episode',
445 'age_limit': 0,
446 },
447 'params': {
448 'skip_download': True,
449 },
450 'skip': 'ProgramRightsHasExpired',
451 }]
452
453 def _real_extract(self, url):
454 display_id, season_number, episode_number = re.match(self._VALID_URL, url).groups()
455
456 webpage = self._download_webpage(url, display_id)
457
458 info = self._search_json_ld(webpage, display_id, default={})
459 nrk_id = info.get('@id') or self._html_search_meta(
460 'nrk:program-id', webpage, default=None) or self._search_regex(
461 r'data-program-id=["\'](%s)' % NRKTVIE._EPISODE_RE, webpage,
462 'nrk id')
463 assert re.match(NRKTVIE._EPISODE_RE, nrk_id)
464
465 info.update({
466 '_type': 'url',
467 'id': nrk_id,
468 'url': 'nrk:%s' % nrk_id,
469 'ie_key': NRKIE.ie_key(),
470 'season_number': int(season_number),
471 'episode_number': int(episode_number),
472 })
473 return info
474
475
476 class NRKTVSerieBaseIE(NRKBaseIE):
477 def _extract_entries(self, entry_list):
478 if not isinstance(entry_list, list):
479 return []
480 entries = []
481 for episode in entry_list:
482 nrk_id = episode.get('prfId') or episode.get('episodeId')
483 if not nrk_id or not isinstance(nrk_id, compat_str):
484 continue
485 entries.append(self.url_result(
486 'nrk:%s' % nrk_id, ie=NRKIE.ie_key(), video_id=nrk_id))
487 return entries
488
489 _ASSETS_KEYS = ('episodes', 'instalments',)
490
491 def _extract_assets_key(self, embedded):
492 for asset_key in self._ASSETS_KEYS:
493 if embedded.get(asset_key):
494 return asset_key
495
496 @staticmethod
497 def _catalog_name(serie_kind):
498 return 'podcast' if serie_kind in ('podcast', 'podkast') else 'series'
499
500 def _entries(self, data, display_id):
501 for page_num in itertools.count(1):
502 embedded = data.get('_embedded') or data
503 if not isinstance(embedded, dict):
504 break
505 assets_key = self._extract_assets_key(embedded)
506 if not assets_key:
507 break
508 # Extract entries
509 entries = try_get(
510 embedded,
511 (lambda x: x[assets_key]['_embedded'][assets_key],
512 lambda x: x[assets_key]),
513 list)
514 for e in self._extract_entries(entries):
515 yield e
516 # Find next URL
517 next_url_path = try_get(
518 data,
519 (lambda x: x['_links']['next']['href'],
520 lambda x: x['_embedded'][assets_key]['_links']['next']['href']),
521 compat_str)
522 if not next_url_path:
523 break
524 data = self._call_api(
525 next_url_path, display_id,
526 note='Downloading %s JSON page %d' % (assets_key, page_num),
527 fatal=False)
528 if not data:
529 break
530
531
532 class NRKTVSeasonIE(NRKTVSerieBaseIE):
533 _VALID_URL = r'''(?x)
534 https?://
535 (?P<domain>tv|radio)\.nrk\.no/
536 (?P<serie_kind>serie|pod[ck]ast)/
537 (?P<serie>[^/]+)/
538 (?:
539 (?:sesong/)?(?P<id>\d+)|
540 sesong/(?P<id_2>[^/?#&]+)
541 )
542 '''
543 _TESTS = [{
544 'url': 'https://tv.nrk.no/serie/backstage/sesong/1',
545 'info_dict': {
546 'id': 'backstage/1',
547 'title': 'Sesong 1',
548 },
549 'playlist_mincount': 30,
550 }, {
551 # no /sesong/ in path
552 'url': 'https://tv.nrk.no/serie/lindmo/2016',
553 'info_dict': {
554 'id': 'lindmo/2016',
555 'title': '2016',
556 },
557 'playlist_mincount': 29,
558 }, {
559 # weird nested _embedded in catalog JSON response
560 'url': 'https://radio.nrk.no/serie/dickie-dick-dickens/sesong/1',
561 'info_dict': {
562 'id': 'dickie-dick-dickens/1',
563 'title': 'Sesong 1',
564 },
565 'playlist_mincount': 11,
566 }, {
567 # 841 entries, multi page
568 'url': 'https://radio.nrk.no/serie/dagsnytt/sesong/201509',
569 'info_dict': {
570 'id': 'dagsnytt/201509',
571 'title': 'September 2015',
572 },
573 'playlist_mincount': 841,
574 }, {
575 # 180 entries, single page
576 'url': 'https://tv.nrk.no/serie/spangas/sesong/1',
577 'only_matching': True,
578 }, {
579 'url': 'https://radio.nrk.no/podkast/hele_historien/sesong/diagnose-kverulant',
580 'info_dict': {
581 'id': 'hele_historien/diagnose-kverulant',
582 'title': 'Diagnose kverulant',
583 },
584 'playlist_mincount': 3,
585 }, {
586 'url': 'https://radio.nrk.no/podkast/loerdagsraadet/sesong/202101',
587 'only_matching': True,
588 }]
589
590 @classmethod
591 def suitable(cls, url):
592 return (False if NRKTVIE.suitable(url) or NRKTVEpisodeIE.suitable(url) or NRKRadioPodkastIE.suitable(url)
593 else super(NRKTVSeasonIE, cls).suitable(url))
594
595 def _real_extract(self, url):
596 mobj = re.match(self._VALID_URL, url)
597 domain = mobj.group('domain')
598 serie_kind = mobj.group('serie_kind')
599 serie = mobj.group('serie')
600 season_id = mobj.group('id') or mobj.group('id_2')
601 display_id = '%s/%s' % (serie, season_id)
602
603 data = self._call_api(
604 '%s/catalog/%s/%s/seasons/%s'
605 % (domain, self._catalog_name(serie_kind), serie, season_id),
606 display_id, 'season', query={'pageSize': 50})
607
608 title = try_get(data, lambda x: x['titles']['title'], compat_str) or display_id
609 return self.playlist_result(
610 self._entries(data, display_id),
611 display_id, title)
612
613
614 class NRKTVSeriesIE(NRKTVSerieBaseIE):
615 _VALID_URL = r'https?://(?P<domain>(?:tv|radio)\.nrk|(?:tv\.)?nrksuper)\.no/(?P<serie_kind>serie|pod[ck]ast)/(?P<id>[^/]+)'
616 _TESTS = [{
617 # new layout, instalments
618 'url': 'https://tv.nrk.no/serie/groenn-glede',
619 'info_dict': {
620 'id': 'groenn-glede',
621 'title': 'Grønn glede',
622 'description': 'md5:7576e92ae7f65da6993cf90ee29e4608',
623 },
624 'playlist_mincount': 90,
625 }, {
626 # new layout, instalments, more entries
627 'url': 'https://tv.nrk.no/serie/lindmo',
628 'only_matching': True,
629 }, {
630 'url': 'https://tv.nrk.no/serie/blank',
631 'info_dict': {
632 'id': 'blank',
633 'title': 'Blank',
634 'description': 'md5:7664b4e7e77dc6810cd3bca367c25b6e',
635 },
636 'playlist_mincount': 30,
637 }, {
638 # new layout, seasons
639 'url': 'https://tv.nrk.no/serie/backstage',
640 'info_dict': {
641 'id': 'backstage',
642 'title': 'Backstage',
643 'description': 'md5:63692ceb96813d9a207e9910483d948b',
644 },
645 'playlist_mincount': 60,
646 }, {
647 # old layout
648 'url': 'https://tv.nrksuper.no/serie/labyrint',
649 'info_dict': {
650 'id': 'labyrint',
651 'title': 'Labyrint',
652 'description': 'I Daidalos sin undersjøiske Labyrint venter spennende oppgaver, skumle robotskapninger og slim.',
653 },
654 'playlist_mincount': 3,
655 }, {
656 'url': 'https://tv.nrk.no/serie/broedrene-dal-og-spektralsteinene',
657 'only_matching': True,
658 }, {
659 'url': 'https://tv.nrk.no/serie/saving-the-human-race',
660 'only_matching': True,
661 }, {
662 'url': 'https://tv.nrk.no/serie/postmann-pat',
663 'only_matching': True,
664 }, {
665 'url': 'https://radio.nrk.no/serie/dickie-dick-dickens',
666 'info_dict': {
667 'id': 'dickie-dick-dickens',
668 'title': 'Dickie Dick Dickens',
669 'description': 'md5:19e67411ffe57f7dce08a943d7a0b91f',
670 },
671 'playlist_mincount': 8,
672 }, {
673 'url': 'https://nrksuper.no/serie/labyrint',
674 'only_matching': True,
675 }, {
676 'url': 'https://radio.nrk.no/podkast/ulrikkes_univers',
677 'info_dict': {
678 'id': 'ulrikkes_univers',
679 },
680 'playlist_mincount': 10,
681 }, {
682 'url': 'https://radio.nrk.no/podkast/ulrikkes_univers/nrkno-poddkast-26588-134079-05042018030000',
683 'only_matching': True,
684 }]
685
686 @classmethod
687 def suitable(cls, url):
688 return (
689 False if any(ie.suitable(url)
690 for ie in (NRKTVIE, NRKTVEpisodeIE, NRKRadioPodkastIE, NRKTVSeasonIE))
691 else super(NRKTVSeriesIE, cls).suitable(url))
692
693 def _real_extract(self, url):
694 site, serie_kind, series_id = re.match(self._VALID_URL, url).groups()
695 is_radio = site == 'radio.nrk'
696 domain = 'radio' if is_radio else 'tv'
697
698 size_prefix = 'p' if is_radio else 'embeddedInstalmentsP'
699 series = self._call_api(
700 '%s/catalog/%s/%s'
701 % (domain, self._catalog_name(serie_kind), series_id),
702 series_id, 'serie', query={size_prefix + 'ageSize': 50})
703 titles = try_get(series, [
704 lambda x: x['titles'],
705 lambda x: x[x['type']]['titles'],
706 lambda x: x[x['seriesType']]['titles'],
707 ]) or {}
708
709 entries = []
710 entries.extend(self._entries(series, series_id))
711 embedded = series.get('_embedded') or {}
712 linked_seasons = try_get(series, lambda x: x['_links']['seasons']) or []
713 embedded_seasons = embedded.get('seasons') or []
714 if len(linked_seasons) > len(embedded_seasons):
715 for season in linked_seasons:
716 season_url = urljoin(url, season.get('href'))
717 if not season_url:
718 season_name = season.get('name')
719 if season_name and isinstance(season_name, compat_str):
720 season_url = 'https://%s.nrk.no/serie/%s/sesong/%s' % (domain, series_id, season_name)
721 if season_url:
722 entries.append(self.url_result(
723 season_url, ie=NRKTVSeasonIE.ie_key(),
724 video_title=season.get('title')))
725 else:
726 for season in embedded_seasons:
727 entries.extend(self._entries(season, series_id))
728 entries.extend(self._entries(
729 embedded.get('extraMaterial') or {}, series_id))
730
731 return self.playlist_result(
732 entries, series_id, titles.get('title'), titles.get('subtitle'))
733
734
735 class NRKTVDirekteIE(NRKTVIE):
736 IE_DESC = 'NRK TV Direkte and NRK Radio Direkte'
737 _VALID_URL = r'https?://(?:tv|radio)\.nrk\.no/direkte/(?P<id>[^/?#&]+)'
738
739 _TESTS = [{
740 'url': 'https://tv.nrk.no/direkte/nrk1',
741 'only_matching': True,
742 }, {
743 'url': 'https://radio.nrk.no/direkte/p1_oslo_akershus',
744 'only_matching': True,
745 }]
746
747
748 class NRKRadioPodkastIE(InfoExtractor):
749 _VALID_URL = r'https?://radio\.nrk\.no/pod[ck]ast/(?:[^/]+/)+(?P<id>l_[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
750
751 _TESTS = [{
752 'url': 'https://radio.nrk.no/podkast/ulrikkes_univers/l_96f4f1b0-de54-4e6a-b4f1-b0de54fe6af8',
753 'md5': '8d40dab61cea8ab0114e090b029a0565',
754 'info_dict': {
755 'id': 'MUHH48000314AA',
756 'ext': 'mp4',
757 'title': '20 spørsmål 23.05.2014',
758 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
759 'duration': 1741,
760 'series': '20 spørsmål',
761 'episode': '23.05.2014',
762 },
763 }, {
764 'url': 'https://radio.nrk.no/podcast/ulrikkes_univers/l_96f4f1b0-de54-4e6a-b4f1-b0de54fe6af8',
765 'only_matching': True,
766 }, {
767 'url': 'https://radio.nrk.no/podkast/ulrikkes_univers/sesong/1/l_96f4f1b0-de54-4e6a-b4f1-b0de54fe6af8',
768 'only_matching': True,
769 }, {
770 'url': 'https://radio.nrk.no/podkast/hele_historien/sesong/bortfoert-i-bergen/l_774d1a2c-7aa7-4965-8d1a-2c7aa7d9652c',
771 'only_matching': True,
772 }]
773
774 def _real_extract(self, url):
775 video_id = self._match_id(url)
776 return self.url_result(
777 'nrk:%s' % video_id, ie=NRKIE.ie_key(), video_id=video_id)
778
779
780 class NRKPlaylistBaseIE(InfoExtractor):
781 def _extract_description(self, webpage):
782 pass
783
784 def _real_extract(self, url):
785 playlist_id = self._match_id(url)
786
787 webpage = self._download_webpage(url, playlist_id)
788
789 entries = [
790 self.url_result('nrk:%s' % video_id, NRKIE.ie_key())
791 for video_id in re.findall(self._ITEM_RE, webpage)
792 ]
793
794 playlist_title = self. _extract_title(webpage)
795 playlist_description = self._extract_description(webpage)
796
797 return self.playlist_result(
798 entries, playlist_id, playlist_title, playlist_description)
799
800
801 class NRKPlaylistIE(NRKPlaylistBaseIE):
802 _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
803 _ITEM_RE = r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"'
804 _TESTS = [{
805 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
806 'info_dict': {
807 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
808 'title': 'Gjenopplev den historiske solformørkelsen',
809 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
810 },
811 'playlist_count': 2,
812 }, {
813 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
814 'info_dict': {
815 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
816 'title': 'Rivertonprisen til Karin Fossum',
817 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
818 },
819 'playlist_count': 2,
820 }]
821
822 def _extract_title(self, webpage):
823 return self._og_search_title(webpage, fatal=False)
824
825 def _extract_description(self, webpage):
826 return self._og_search_description(webpage)
827
828
829 class NRKTVEpisodesIE(NRKPlaylistBaseIE):
830 _VALID_URL = r'https?://tv\.nrk\.no/program/[Ee]pisodes/[^/]+/(?P<id>\d+)'
831 _ITEM_RE = r'data-episode=["\']%s' % NRKTVIE._EPISODE_RE
832 _TESTS = [{
833 'url': 'https://tv.nrk.no/program/episodes/nytt-paa-nytt/69031',
834 'info_dict': {
835 'id': '69031',
836 'title': 'Nytt på nytt, sesong: 201210',
837 },
838 'playlist_count': 4,
839 }]
840
841 def _extract_title(self, webpage):
842 return self._html_search_regex(
843 r'<h1>([^<]+)</h1>', webpage, 'title', fatal=False)
844
845
846 class NRKSkoleIE(InfoExtractor):
847 IE_DESC = 'NRK Skole'
848 _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
849
850 _TESTS = [{
851 'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
852 'md5': '18c12c3d071953c3bf8d54ef6b2587b7',
853 'info_dict': {
854 'id': '6021',
855 'ext': 'mp4',
856 'title': 'Genetikk og eneggede tvillinger',
857 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
858 'duration': 399,
859 },
860 }, {
861 'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
862 'only_matching': True,
863 }]
864
865 def _real_extract(self, url):
866 video_id = self._match_id(url)
867
868 nrk_id = self._download_json(
869 'https://nrkno-skole-prod.kube.nrk.no/skole/api/media/%s' % video_id,
870 video_id)['psId']
871
872 return self.url_result('nrk:%s' % nrk_id)
|