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