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