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