]>
Commit | Line | Data |
---|---|---|
d2176c80 S |
1 | # encoding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import re | |
5 | ||
6 | from .common import InfoExtractor | |
dfb2e1a3 S |
7 | from ..utils import ( |
8 | ExtractorError, | |
94128d6b | 9 | float_or_none, |
76bfaf6d | 10 | parse_duration, |
dfb2e1a3 S |
11 | unified_strdate, |
12 | ) | |
d2176c80 S |
13 | |
14 | ||
15 | class NRKIE(InfoExtractor): | |
c4bd188d | 16 | _VALID_URL = r'(?:nrk:|https?://(?:www\.)?nrk\.no/video/PS\*)(?P<id>\d+)' |
d2176c80 S |
17 | |
18 | _TESTS = [ | |
19 | { | |
4e6a2286 S |
20 | 'url': 'http://www.nrk.no/video/PS*150533', |
21 | 'md5': 'bccd850baebefe23b56d708a113229c2', | |
d2176c80 S |
22 | 'info_dict': { |
23 | 'id': '150533', | |
24 | 'ext': 'flv', | |
25 | 'title': 'Dompap og andre fugler i Piip-Show', | |
4e6a2286 | 26 | 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f', |
393d9fc6 | 27 | 'duration': 263, |
d2176c80 S |
28 | } |
29 | }, | |
30 | { | |
4e6a2286 S |
31 | 'url': 'http://www.nrk.no/video/PS*154915', |
32 | 'md5': '0b1493ba1aae7d9579a5ad5531bc395a', | |
d2176c80 S |
33 | 'info_dict': { |
34 | 'id': '154915', | |
35 | 'ext': 'flv', | |
36 | 'title': 'Slik høres internett ut når du er blind', | |
37 | 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568', | |
393d9fc6 | 38 | 'duration': 20, |
d2176c80 S |
39 | } |
40 | }, | |
41 | ] | |
42 | ||
43 | def _real_extract(self, url): | |
4e6a2286 | 44 | video_id = self._match_id(url) |
d2176c80 S |
45 | |
46 | data = self._download_json( | |
4e6a2286 S |
47 | 'http://v8.psapi.nrk.no/mediaelement/%s' % video_id, |
48 | video_id, 'Downloading media JSON') | |
d2176c80 S |
49 | |
50 | if data['usageRights']['isGeoBlocked']: | |
4e6a2286 S |
51 | raise ExtractorError( |
52 | 'NRK har ikke rettig-heter til å vise dette programmet utenfor Norge', | |
53 | expected=True) | |
d2176c80 | 54 | |
4e6a2286 | 55 | video_url = data['mediaUrl'] + '?hdcore=3.5.0&plugin=aasp-3.5.0.151.81' |
d2176c80 | 56 | |
393d9fc6 S |
57 | duration = parse_duration(data.get('duration')) |
58 | ||
d2176c80 S |
59 | images = data.get('images') |
60 | if images: | |
61 | thumbnails = images['webImages'] | |
62 | thumbnails.sort(key=lambda image: image['pixelWidth']) | |
63 | thumbnail = thumbnails[-1]['imageUrl'] | |
64 | else: | |
65 | thumbnail = None | |
66 | ||
67 | return { | |
68 | 'id': video_id, | |
69 | 'url': video_url, | |
70 | 'ext': 'flv', | |
71 | 'title': data['title'], | |
72 | 'description': data['description'], | |
393d9fc6 | 73 | 'duration': duration, |
d2176c80 | 74 | 'thumbnail': thumbnail, |
dfb2e1a3 S |
75 | } |
76 | ||
77 | ||
faa1b5c2 | 78 | class NRKPlaylistIE(InfoExtractor): |
c4bd188d | 79 | _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video)(?:[^/]+/)+(?P<id>[^/]+)' |
faa1b5c2 | 80 | |
a0914154 | 81 | _TESTS = [{ |
faa1b5c2 S |
82 | 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763', |
83 | 'info_dict': { | |
84 | 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763', | |
85 | 'title': 'Gjenopplev den historiske solformørkelsen', | |
86 | 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed', | |
87 | }, | |
a0914154 S |
88 | 'playlist_count': 2, |
89 | }, { | |
90 | 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449', | |
91 | 'info_dict': { | |
92 | 'id': 'rivertonprisen-til-karin-fossum-1.12266449', | |
93 | 'title': 'Rivertonprisen til Karin Fossum', | |
94 | 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.', | |
95 | }, | |
96 | 'playlist_count': 5, | |
97 | }] | |
faa1b5c2 S |
98 | |
99 | def _real_extract(self, url): | |
100 | playlist_id = self._match_id(url) | |
101 | ||
102 | webpage = self._download_webpage(url, playlist_id) | |
103 | ||
104 | entries = [ | |
105 | self.url_result('nrk:%s' % video_id, 'NRK') | |
106 | for video_id in re.findall( | |
a0914154 S |
107 | r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"', |
108 | webpage) | |
faa1b5c2 S |
109 | ] |
110 | ||
111 | playlist_title = self._og_search_title(webpage) | |
112 | playlist_description = self._og_search_description(webpage) | |
113 | ||
114 | return self.playlist_result( | |
115 | entries, playlist_id, playlist_title, playlist_description) | |
116 | ||
117 | ||
01561da1 | 118 | class NRKTVIE(InfoExtractor): |
667170e2 S |
119 | IE_DESC = 'NRK TV and NRK Radio' |
120 | _VALID_URL = r'(?P<baseurl>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+))?' | |
dfb2e1a3 S |
121 | |
122 | _TESTS = [ | |
123 | { | |
c4bd188d | 124 | 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014', |
5878e639 | 125 | 'md5': 'adf2c5454fa2bf032f47a9f8fb351342', |
dfb2e1a3 | 126 | 'info_dict': { |
70126312 | 127 | 'id': 'MUHH48000314', |
dfb2e1a3 S |
128 | 'ext': 'flv', |
129 | 'title': '20 spørsmål', | |
130 | 'description': 'md5:bdea103bc35494c143c6a9acdd84887a', | |
131 | 'upload_date': '20140523', | |
132 | 'duration': 1741.52, | |
799d88d3 | 133 | }, |
dfb2e1a3 S |
134 | }, |
135 | { | |
c4bd188d | 136 | 'url': 'https://tv.nrk.no/program/mdfp15000514', |
5878e639 | 137 | 'md5': '383650ece2b25ecec996ad7b5bb2a384', |
dfb2e1a3 S |
138 | 'info_dict': { |
139 | 'id': 'mdfp15000514', | |
140 | 'ext': 'flv', | |
141 | 'title': 'Kunnskapskanalen: Grunnlovsjubiléet - Stor ståhei for ingenting', | |
142 | 'description': 'md5:654c12511f035aed1e42bdf5db3b206a', | |
143 | 'upload_date': '20140524', | |
144 | 'duration': 4605.0, | |
799d88d3 | 145 | }, |
dfb2e1a3 | 146 | }, |
799d88d3 S |
147 | { |
148 | # single playlist video | |
c4bd188d | 149 | 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2', |
799d88d3 S |
150 | 'md5': 'adbd1dbd813edaf532b0a253780719c2', |
151 | 'info_dict': { | |
152 | 'id': 'MSPO40010515-part2', | |
153 | 'ext': 'flv', | |
154 | 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)', | |
155 | 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26', | |
156 | 'upload_date': '20150106', | |
157 | }, | |
158 | 'skip': 'Only works from Norway', | |
799d88d3 S |
159 | }, |
160 | { | |
c4bd188d | 161 | 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015', |
799d88d3 S |
162 | 'playlist': [ |
163 | { | |
164 | 'md5': '9480285eff92d64f06e02a5367970a7a', | |
165 | 'info_dict': { | |
166 | 'id': 'MSPO40010515-part1', | |
167 | 'ext': 'flv', | |
168 | 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)', | |
169 | 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26', | |
170 | 'upload_date': '20150106', | |
171 | }, | |
172 | }, | |
173 | { | |
174 | 'md5': 'adbd1dbd813edaf532b0a253780719c2', | |
175 | 'info_dict': { | |
176 | 'id': 'MSPO40010515-part2', | |
177 | 'ext': 'flv', | |
178 | 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)', | |
179 | 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26', | |
180 | 'upload_date': '20150106', | |
181 | }, | |
182 | }, | |
183 | ], | |
184 | 'info_dict': { | |
185 | 'id': 'MSPO40010515', | |
186 | 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn', | |
187 | 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26', | |
188 | 'upload_date': '20150106', | |
189 | 'duration': 6947.5199999999995, | |
190 | }, | |
191 | 'skip': 'Only works from Norway', | |
667170e2 S |
192 | }, |
193 | { | |
194 | 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#', | |
195 | 'only_matching': True, | |
799d88d3 | 196 | } |
dfb2e1a3 S |
197 | ] |
198 | ||
76bfaf6d PH |
199 | def _debug_print(self, txt): |
200 | if self._downloader.params.get('verbose', False): | |
201 | self.to_screen('[debug] %s' % txt) | |
202 | ||
01561da1 | 203 | def _get_subtitles(self, subtitlesurl, video_id, baseurl): |
76bfaf6d PH |
204 | url = "%s%s" % (baseurl, subtitlesurl) |
205 | self._debug_print('%s: Subtitle url: %s' % (video_id, url)) | |
41b264e7 | 206 | captions = self._download_xml( |
1c7e2e64 | 207 | url, video_id, 'Downloading subtitles') |
76bfaf6d | 208 | lang = captions.get('lang', 'no') |
01561da1 JMF |
209 | return {lang: [ |
210 | {'ext': 'ttml', 'url': url}, | |
01561da1 | 211 | ]} |
76bfaf6d | 212 | |
799d88d3 | 213 | def _extract_f4m(self, manifest_url, video_id): |
c4f1fde7 S |
214 | return self._extract_f4m_formats( |
215 | manifest_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124', video_id, f4m_id='hds') | |
799d88d3 | 216 | |
dfb2e1a3 S |
217 | def _real_extract(self, url): |
218 | mobj = re.match(self._VALID_URL, url) | |
219 | video_id = mobj.group('id') | |
799d88d3 | 220 | part_id = mobj.group('part_id') |
76bfaf6d | 221 | baseurl = mobj.group('baseurl') |
799d88d3 S |
222 | |
223 | webpage = self._download_webpage(url, video_id) | |
224 | ||
225 | title = self._html_search_meta( | |
226 | 'title', webpage, 'title') | |
227 | description = self._html_search_meta( | |
228 | 'description', webpage, 'description') | |
229 | ||
230 | thumbnail = self._html_search_regex( | |
231 | r'data-posterimage="([^"]+)"', | |
232 | webpage, 'thumbnail', fatal=False) | |
233 | upload_date = unified_strdate(self._html_search_meta( | |
234 | 'rightsfrom', webpage, 'upload date', fatal=False)) | |
235 | duration = float_or_none(self._html_search_regex( | |
236 | r'data-duration="([^"]+)"', | |
237 | webpage, 'duration', fatal=False)) | |
238 | ||
239 | # playlist | |
240 | parts = re.findall( | |
241 | r'<a href="#del=(\d+)"[^>]+data-argument="([^"]+)">([^<]+)</a>', webpage) | |
242 | if parts: | |
243 | entries = [] | |
244 | for current_part_id, stream_url, part_title in parts: | |
245 | if part_id and current_part_id != part_id: | |
246 | continue | |
247 | video_part_id = '%s-part%s' % (video_id, current_part_id) | |
248 | formats = self._extract_f4m(stream_url, video_part_id) | |
249 | entries.append({ | |
250 | 'id': video_part_id, | |
251 | 'title': part_title, | |
252 | 'description': description, | |
253 | 'thumbnail': thumbnail, | |
254 | 'upload_date': upload_date, | |
255 | 'formats': formats, | |
256 | }) | |
257 | if part_id: | |
258 | if entries: | |
259 | return entries[0] | |
260 | else: | |
261 | playlist = self.playlist_result(entries, video_id, title, description) | |
262 | playlist.update({ | |
263 | 'thumbnail': thumbnail, | |
264 | 'upload_date': upload_date, | |
265 | 'duration': duration, | |
266 | }) | |
267 | return playlist | |
dfb2e1a3 S |
268 | |
269 | formats = [] | |
270 | ||
799d88d3 | 271 | f4m_url = re.search(r'data-media="([^"]+)"', webpage) |
dfb2e1a3 | 272 | if f4m_url: |
799d88d3 | 273 | formats.extend(self._extract_f4m(f4m_url.group(1), video_id)) |
dfb2e1a3 | 274 | |
799d88d3 | 275 | m3u8_url = re.search(r'data-hls-media="([^"]+)"', webpage) |
dfb2e1a3 | 276 | if m3u8_url: |
c4f1fde7 | 277 | formats.extend(self._extract_m3u8_formats(m3u8_url.group(1), video_id, 'mp4', m3u8_id='hls')) |
dfb2e1a3 S |
278 | self._sort_formats(formats) |
279 | ||
76bfaf6d PH |
280 | subtitles_url = self._html_search_regex( |
281 | r'data-subtitlesurl[ ]*=[ ]*"([^"]+)"', | |
282 | webpage, 'subtitle URL', default=None) | |
283 | subtitles = None | |
284 | if subtitles_url: | |
01561da1 | 285 | subtitles = self.extract_subtitles(subtitles_url, video_id, baseurl) |
76bfaf6d | 286 | |
dfb2e1a3 S |
287 | return { |
288 | 'id': video_id, | |
289 | 'title': title, | |
290 | 'description': description, | |
291 | 'thumbnail': thumbnail, | |
292 | 'upload_date': upload_date, | |
293 | 'duration': duration, | |
294 | 'formats': formats, | |
76bfaf6d | 295 | 'subtitles': subtitles, |
e6c9f80c | 296 | } |