]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/nova.py
[ie/ERRJupiter] Add extractor (#8549)
[yt-dlp.git] / yt_dlp / extractor / nova.py
1 import re
2
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,
12 )
13
14
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,
24 },
25 'params': {
26 'skip_download': True,
27 'ignore_no_formats_error': True,
28 },
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,
38 },
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,
48 },
49 'params': {'skip_download': 'm3u8'},
50 }]
51
52 def _real_extract(self, url):
53 video_id = self._match_id(url)
54
55 webpage = self._download_webpage(url, video_id)
56
57 has_drm = False
58 duration = None
59 formats = []
60
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,
88 })
89
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'))
107
108 if not formats and has_drm:
109 self.report_drm(video_id)
110
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))
123
124 return {
125 'id': video_id,
126 'title': title,
127 'thumbnail': thumbnail,
128 'duration': duration,
129 'formats': formats,
130 }
131
132
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': '249baab7d0104e186e78b0899c7d5f28',
139 'info_dict': {
140 'id': '1757139',
141 'display_id': 'tajemstvi-ukryte-v-podzemi-specialni-nemocnice-v-prazske-krci',
142 'ext': 'mp4',
143 'title': 'Podzemní nemocnice v pražské Krči',
144 'description': 'md5:f0a42dd239c26f61c28f19e62d20ef53',
145 'thumbnail': r're:^https?://.*\.(?:jpg)',
146 }
147 }, {
148 'url': 'http://fanda.nova.cz/clanek/fun-and-games/krvavy-epos-zaklinac-3-divoky-hon-vychazi-vyhrajte-ho-pro-sebe.html',
149 'info_dict': {
150 'id': '1753621',
151 'ext': 'mp4',
152 'title': 'Zaklínač 3: Divoký hon',
153 'description': 're:.*Pokud se stejně jako my nemůžete.*',
154 'thumbnail': r're:https?://.*\.jpg(\?.*)?',
155 'upload_date': '20150521',
156 },
157 'params': {
158 # rtmp download
159 'skip_download': True,
160 },
161 'skip': 'gone',
162 }, {
163 # media.cms.nova.cz embed
164 'url': 'https://novaplus.nova.cz/porad/ulice/epizoda/18760-2180-dil',
165 'info_dict': {
166 'id': '8o0n0r',
167 'ext': 'mp4',
168 'title': '2180. díl',
169 'thumbnail': r're:^https?://.*\.jpg',
170 'duration': 2578,
171 },
172 'params': {
173 'skip_download': True,
174 },
175 'add_ie': [NovaEmbedIE.ie_key()],
176 'skip': 'CHYBA 404: STRÁNKA NENALEZENA',
177 }, {
178 'url': 'http://sport.tn.nova.cz/clanek/sport/hokej/nhl/zivot-jde-dal-hodnotil-po-vyrazeni-z-playoff-jiri-sekac.html',
179 'only_matching': True,
180 }, {
181 'url': 'http://fanda.nova.cz/clanek/fun-and-games/krvavy-epos-zaklinac-3-divoky-hon-vychazi-vyhrajte-ho-pro-sebe.html',
182 'only_matching': True,
183 }, {
184 'url': 'http://doma.nova.cz/clanek/zdravi/prijdte-se-zapsat-do-registru-kostni-drene-jiz-ve-stredu-3-cervna.html',
185 'only_matching': True,
186 }, {
187 'url': 'http://prask.nova.cz/clanek/novinky/co-si-na-sobe-nase-hvezdy-nechaly-pojistit.html',
188 'only_matching': True,
189 }, {
190 'url': 'http://tv.nova.cz/clanek/novinky/zivot-je-zivot-bondovsky-trailer.html',
191 'only_matching': True,
192 }]
193
194 def _real_extract(self, url):
195 mobj = self._match_valid_url(url)
196 display_id = mobj.group('id')
197 site = mobj.group('site')
198
199 webpage = self._download_webpage(url, display_id)
200
201 description = clean_html(self._og_search_description(webpage, default=None))
202 if site == 'novaplus':
203 upload_date = unified_strdate(self._search_regex(
204 r'(\d{1,2}-\d{1,2}-\d{4})$', display_id, 'upload date', default=None))
205 elif site == 'fanda':
206 upload_date = unified_strdate(self._search_regex(
207 r'<span class="date_time">(\d{1,2}\.\d{1,2}\.\d{4})', webpage, 'upload date', default=None))
208 else:
209 upload_date = None
210
211 # novaplus
212 embed_id = self._search_regex(
213 r'<iframe[^>]+\bsrc=["\'](?:https?:)?//media\.cms\.nova\.cz/embed/([^/?#&]+)',
214 webpage, 'embed url', default=None)
215 if embed_id:
216 return {
217 '_type': 'url_transparent',
218 'url': 'https://media.cms.nova.cz/embed/%s' % embed_id,
219 'ie_key': NovaEmbedIE.ie_key(),
220 'id': embed_id,
221 'description': description,
222 'upload_date': upload_date
223 }
224
225 video_id = self._search_regex(
226 [r"(?:media|video_id)\s*:\s*'(\d+)'",
227 r'media=(\d+)',
228 r'id="article_video_(\d+)"',
229 r'id="player_(\d+)"'],
230 webpage, 'video id')
231
232 config_url = self._search_regex(
233 r'src="(https?://(?:tn|api)\.nova\.cz/bin/player/videojs/config\.php\?[^"]+)"',
234 webpage, 'config url', default=None)
235 config_params = {}
236
237 if not config_url:
238 player = self._parse_json(
239 self._search_regex(
240 r'(?s)Player\s*\(.+?\s*,\s*({.+?\bmedia\b["\']?\s*:\s*["\']?\d+.+?})\s*\)', webpage,
241 'player', default='{}'),
242 video_id, transform_source=js_to_json, fatal=False)
243 if player:
244 config_url = url_or_none(player.get('configUrl'))
245 params = player.get('configParams')
246 if isinstance(params, dict):
247 config_params = params
248
249 if not config_url:
250 DEFAULT_SITE_ID = '23000'
251 SITES = {
252 'tvnoviny': DEFAULT_SITE_ID,
253 'novaplus': DEFAULT_SITE_ID,
254 'vymena': DEFAULT_SITE_ID,
255 'krasna': DEFAULT_SITE_ID,
256 'fanda': '30',
257 'tn': '30',
258 'doma': '30',
259 }
260
261 site_id = self._search_regex(
262 r'site=(\d+)', webpage, 'site id', default=None) or SITES.get(
263 site, DEFAULT_SITE_ID)
264
265 config_url = 'https://api.nova.cz/bin/player/videojs/config.php'
266 config_params = {
267 'site': site_id,
268 'media': video_id,
269 'quality': 3,
270 'version': 1,
271 }
272
273 config = self._download_json(
274 config_url, display_id,
275 'Downloading config JSON', query=config_params,
276 transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1])
277
278 mediafile = config['mediafile']
279 video_url = mediafile['src']
280
281 m = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>[^/]+?))/&*(?P<playpath>.+)$', video_url)
282 if m:
283 formats = [{
284 'url': m.group('url'),
285 'app': m.group('app'),
286 'play_path': m.group('playpath'),
287 'player_path': 'http://tvnoviny.nova.cz/static/shared/app/videojs/video-js.swf',
288 'ext': 'flv',
289 }]
290 else:
291 formats = [{
292 'url': video_url,
293 }]
294
295 title = mediafile.get('meta', {}).get('title') or self._og_search_title(webpage)
296 thumbnail = config.get('poster')
297
298 return {
299 'id': video_id,
300 'display_id': display_id,
301 'title': title,
302 'description': description,
303 'upload_date': upload_date,
304 'thumbnail': thumbnail,
305 'formats': formats,
306 }