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