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