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