[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / onet.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     determine_ext,
9     ExtractorError,
10     float_or_none,
11     get_element_by_class,
12     int_or_none,
13     js_to_json,
14     NO_DEFAULT,
15     parse_iso8601,
16     remove_start,
17     strip_or_none,
18     url_basename,
19 )
20
21
22 class OnetBaseIE(InfoExtractor):
23     _URL_BASE_RE = r'https?://(?:(?:www\.)?onet\.tv|onet100\.vod\.pl)/[a-z]/'
24
25     def _search_mvp_id(self, webpage):
26         return self._search_regex(
27             r'id=(["\'])mvp:(?P<id>.+?)\1', webpage, 'mvp id', group='id')
28
29     def _extract_from_id(self, video_id, webpage=None):
30         response = self._download_json(
31             'http://qi.ckm.onetapi.pl/', video_id,
32             query={
33                 'body[id]': video_id,
34                 'body[jsonrpc]': '2.0',
35                 'body[method]': 'get_asset_detail',
36                 'body[params][ID_Publikacji]': video_id,
37                 'body[params][Service]': 'www.onet.pl',
38                 'content-type': 'application/jsonp',
39                 'x-onet-app': 'player.front.onetapi.pl',
40             })
41
42         error = response.get('error')
43         if error:
44             raise ExtractorError(
45                 '%s said: %s' % (self.IE_NAME, error['message']), expected=True)
46
47         video = response['result'].get('0')
48
49         formats = []
50         for format_type, formats_dict in video['formats'].items():
51             if not isinstance(formats_dict, dict):
52                 continue
53             for format_id, format_list in formats_dict.items():
54                 if not isinstance(format_list, list):
55                     continue
56                 for f in format_list:
57                     video_url = f.get('url')
58                     if not video_url:
59                         continue
60                     ext = determine_ext(video_url)
61                     if format_id.startswith('ism'):
62                         formats.extend(self._extract_ism_formats(
63                             video_url, video_id, 'mss', fatal=False))
64                     elif ext == 'mpd':
65                         formats.extend(self._extract_mpd_formats(
66                             video_url, video_id, mpd_id='dash', fatal=False))
67                     elif format_id.startswith('hls'):
68                         formats.extend(self._extract_m3u8_formats(
69                             video_url, video_id, 'mp4', 'm3u8_native',
70                             m3u8_id='hls', fatal=False))
71                     else:
72                         http_f = {
73                             'url': video_url,
74                             'format_id': format_id,
75                             'abr': float_or_none(f.get('audio_bitrate')),
76                         }
77                         if format_type == 'audio':
78                             http_f['vcodec'] = 'none'
79                         else:
80                             http_f.update({
81                                 'height': int_or_none(f.get('vertical_resolution')),
82                                 'width': int_or_none(f.get('horizontal_resolution')),
83                                 'vbr': float_or_none(f.get('video_bitrate')),
84                             })
85                         formats.append(http_f)
86         self._sort_formats(formats)
87
88         meta = video.get('meta', {})
89
90         title = (self._og_search_title(
91             webpage, default=None) if webpage else None) or meta['title']
92         description = (self._og_search_description(
93             webpage, default=None) if webpage else None) or meta.get('description')
94         duration = meta.get('length') or meta.get('lenght')
95         timestamp = parse_iso8601(meta.get('addDate'), ' ')
96
97         return {
98             'id': video_id,
99             'title': title,
100             'description': description,
101             'duration': duration,
102             'timestamp': timestamp,
103             'formats': formats,
104         }
105
106
107 class OnetMVPIE(OnetBaseIE):
108     _VALID_URL = r'onetmvp:(?P<id>\d+\.\d+)'
109
110     _TEST = {
111         'url': 'onetmvp:381027.1509591944',
112         'only_matching': True,
113     }
114
115     def _real_extract(self, url):
116         return self._extract_from_id(self._match_id(url))
117
118
119 class OnetIE(OnetBaseIE):
120     _VALID_URL = OnetBaseIE._URL_BASE_RE + r'[a-z]+/(?P<display_id>[0-9a-z-]+)/(?P<id>[0-9a-z]+)'
121     IE_NAME = 'onet.tv'
122
123     _TESTS = [{
124         'url': 'http://onet.tv/k/openerfestival/open-er-festival-2016-najdziwniejsze-wymagania-gwiazd/qbpyqc',
125         'md5': '436102770fb095c75b8bb0392d3da9ff',
126         'info_dict': {
127             'id': 'qbpyqc',
128             'display_id': 'open-er-festival-2016-najdziwniejsze-wymagania-gwiazd',
129             'ext': 'mp4',
130             'title': 'Open\'er Festival 2016: najdziwniejsze wymagania gwiazd',
131             'description': 'Trzy samochody, których nigdy nie użyto, prywatne spa, hotel dekorowany czarnym suknem czy nielegalne używki. Organizatorzy koncertów i festiwali muszą stawać przed nie lada wyzwaniem zapraszając gwia...',
132             'upload_date': '20160705',
133             'timestamp': 1467721580,
134         },
135     }, {
136         'url': 'https://onet100.vod.pl/k/openerfestival/open-er-festival-2016-najdziwniejsze-wymagania-gwiazd/qbpyqc',
137         'only_matching': True,
138     }]
139
140     def _real_extract(self, url):
141         mobj = re.match(self._VALID_URL, url)
142         display_id, video_id = mobj.group('display_id', 'id')
143
144         webpage = self._download_webpage(url, display_id)
145
146         mvp_id = self._search_mvp_id(webpage)
147
148         info_dict = self._extract_from_id(mvp_id, webpage)
149         info_dict.update({
150             'id': video_id,
151             'display_id': display_id,
152         })
153
154         return info_dict
155
156
157 class OnetChannelIE(OnetBaseIE):
158     _VALID_URL = OnetBaseIE._URL_BASE_RE + r'(?P<id>[a-z]+)(?:[?#]|$)'
159     IE_NAME = 'onet.tv:channel'
160
161     _TESTS = [{
162         'url': 'http://onet.tv/k/openerfestival',
163         'info_dict': {
164             'id': 'openerfestival',
165             'title': "Open'er Festival",
166             'description': "Tak było na Open'er Festival 2016! Oglądaj nasze reportaże i wywiady z artystami.",
167         },
168         'playlist_mincount': 35,
169     }, {
170         'url': 'https://onet100.vod.pl/k/openerfestival',
171         'only_matching': True,
172     }]
173
174     def _real_extract(self, url):
175         channel_id = self._match_id(url)
176
177         webpage = self._download_webpage(url, channel_id)
178
179         current_clip_info = self._parse_json(self._search_regex(
180             r'var\s+currentClip\s*=\s*({[^}]+})', webpage, 'video info'), channel_id,
181             transform_source=lambda s: js_to_json(re.sub(r'\'\s*\+\s*\'', '', s)))
182         video_id = remove_start(current_clip_info['ckmId'], 'mvp:')
183         video_name = url_basename(current_clip_info['url'])
184
185         if self._downloader.params.get('noplaylist'):
186             self.to_screen(
187                 'Downloading just video %s because of --no-playlist' % video_name)
188             return self._extract_from_id(video_id, webpage)
189
190         self.to_screen(
191             'Downloading channel %s - add --no-playlist to just download video %s' % (
192                 channel_id, video_name))
193         matches = re.findall(
194             r'<a[^>]+href=[\'"](%s[a-z]+/[0-9a-z-]+/[0-9a-z]+)' % self._URL_BASE_RE,
195             webpage)
196         entries = [
197             self.url_result(video_link, OnetIE.ie_key())
198             for video_link in matches]
199
200         channel_title = strip_or_none(get_element_by_class('o_channelName', webpage))
201         channel_description = strip_or_none(get_element_by_class('o_channelDesc', webpage))
202         return self.playlist_result(entries, channel_id, channel_title, channel_description)
203
204
205 class OnetPlIE(InfoExtractor):
206     _VALID_URL = r'https?://(?:[^/]+\.)?(?:onet|businessinsider\.com|plejada)\.pl/(?:[^/]+/)+(?P<id>[0-9a-z]+)'
207     IE_NAME = 'onet.pl'
208
209     _TESTS = [{
210         'url': 'http://eurosport.onet.pl/zimowe/skoki-narciarskie/ziobro-wygral-kwalifikacje-w-pjongczangu/9ckrly',
211         'md5': 'b94021eb56214c3969380388b6e73cb0',
212         'info_dict': {
213             'id': '1561707.1685479',
214             'ext': 'mp4',
215             'title': 'Ziobro wygrał kwalifikacje w Pjongczangu',
216             'description': 'md5:61fb0740084d2d702ea96512a03585b4',
217             'upload_date': '20170214',
218             'timestamp': 1487078046,
219         },
220     }, {
221         # embedded via pulsembed
222         'url': 'http://film.onet.pl/pensjonat-nad-rozlewiskiem-relacja-z-planu-serialu/y428n0',
223         'info_dict': {
224             'id': '501235.965429946',
225             'ext': 'mp4',
226             'title': '"Pensjonat nad rozlewiskiem": relacja z planu serialu',
227             'upload_date': '20170622',
228             'timestamp': 1498159955,
229         },
230         'params': {
231             'skip_download': True,
232         },
233     }, {
234         'url': 'http://film.onet.pl/zwiastuny/ghost-in-the-shell-drugi-zwiastun-pl/5q6yl3',
235         'only_matching': True,
236     }, {
237         'url': 'http://moto.onet.pl/jak-wybierane-sa-miejsca-na-fotoradary/6rs04e',
238         'only_matching': True,
239     }, {
240         'url': 'http://businessinsider.com.pl/wideo/scenariusz-na-koniec-swiata-wedlug-nasa/dwnqptk',
241         'only_matching': True,
242     }, {
243         'url': 'http://plejada.pl/weronika-rosati-o-swoim-domniemanym-slubie/n2bq89',
244         'only_matching': True,
245     }]
246
247     def _search_mvp_id(self, webpage, default=NO_DEFAULT):
248         return self._search_regex(
249             r'data-(?:params-)?mvp=["\'](\d+\.\d+)', webpage, 'mvp id',
250             default=default)
251
252     def _real_extract(self, url):
253         video_id = self._match_id(url)
254
255         webpage = self._download_webpage(url, video_id)
256
257         mvp_id = self._search_mvp_id(webpage, default=None)
258
259         if not mvp_id:
260             pulsembed_url = self._search_regex(
261                 r'data-src=(["\'])(?P<url>(?:https?:)?//pulsembed\.eu/.+?)\1',
262                 webpage, 'pulsembed url', group='url')
263             webpage = self._download_webpage(
264                 pulsembed_url, video_id, 'Downloading pulsembed webpage')
265             mvp_id = self._search_mvp_id(webpage)
266
267         return self.url_result(
268             'onetmvp:%s' % mvp_id, OnetMVPIE.ie_key(), video_id=mvp_id)