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