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