]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/nrk.py
[nytimes] Fix typo
[yt-dlp.git] / youtube_dl / extractor / nrk.py
CommitLineData
dcdb292f 1# coding: utf-8
d2176c80
S
2from __future__ import unicode_literals
3
4import re
5
6from .common import InfoExtractor
d8d540cf 7from ..compat import compat_urllib_parse_unquote
dfb2e1a3
S
8from ..utils import (
9 ExtractorError,
d8d540cf
S
10 int_or_none,
11 parse_age_limit,
76bfaf6d 12 parse_duration,
dfb2e1a3 13)
d2176c80
S
14
15
d8d540cf 16class NRKBaseIE(InfoExtractor):
d2176c80 17 def _real_extract(self, url):
4e6a2286 18 video_id = self._match_id(url)
d2176c80
S
19
20 data = self._download_json(
d8d540cf
S
21 'http://%s/mediaelement/%s' % (self._API_HOST, video_id),
22 video_id, 'Downloading mediaelement JSON')
23
24 title = data.get('fullTitle') or data.get('mainTitle') or data['title']
25 video_id = data.get('id') or video_id
26
27 entries = []
28
29 media_assets = data.get('mediaAssets')
30 if media_assets and isinstance(media_assets, list):
31 def video_id_and_title(idx):
32 return ((video_id, title) if len(media_assets) == 1
33 else ('%s-%d' % (video_id, idx), '%s (Part %d)' % (title, idx)))
34 for num, asset in enumerate(media_assets, 1):
35 asset_url = asset.get('url')
36 if not asset_url:
37 continue
ad316425 38 formats = self._extract_akamai_formats(asset_url, video_id)
d8d540cf
S
39 if not formats:
40 continue
41 self._sort_formats(formats)
42 entry_id, entry_title = video_id_and_title(num)
43 duration = parse_duration(asset.get('duration'))
44 subtitles = {}
45 for subtitle in ('webVtt', 'timedText'):
46 subtitle_url = asset.get('%sSubtitlesUrl' % subtitle)
47 if subtitle_url:
c8602b2f
S
48 subtitles.setdefault('no', []).append({
49 'url': compat_urllib_parse_unquote(subtitle_url)
50 })
d8d540cf
S
51 entries.append({
52 'id': asset.get('carrierId') or entry_id,
53 'title': entry_title,
54 'duration': duration,
55 'subtitles': subtitles,
56 'formats': formats,
57 })
d2176c80 58
d8d540cf
S
59 if not entries:
60 media_url = data.get('mediaUrl')
61 if media_url:
ad316425 62 formats = self._extract_akamai_formats(media_url, video_id)
d8d540cf
S
63 self._sort_formats(formats)
64 duration = parse_duration(data.get('duration'))
65 entries = [{
66 'id': video_id,
67 'title': title,
68 'duration': duration,
69 'formats': formats,
70 }]
d2176c80 71
d8d540cf
S
72 if not entries:
73 if data.get('usageRights', {}).get('isGeoBlocked'):
874ae035
S
74 raise ExtractorError(
75 'NRK har ikke rettigheter til å vise dette programmet utenfor Norge',
76 expected=True)
77
d8d540cf
S
78 conviva = data.get('convivaStatistics') or {}
79 series = conviva.get('seriesName') or data.get('seriesTitle')
80 episode = conviva.get('episodeName') or data.get('episodeNumberOrDate')
393d9fc6 81
d8d540cf 82 thumbnails = None
d2176c80 83 images = data.get('images')
d8d540cf
S
84 if images and isinstance(images, dict):
85 web_images = images.get('webImages')
86 if isinstance(web_images, list):
87 thumbnails = [{
88 'url': image['imageUrl'],
89 'width': int_or_none(image.get('width')),
90 'height': int_or_none(image.get('height')),
91 } for image in web_images if image.get('imageUrl')]
92
93 description = data.get('description')
94
95 common_info = {
96 'description': description,
97 'series': series,
98 'episode': episode,
99 'age_limit': parse_age_limit(data.get('legalAge')),
100 'thumbnails': thumbnails,
dfb2e1a3
S
101 }
102
d8d540cf
S
103 vcodec = 'none' if data.get('mediaType') == 'Audio' else None
104
105 # TODO: extract chapters when https://github.com/rg3/youtube-dl/pull/9409 is merged
106
107 for entry in entries:
108 entry.update(common_info)
109 for f in entry['formats']:
110 f['vcodec'] = vcodec
111
112 return self.playlist_result(entries, video_id, title, description)
113
114
115class NRKIE(NRKBaseIE):
116 _VALID_URL = r'(?:nrk:|https?://(?:www\.)?nrk\.no/video/PS\*)(?P<id>\d+)'
117 _API_HOST = 'v8.psapi.nrk.no'
118 _TESTS = [{
119 # video
120 'url': 'http://www.nrk.no/video/PS*150533',
18cf6381 121 'md5': '2f7f6eeb2aacdd99885f355428715cfa',
d8d540cf
S
122 'info_dict': {
123 'id': '150533',
18cf6381 124 'ext': 'mp4',
d8d540cf
S
125 'title': 'Dompap og andre fugler i Piip-Show',
126 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
127 'duration': 263,
128 }
129 }, {
130 # audio
131 'url': 'http://www.nrk.no/video/PS*154915',
132 # MD5 is unstable
133 'info_dict': {
134 'id': '154915',
135 'ext': 'flv',
136 'title': 'Slik høres internett ut når du er blind',
137 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
138 'duration': 20,
139 }
140 }]
141
142
143class NRKTVIE(NRKBaseIE):
144 IE_DESC = 'NRK TV and NRK Radio'
145 _VALID_URL = r'https?://(?:tv|radio)\.nrk(?:super)?\.no/(?:serie/[^/]+|program)/(?P<id>[a-zA-Z]{4}\d{8})(?:/\d{2}-\d{2}-\d{4})?(?:#del=(?P<part_id>\d+))?'
146 _API_HOST = 'psapi-we.nrk.no'
147
148 _TESTS = [{
149 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
18cf6381 150 'md5': '4e9ca6629f09e588ed240fb11619922a',
d8d540cf 151 'info_dict': {
18cf6381 152 'id': 'MUHH48000314AA',
d8d540cf 153 'ext': 'mp4',
18cf6381 154 'title': '20 spørsmål 23.05.2014',
d8d540cf 155 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
4e790117 156 'duration': 1741,
d8d540cf 157 },
d8d540cf
S
158 }, {
159 'url': 'https://tv.nrk.no/program/mdfp15000514',
18cf6381 160 'md5': '43d0be26663d380603a9cf0c24366531',
d8d540cf 161 'info_dict': {
18cf6381 162 'id': 'MDFP15000514CA',
d8d540cf 163 'ext': 'mp4',
18cf6381 164 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
165 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
4e790117 166 'duration': 4605,
d8d540cf 167 },
d8d540cf
S
168 }, {
169 # single playlist video
170 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
171 'md5': 'adbd1dbd813edaf532b0a253780719c2',
172 'info_dict': {
173 'id': 'MSPO40010515-part2',
174 'ext': 'flv',
175 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
176 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
d8d540cf
S
177 },
178 'skip': 'Only works from Norway',
179 }, {
180 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
181 'playlist': [{
182 'md5': '9480285eff92d64f06e02a5367970a7a',
183 'info_dict': {
184 'id': 'MSPO40010515-part1',
185 'ext': 'flv',
186 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
187 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
d8d540cf
S
188 },
189 }, {
190 'md5': 'adbd1dbd813edaf532b0a253780719c2',
191 'info_dict': {
192 'id': 'MSPO40010515-part2',
193 'ext': 'flv',
194 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
195 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
d8d540cf
S
196 },
197 }],
198 'info_dict': {
199 'id': 'MSPO40010515',
200 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
201 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
d8d540cf
S
202 'duration': 6947.52,
203 },
204 'skip': 'Only works from Norway',
205 }, {
206 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
207 'only_matching': True,
208 }]
209
dfb2e1a3 210
faa1b5c2 211class NRKPlaylistIE(InfoExtractor):
3099b312 212 _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
faa1b5c2 213
a0914154 214 _TESTS = [{
faa1b5c2
S
215 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
216 'info_dict': {
217 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
218 'title': 'Gjenopplev den historiske solformørkelsen',
219 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
220 },
a0914154
S
221 'playlist_count': 2,
222 }, {
223 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
224 'info_dict': {
225 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
226 'title': 'Rivertonprisen til Karin Fossum',
227 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
228 },
229 'playlist_count': 5,
230 }]
faa1b5c2
S
231
232 def _real_extract(self, url):
233 playlist_id = self._match_id(url)
234
235 webpage = self._download_webpage(url, playlist_id)
236
237 entries = [
238 self.url_result('nrk:%s' % video_id, 'NRK')
239 for video_id in re.findall(
a0914154
S
240 r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"',
241 webpage)
faa1b5c2
S
242 ]
243
244 playlist_title = self._og_search_title(webpage)
245 playlist_description = self._og_search_description(webpage)
246
247 return self.playlist_result(
248 entries, playlist_id, playlist_title, playlist_description)
249
250
3099b312
S
251class NRKSkoleIE(InfoExtractor):
252 IE_DESC = 'NRK Skole'
971e3b75 253 _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
3099b312
S
254
255 _TESTS = [{
971e3b75
S
256 'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
257 'md5': '6bc936b01f9dd8ed45bc58b252b2d9b6',
3099b312
S
258 'info_dict': {
259 'id': '6021',
971e3b75 260 'ext': 'mp4',
3099b312
S
261 'title': 'Genetikk og eneggede tvillinger',
262 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
263 'duration': 399,
264 },
265 }, {
971e3b75 266 'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
61140904 267 'only_matching': True,
3099b312
S
268 }]
269
270 def _real_extract(self, url):
971e3b75
S
271 video_id = self._match_id(url)
272
273 webpage = self._download_webpage(
274 'https://mimir.nrk.no/plugin/1.0/static?mediaId=%s' % video_id,
275 video_id)
3099b312 276
971e3b75
S
277 nrk_id = self._parse_json(
278 self._search_regex(
279 r'<script[^>]+type=["\']application/json["\'][^>]*>({.+?})</script>',
280 webpage, 'application json'),
281 video_id)['activeMedia']['psId']
3099b312 282
3099b312 283 return self.url_result('nrk:%s' % nrk_id)