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