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