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