]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/nrk.py
[nrk] Adapt to new URL format
[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',
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',
38 }
39 },
40 ]
41
42 def _real_extract(self, url):
4e6a2286 43 video_id = self._match_id(url)
d2176c80
S
44
45 data = self._download_json(
4e6a2286
S
46 'http://v8.psapi.nrk.no/mediaelement/%s' % video_id,
47 video_id, 'Downloading media JSON')
d2176c80
S
48
49 if data['usageRights']['isGeoBlocked']:
4e6a2286
S
50 raise ExtractorError(
51 'NRK har ikke rettig-heter til å vise dette programmet utenfor Norge',
52 expected=True)
d2176c80 53
4e6a2286 54 video_url = data['mediaUrl'] + '?hdcore=3.5.0&plugin=aasp-3.5.0.151.81'
d2176c80
S
55
56 images = data.get('images')
57 if images:
58 thumbnails = images['webImages']
59 thumbnails.sort(key=lambda image: image['pixelWidth'])
60 thumbnail = thumbnails[-1]['imageUrl']
61 else:
62 thumbnail = None
63
64 return {
65 'id': video_id,
66 'url': video_url,
67 'ext': 'flv',
68 'title': data['title'],
69 'description': data['description'],
70 'thumbnail': thumbnail,
dfb2e1a3
S
71 }
72
73
01561da1 74class NRKTVIE(InfoExtractor):
76bfaf6d 75 _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
76
77 _TESTS = [
78 {
e6c9f80c 79 'url': 'http://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
5878e639 80 'md5': 'adf2c5454fa2bf032f47a9f8fb351342',
dfb2e1a3 81 'info_dict': {
70126312 82 'id': 'MUHH48000314',
dfb2e1a3
S
83 'ext': 'flv',
84 'title': '20 spørsmål',
85 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
86 'upload_date': '20140523',
87 'duration': 1741.52,
799d88d3 88 },
dfb2e1a3
S
89 },
90 {
91 'url': 'http://tv.nrk.no/program/mdfp15000514',
5878e639 92 'md5': '383650ece2b25ecec996ad7b5bb2a384',
dfb2e1a3
S
93 'info_dict': {
94 'id': 'mdfp15000514',
95 'ext': 'flv',
96 'title': 'Kunnskapskanalen: Grunnlovsjubiléet - Stor ståhei for ingenting',
97 'description': 'md5:654c12511f035aed1e42bdf5db3b206a',
98 'upload_date': '20140524',
99 'duration': 4605.0,
799d88d3 100 },
dfb2e1a3 101 },
799d88d3
S
102 {
103 # single playlist video
104 'url': 'http://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
105 'md5': 'adbd1dbd813edaf532b0a253780719c2',
106 'info_dict': {
107 'id': 'MSPO40010515-part2',
108 'ext': 'flv',
109 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
110 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
111 'upload_date': '20150106',
112 },
113 'skip': 'Only works from Norway',
799d88d3
S
114 },
115 {
116 'url': 'http://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
117 'playlist': [
118 {
119 'md5': '9480285eff92d64f06e02a5367970a7a',
120 'info_dict': {
121 'id': 'MSPO40010515-part1',
122 'ext': 'flv',
123 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
124 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
125 'upload_date': '20150106',
126 },
127 },
128 {
129 'md5': 'adbd1dbd813edaf532b0a253780719c2',
130 'info_dict': {
131 'id': 'MSPO40010515-part2',
132 'ext': 'flv',
133 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
134 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
135 'upload_date': '20150106',
136 },
137 },
138 ],
139 'info_dict': {
140 'id': 'MSPO40010515',
141 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
142 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
143 'upload_date': '20150106',
144 'duration': 6947.5199999999995,
145 },
146 'skip': 'Only works from Norway',
799d88d3 147 }
dfb2e1a3
S
148 ]
149
76bfaf6d
PH
150 def _debug_print(self, txt):
151 if self._downloader.params.get('verbose', False):
152 self.to_screen('[debug] %s' % txt)
153
01561da1 154 def _get_subtitles(self, subtitlesurl, video_id, baseurl):
76bfaf6d
PH
155 url = "%s%s" % (baseurl, subtitlesurl)
156 self._debug_print('%s: Subtitle url: %s' % (video_id, url))
41b264e7
S
157 captions = self._download_xml(
158 url, video_id, 'Downloading subtitles',
159 transform_source=lambda s: s.replace(r'<br />', '\r\n'))
76bfaf6d
PH
160 lang = captions.get('lang', 'no')
161 ps = captions.findall('./{0}body/{0}div/{0}p'.format('{http://www.w3.org/ns/ttml}'))
162 srt = ''
163 for pos, p in enumerate(ps):
164 begin = parse_duration(p.get('begin'))
165 duration = parse_duration(p.get('dur'))
545315a9
S
166 starttime = self._subtitles_timecode(begin)
167 endtime = self._subtitles_timecode(begin + duration)
41b264e7 168 srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (compat_str(pos), starttime, endtime, p.text)
01561da1
JMF
169 return {lang: [
170 {'ext': 'ttml', 'url': url},
171 {'ext': 'srt', 'data': srt},
172 ]}
76bfaf6d 173
799d88d3
S
174 def _extract_f4m(self, manifest_url, video_id):
175 return self._extract_f4m_formats(manifest_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124', video_id)
176
dfb2e1a3
S
177 def _real_extract(self, url):
178 mobj = re.match(self._VALID_URL, url)
179 video_id = mobj.group('id')
799d88d3 180 part_id = mobj.group('part_id')
76bfaf6d 181 baseurl = mobj.group('baseurl')
799d88d3
S
182
183 webpage = self._download_webpage(url, video_id)
184
185 title = self._html_search_meta(
186 'title', webpage, 'title')
187 description = self._html_search_meta(
188 'description', webpage, 'description')
189
190 thumbnail = self._html_search_regex(
191 r'data-posterimage="([^"]+)"',
192 webpage, 'thumbnail', fatal=False)
193 upload_date = unified_strdate(self._html_search_meta(
194 'rightsfrom', webpage, 'upload date', fatal=False))
195 duration = float_or_none(self._html_search_regex(
196 r'data-duration="([^"]+)"',
197 webpage, 'duration', fatal=False))
198
199 # playlist
200 parts = re.findall(
201 r'<a href="#del=(\d+)"[^>]+data-argument="([^"]+)">([^<]+)</a>', webpage)
202 if parts:
203 entries = []
204 for current_part_id, stream_url, part_title in parts:
205 if part_id and current_part_id != part_id:
206 continue
207 video_part_id = '%s-part%s' % (video_id, current_part_id)
208 formats = self._extract_f4m(stream_url, video_part_id)
209 entries.append({
210 'id': video_part_id,
211 'title': part_title,
212 'description': description,
213 'thumbnail': thumbnail,
214 'upload_date': upload_date,
215 'formats': formats,
216 })
217 if part_id:
218 if entries:
219 return entries[0]
220 else:
221 playlist = self.playlist_result(entries, video_id, title, description)
222 playlist.update({
223 'thumbnail': thumbnail,
224 'upload_date': upload_date,
225 'duration': duration,
226 })
227 return playlist
dfb2e1a3
S
228
229 formats = []
230
799d88d3 231 f4m_url = re.search(r'data-media="([^"]+)"', webpage)
dfb2e1a3 232 if f4m_url:
799d88d3 233 formats.extend(self._extract_f4m(f4m_url.group(1), video_id))
dfb2e1a3 234
799d88d3 235 m3u8_url = re.search(r'data-hls-media="([^"]+)"', webpage)
dfb2e1a3 236 if m3u8_url:
799d88d3 237 formats.extend(self._extract_m3u8_formats(m3u8_url.group(1), video_id, 'mp4'))
dfb2e1a3
S
238 self._sort_formats(formats)
239
76bfaf6d
PH
240 subtitles_url = self._html_search_regex(
241 r'data-subtitlesurl[ ]*=[ ]*"([^"]+)"',
242 webpage, 'subtitle URL', default=None)
243 subtitles = None
244 if subtitles_url:
01561da1 245 subtitles = self.extract_subtitles(subtitles_url, video_id, baseurl)
76bfaf6d 246
dfb2e1a3
S
247 return {
248 'id': video_id,
249 'title': title,
250 'description': description,
251 'thumbnail': thumbnail,
252 'upload_date': upload_date,
253 'duration': duration,
254 'formats': formats,
76bfaf6d 255 'subtitles': subtitles,
e6c9f80c 256 }