Merge branch 'kuwo' of https://github.com/ping/youtube-dl into ping-kuwo
[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 ..utils import (
8     ExtractorError,
9     float_or_none,
10     parse_duration,
11     unified_strdate,
12 )
13
14
15 class NRKIE(InfoExtractor):
16     _VALID_URL = r'(?:nrk:|https?://(?:www\.)?nrk\.no/video/PS\*)(?P<id>\d+)'
17
18     _TESTS = [
19         {
20             'url': 'http://www.nrk.no/video/PS*150533',
21             'md5': 'bccd850baebefe23b56d708a113229c2',
22             'info_dict': {
23                 'id': '150533',
24                 'ext': 'flv',
25                 'title': 'Dompap og andre fugler i Piip-Show',
26                 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
27                 'duration': 263,
28             }
29         },
30         {
31             'url': 'http://www.nrk.no/video/PS*154915',
32             'md5': '0b1493ba1aae7d9579a5ad5531bc395a',
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                 'duration': 20,
39             }
40         },
41     ]
42
43     def _real_extract(self, url):
44         video_id = self._match_id(url)
45
46         data = self._download_json(
47             'http://v8.psapi.nrk.no/mediaelement/%s' % video_id,
48             video_id, 'Downloading media JSON')
49
50         if data['usageRights']['isGeoBlocked']:
51             raise ExtractorError(
52                 'NRK har ikke rettig-heter til å vise dette programmet utenfor Norge',
53                 expected=True)
54
55         video_url = data['mediaUrl'] + '?hdcore=3.5.0&plugin=aasp-3.5.0.151.81'
56
57         duration = parse_duration(data.get('duration'))
58
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'],
73             'duration': duration,
74             'thumbnail': thumbnail,
75         }
76
77
78 class NRKPlaylistIE(InfoExtractor):
79     _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video)(?:[^/]+/)+(?P<id>[^/]+)'
80
81     _TESTS = [{
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         },
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     }]
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(
107                 r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"',
108                 webpage)
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
118 class NRKTVIE(InfoExtractor):
119     _VALID_URL = r'(?P<baseurl>https?://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+))?'
120
121     _TESTS = [
122         {
123             'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
124             'md5': 'adf2c5454fa2bf032f47a9f8fb351342',
125             'info_dict': {
126                 'id': 'MUHH48000314',
127                 'ext': 'flv',
128                 'title': '20 spørsmål',
129                 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
130                 'upload_date': '20140523',
131                 'duration': 1741.52,
132             },
133         },
134         {
135             'url': 'https://tv.nrk.no/program/mdfp15000514',
136             'md5': '383650ece2b25ecec996ad7b5bb2a384',
137             'info_dict': {
138                 'id': 'mdfp15000514',
139                 'ext': 'flv',
140                 'title': 'Kunnskapskanalen: Grunnlovsjubiléet - Stor ståhei for ingenting',
141                 'description': 'md5:654c12511f035aed1e42bdf5db3b206a',
142                 'upload_date': '20140524',
143                 'duration': 4605.0,
144             },
145         },
146         {
147             # single playlist video
148             'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
149             'md5': 'adbd1dbd813edaf532b0a253780719c2',
150             'info_dict': {
151                 'id': 'MSPO40010515-part2',
152                 'ext': 'flv',
153                 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
154                 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
155                 'upload_date': '20150106',
156             },
157             'skip': 'Only works from Norway',
158         },
159         {
160             'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
161             'playlist': [
162                 {
163                     'md5': '9480285eff92d64f06e02a5367970a7a',
164                     'info_dict': {
165                         'id': 'MSPO40010515-part1',
166                         'ext': 'flv',
167                         'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
168                         'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
169                         'upload_date': '20150106',
170                     },
171                 },
172                 {
173                     'md5': 'adbd1dbd813edaf532b0a253780719c2',
174                     'info_dict': {
175                         'id': 'MSPO40010515-part2',
176                         'ext': 'flv',
177                         'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
178                         'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
179                         'upload_date': '20150106',
180                     },
181                 },
182             ],
183             'info_dict': {
184                 'id': 'MSPO40010515',
185                 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
186                 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
187                 'upload_date': '20150106',
188                 'duration': 6947.5199999999995,
189             },
190             'skip': 'Only works from Norway',
191         }
192     ]
193
194     def _debug_print(self, txt):
195         if self._downloader.params.get('verbose', False):
196             self.to_screen('[debug] %s' % txt)
197
198     def _get_subtitles(self, subtitlesurl, video_id, baseurl):
199         url = "%s%s" % (baseurl, subtitlesurl)
200         self._debug_print('%s: Subtitle url: %s' % (video_id, url))
201         captions = self._download_xml(
202             url, video_id, 'Downloading subtitles')
203         lang = captions.get('lang', 'no')
204         return {lang: [
205             {'ext': 'ttml', 'url': url},
206         ]}
207
208     def _extract_f4m(self, manifest_url, video_id):
209         return self._extract_f4m_formats(manifest_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124', video_id)
210
211     def _real_extract(self, url):
212         mobj = re.match(self._VALID_URL, url)
213         video_id = mobj.group('id')
214         part_id = mobj.group('part_id')
215         baseurl = mobj.group('baseurl')
216
217         webpage = self._download_webpage(url, video_id)
218
219         title = self._html_search_meta(
220             'title', webpage, 'title')
221         description = self._html_search_meta(
222             'description', webpage, 'description')
223
224         thumbnail = self._html_search_regex(
225             r'data-posterimage="([^"]+)"',
226             webpage, 'thumbnail', fatal=False)
227         upload_date = unified_strdate(self._html_search_meta(
228             'rightsfrom', webpage, 'upload date', fatal=False))
229         duration = float_or_none(self._html_search_regex(
230             r'data-duration="([^"]+)"',
231             webpage, 'duration', fatal=False))
232
233         # playlist
234         parts = re.findall(
235             r'<a href="#del=(\d+)"[^>]+data-argument="([^"]+)">([^<]+)</a>', webpage)
236         if parts:
237             entries = []
238             for current_part_id, stream_url, part_title in parts:
239                 if part_id and current_part_id != part_id:
240                     continue
241                 video_part_id = '%s-part%s' % (video_id, current_part_id)
242                 formats = self._extract_f4m(stream_url, video_part_id)
243                 entries.append({
244                     'id': video_part_id,
245                     'title': part_title,
246                     'description': description,
247                     'thumbnail': thumbnail,
248                     'upload_date': upload_date,
249                     'formats': formats,
250                 })
251             if part_id:
252                 if entries:
253                     return entries[0]
254             else:
255                 playlist = self.playlist_result(entries, video_id, title, description)
256                 playlist.update({
257                     'thumbnail': thumbnail,
258                     'upload_date': upload_date,
259                     'duration': duration,
260                 })
261                 return playlist
262
263         formats = []
264
265         f4m_url = re.search(r'data-media="([^"]+)"', webpage)
266         if f4m_url:
267             formats.extend(self._extract_f4m(f4m_url.group(1), video_id))
268
269         m3u8_url = re.search(r'data-hls-media="([^"]+)"', webpage)
270         if m3u8_url:
271             formats.extend(self._extract_m3u8_formats(m3u8_url.group(1), video_id, 'mp4'))
272         self._sort_formats(formats)
273
274         subtitles_url = self._html_search_regex(
275             r'data-subtitlesurl[ ]*=[ ]*"([^"]+)"',
276             webpage, 'subtitle URL', default=None)
277         subtitles = None
278         if subtitles_url:
279             subtitles = self.extract_subtitles(subtitles_url, video_id, baseurl)
280
281         return {
282             'id': video_id,
283             'title': title,
284             'description': description,
285             'thumbnail': thumbnail,
286             'upload_date': upload_date,
287             'duration': duration,
288             'formats': formats,
289             'subtitles': subtitles,
290         }