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