]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/nova.py
Fix 'postprocessor_hooks`
[yt-dlp.git] / yt_dlp / extractor / nova.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 clean_html,
9 determine_ext,
10 int_or_none,
11 js_to_json,
12 qualities,
13 traverse_obj,
14 unified_strdate,
15 url_or_none,
16 )
17
18
19 class NovaEmbedIE(InfoExtractor):
20 _VALID_URL = r'https?://media\.cms\.nova\.cz/embed/(?P<id>[^/?#&]+)'
21 _TESTS = [{
22 'url': 'https://media.cms.nova.cz/embed/8o0n0r?autoplay=1',
23 'info_dict': {
24 'id': '8o0n0r',
25 'title': '2180. díl',
26 'thumbnail': r're:^https?://.*\.jpg',
27 'duration': 2578,
28 },
29 'params': {
30 'skip_download': True,
31 'ignore_no_formats_error': True,
32 },
33 'expected_warnings': ['DRM protected', 'Requested format is not available'],
34 }, {
35 'url': 'https://media.cms.nova.cz/embed/KybpWYvcgOa',
36 'info_dict': {
37 'id': 'KybpWYvcgOa',
38 'ext': 'mp4',
39 'title': 'Borhyová oslavila 60? Soutěžící z pořadu odboural moderátora Ondřeje Sokola',
40 'thumbnail': r're:^https?://.*\.jpg',
41 'duration': 114,
42 },
43 'params': {'skip_download': 'm3u8'},
44 }]
45
46 def _real_extract(self, url):
47 video_id = self._match_id(url)
48
49 webpage = self._download_webpage(url, video_id)
50
51 has_drm = False
52 duration = None
53 formats = []
54
55 player = self._parse_json(
56 self._search_regex(
57 r'Player\.init\s*\([^,]+,(?P<cndn>\s*\w+\s*\?)?\s*(?P<json>{(?(cndn).+?|.+)})\s*(?(cndn):|,\s*{.+?}\s*\)\s*;)',
58 webpage, 'player', default='{}', group='json'), video_id, fatal=False)
59 if player:
60 for format_id, format_list in player['tracks'].items():
61 if not isinstance(format_list, list):
62 format_list = [format_list]
63 for format_dict in format_list:
64 if not isinstance(format_dict, dict):
65 continue
66 if (not self.get_param('allow_unplayable_formats')
67 and traverse_obj(format_dict, ('drm', 'keySystem'))):
68 has_drm = True
69 continue
70 format_url = url_or_none(format_dict.get('src'))
71 format_type = format_dict.get('type')
72 ext = determine_ext(format_url)
73 if (format_type == 'application/x-mpegURL'
74 or format_id == 'HLS' or ext == 'm3u8'):
75 formats.extend(self._extract_m3u8_formats(
76 format_url, video_id, 'mp4',
77 entry_protocol='m3u8_native', m3u8_id='hls',
78 fatal=False))
79 elif (format_type == 'application/dash+xml'
80 or format_id == 'DASH' or ext == 'mpd'):
81 formats.extend(self._extract_mpd_formats(
82 format_url, video_id, mpd_id='dash', fatal=False))
83 else:
84 formats.append({
85 'url': format_url,
86 })
87 duration = int_or_none(player.get('duration'))
88 else:
89 # Old path, not actual as of 08.04.2020
90 bitrates = self._parse_json(
91 self._search_regex(
92 r'(?s)(?:src|bitrates)\s*=\s*({.+?})\s*;', webpage, 'formats'),
93 video_id, transform_source=js_to_json)
94
95 QUALITIES = ('lq', 'mq', 'hq', 'hd')
96 quality_key = qualities(QUALITIES)
97
98 for format_id, format_list in bitrates.items():
99 if not isinstance(format_list, list):
100 format_list = [format_list]
101 for format_url in format_list:
102 format_url = url_or_none(format_url)
103 if not format_url:
104 continue
105 if format_id == 'hls':
106 formats.extend(self._extract_m3u8_formats(
107 format_url, video_id, ext='mp4',
108 entry_protocol='m3u8_native', m3u8_id='hls',
109 fatal=False))
110 continue
111 f = {
112 'url': format_url,
113 }
114 f_id = format_id
115 for quality in QUALITIES:
116 if '%s.mp4' % quality in format_url:
117 f_id += '-%s' % quality
118 f.update({
119 'quality': quality_key(quality),
120 'format_note': quality.upper(),
121 })
122 break
123 f['format_id'] = f_id
124 formats.append(f)
125
126 if not formats and has_drm:
127 self.report_drm(video_id)
128 self._sort_formats(formats)
129
130 title = self._og_search_title(
131 webpage, default=None) or self._search_regex(
132 (r'<value>(?P<title>[^<]+)',
133 r'videoTitle\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1'), webpage,
134 'title', group='value')
135 thumbnail = self._og_search_thumbnail(
136 webpage, default=None) or self._search_regex(
137 r'poster\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1', webpage,
138 'thumbnail', fatal=False, group='value')
139 duration = int_or_none(self._search_regex(
140 r'videoDuration\s*:\s*(\d+)', webpage, 'duration',
141 default=duration))
142
143 return {
144 'id': video_id,
145 'title': title,
146 'thumbnail': thumbnail,
147 'duration': duration,
148 'formats': formats,
149 }
150
151
152 class NovaIE(InfoExtractor):
153 IE_DESC = 'TN.cz, Prásk.tv, Nova.cz, Novaplus.cz, FANDA.tv, Krásná.cz and Doma.cz'
154 _VALID_URL = r'https?://(?:[^.]+\.)?(?P<site>tv(?:noviny)?|tn|novaplus|vymena|fanda|krasna|doma|prask)\.nova\.cz/(?:[^/]+/)+(?P<id>[^/]+?)(?:\.html|/|$)'
155 _TESTS = [{
156 'url': 'http://tn.nova.cz/clanek/tajemstvi-ukryte-v-podzemi-specialni-nemocnice-v-prazske-krci.html#player_13260',
157 'md5': '249baab7d0104e186e78b0899c7d5f28',
158 'info_dict': {
159 'id': '1757139',
160 'display_id': 'tajemstvi-ukryte-v-podzemi-specialni-nemocnice-v-prazske-krci',
161 'ext': 'mp4',
162 'title': 'Podzemní nemocnice v pražské Krči',
163 'description': 'md5:f0a42dd239c26f61c28f19e62d20ef53',
164 'thumbnail': r're:^https?://.*\.(?:jpg)',
165 }
166 }, {
167 'url': 'http://fanda.nova.cz/clanek/fun-and-games/krvavy-epos-zaklinac-3-divoky-hon-vychazi-vyhrajte-ho-pro-sebe.html',
168 'info_dict': {
169 'id': '1753621',
170 'ext': 'mp4',
171 'title': 'Zaklínač 3: Divoký hon',
172 'description': 're:.*Pokud se stejně jako my nemůžete.*',
173 'thumbnail': r're:https?://.*\.jpg(\?.*)?',
174 'upload_date': '20150521',
175 },
176 'params': {
177 # rtmp download
178 'skip_download': True,
179 },
180 'skip': 'gone',
181 }, {
182 # media.cms.nova.cz embed
183 'url': 'https://novaplus.nova.cz/porad/ulice/epizoda/18760-2180-dil',
184 'info_dict': {
185 'id': '8o0n0r',
186 'ext': 'mp4',
187 'title': '2180. díl',
188 'thumbnail': r're:^https?://.*\.jpg',
189 'duration': 2578,
190 },
191 'params': {
192 'skip_download': True,
193 },
194 'add_ie': [NovaEmbedIE.ie_key()],
195 'skip': 'CHYBA 404: STRÁNKA NENALEZENA',
196 }, {
197 'url': 'http://sport.tn.nova.cz/clanek/sport/hokej/nhl/zivot-jde-dal-hodnotil-po-vyrazeni-z-playoff-jiri-sekac.html',
198 'only_matching': True,
199 }, {
200 'url': 'http://fanda.nova.cz/clanek/fun-and-games/krvavy-epos-zaklinac-3-divoky-hon-vychazi-vyhrajte-ho-pro-sebe.html',
201 'only_matching': True,
202 }, {
203 'url': 'http://doma.nova.cz/clanek/zdravi/prijdte-se-zapsat-do-registru-kostni-drene-jiz-ve-stredu-3-cervna.html',
204 'only_matching': True,
205 }, {
206 'url': 'http://prask.nova.cz/clanek/novinky/co-si-na-sobe-nase-hvezdy-nechaly-pojistit.html',
207 'only_matching': True,
208 }, {
209 'url': 'http://tv.nova.cz/clanek/novinky/zivot-je-zivot-bondovsky-trailer.html',
210 'only_matching': True,
211 }]
212
213 def _real_extract(self, url):
214 mobj = self._match_valid_url(url)
215 display_id = mobj.group('id')
216 site = mobj.group('site')
217
218 webpage = self._download_webpage(url, display_id)
219
220 description = clean_html(self._og_search_description(webpage, default=None))
221 if site == 'novaplus':
222 upload_date = unified_strdate(self._search_regex(
223 r'(\d{1,2}-\d{1,2}-\d{4})$', display_id, 'upload date', default=None))
224 elif site == 'fanda':
225 upload_date = unified_strdate(self._search_regex(
226 r'<span class="date_time">(\d{1,2}\.\d{1,2}\.\d{4})', webpage, 'upload date', default=None))
227 else:
228 upload_date = None
229
230 # novaplus
231 embed_id = self._search_regex(
232 r'<iframe[^>]+\bsrc=["\'](?:https?:)?//media\.cms\.nova\.cz/embed/([^/?#&]+)',
233 webpage, 'embed url', default=None)
234 if embed_id:
235 return {
236 '_type': 'url_transparent',
237 'url': 'https://media.cms.nova.cz/embed/%s' % embed_id,
238 'ie_key': NovaEmbedIE.ie_key(),
239 'id': embed_id,
240 'description': description,
241 'upload_date': upload_date
242 }
243
244 video_id = self._search_regex(
245 [r"(?:media|video_id)\s*:\s*'(\d+)'",
246 r'media=(\d+)',
247 r'id="article_video_(\d+)"',
248 r'id="player_(\d+)"'],
249 webpage, 'video id')
250
251 config_url = self._search_regex(
252 r'src="(https?://(?:tn|api)\.nova\.cz/bin/player/videojs/config\.php\?[^"]+)"',
253 webpage, 'config url', default=None)
254 config_params = {}
255
256 if not config_url:
257 player = self._parse_json(
258 self._search_regex(
259 r'(?s)Player\s*\(.+?\s*,\s*({.+?\bmedia\b["\']?\s*:\s*["\']?\d+.+?})\s*\)', webpage,
260 'player', default='{}'),
261 video_id, transform_source=js_to_json, fatal=False)
262 if player:
263 config_url = url_or_none(player.get('configUrl'))
264 params = player.get('configParams')
265 if isinstance(params, dict):
266 config_params = params
267
268 if not config_url:
269 DEFAULT_SITE_ID = '23000'
270 SITES = {
271 'tvnoviny': DEFAULT_SITE_ID,
272 'novaplus': DEFAULT_SITE_ID,
273 'vymena': DEFAULT_SITE_ID,
274 'krasna': DEFAULT_SITE_ID,
275 'fanda': '30',
276 'tn': '30',
277 'doma': '30',
278 }
279
280 site_id = self._search_regex(
281 r'site=(\d+)', webpage, 'site id', default=None) or SITES.get(
282 site, DEFAULT_SITE_ID)
283
284 config_url = 'https://api.nova.cz/bin/player/videojs/config.php'
285 config_params = {
286 'site': site_id,
287 'media': video_id,
288 'quality': 3,
289 'version': 1,
290 }
291
292 config = self._download_json(
293 config_url, display_id,
294 'Downloading config JSON', query=config_params,
295 transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1])
296
297 mediafile = config['mediafile']
298 video_url = mediafile['src']
299
300 m = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>[^/]+?))/&*(?P<playpath>.+)$', video_url)
301 if m:
302 formats = [{
303 'url': m.group('url'),
304 'app': m.group('app'),
305 'play_path': m.group('playpath'),
306 'player_path': 'http://tvnoviny.nova.cz/static/shared/app/videojs/video-js.swf',
307 'ext': 'flv',
308 }]
309 else:
310 formats = [{
311 'url': video_url,
312 }]
313 self._sort_formats(formats)
314
315 title = mediafile.get('meta', {}).get('title') or self._og_search_title(webpage)
316 thumbnail = config.get('poster')
317
318 return {
319 'id': video_id,
320 'display_id': display_id,
321 'title': title,
322 'description': description,
323 'upload_date': upload_date,
324 'thumbnail': thumbnail,
325 'formats': formats,
326 }