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