]>
Commit | Line | Data |
---|---|---|
e1fb2456 | 1 | # coding: utf-8 |
6e249060 | 2 | from __future__ import unicode_literals |
e1fb2456 JMF |
3 | |
4 | import re | |
e1fb2456 JMF |
5 | |
6 | from .common import InfoExtractor | |
1cc79574 | 7 | from ..compat import ( |
e1fb2456 JMF |
8 | compat_urllib_parse_urlparse, |
9 | compat_urlparse, | |
10 | ) | |
1cc79574 | 11 | from ..utils import ( |
4f427c4b | 12 | determine_ext, |
391d076d | 13 | extract_attributes, |
4f427c4b | 14 | int_or_none, |
391d076d S |
15 | js_to_json, |
16 | mimetype2ext, | |
17 | orderedSet, | |
4f427c4b | 18 | parse_iso8601, |
8bdd16b4 | 19 | strip_or_none, |
20 | try_get, | |
1cc79574 | 21 | ) |
e1fb2456 JMF |
22 | |
23 | ||
24 | class CondeNastIE(InfoExtractor): | |
25 | """ | |
26 | Condé Nast is a media group, some of its sites use a custom HTML5 player | |
27 | that works the same in all of them. | |
28 | """ | |
29 | ||
30 | # The keys are the supported sites and the values are the name to be shown | |
31 | # to the user and in the extractor description. | |
6e249060 | 32 | _SITES = { |
882fc905 | 33 | 'allure': 'Allure', |
34 | 'architecturaldigest': 'Architectural Digest', | |
35 | 'arstechnica': 'Ars Technica', | |
42ca72df | 36 | 'bonappetit': 'Bon Appétit', |
882fc905 | 37 | 'brides': 'Brides', |
38 | 'cnevids': 'Condé Nast', | |
39 | 'cntraveler': 'Condé Nast Traveler', | |
40 | 'details': 'Details', | |
41 | 'epicurious': 'Epicurious', | |
42 | 'glamour': 'Glamour', | |
43 | 'golfdigest': 'Golf Digest', | |
6e249060 | 44 | 'gq': 'GQ', |
882fc905 | 45 | 'newyorker': 'The New Yorker', |
46 | 'self': 'SELF', | |
47 | 'teenvogue': 'Teen Vogue', | |
48 | 'vanityfair': 'Vanity Fair', | |
6e249060 | 49 | 'vogue': 'Vogue', |
882fc905 | 50 | 'wired': 'WIRED', |
6e249060 | 51 | 'wmagazine': 'W Magazine', |
6e249060 | 52 | } |
e1fb2456 | 53 | |
6c52477f RA |
54 | _VALID_URL = r'''(?x)https?://(?:video|www|player(?:-backend)?)\.(?:%s)\.com/ |
55 | (?: | |
56 | (?: | |
57 | embed(?:js)?| | |
58 | (?:script|inline)/video | |
59 | )/(?P<id>[0-9a-f]{24})(?:/(?P<player_id>[0-9a-f]{24}))?(?:.+?\btarget=(?P<target>[^&]+))?| | |
60 | (?P<type>watch|series|video)/(?P<display_id>[^/?#]+) | |
61 | )''' % '|'.join(_SITES.keys()) | |
6e249060 | 62 | IE_DESC = 'Condé Nast media group: %s' % ', '.join(sorted(_SITES.values())) |
e1fb2456 | 63 | |
6c52477f | 64 | EMBED_URL = r'(?:https?:)?//player(?:-backend)?\.(?:%s)\.com/(?:embed(?:js)?|(?:script|inline)/video)/.+?' % '|'.join(_SITES.keys()) |
1419fafd | 65 | |
c6b8f4d0 | 66 | _TESTS = [{ |
6e249060 | 67 | 'url': 'http://video.wired.com/watch/3d-printed-speakers-lit-with-led', |
6e249060 PH |
68 | 'md5': '1921f713ed48aabd715691f774c451f7', |
69 | 'info_dict': { | |
fa35cdad PH |
70 | 'id': '5171b343c2b4c00dd0c1ccb3', |
71 | 'ext': 'mp4', | |
6e249060 PH |
72 | 'title': '3D Printed Speakers Lit With LED', |
73 | 'description': 'Check out these beautiful 3D printed LED speakers. You can\'t actually buy them, but LumiGeek is working on a board that will let you make you\'re own.', | |
4f427c4b RA |
74 | 'uploader': 'wired', |
75 | 'upload_date': '20130314', | |
76 | 'timestamp': 1363219200, | |
e1fb2456 | 77 | } |
391d076d S |
78 | }, { |
79 | 'url': 'http://video.gq.com/watch/the-closer-with-keith-olbermann-the-only-true-surprise-trump-s-an-idiot?c=series', | |
80 | 'info_dict': { | |
81 | 'id': '58d1865bfd2e6126e2000015', | |
82 | 'ext': 'mp4', | |
83 | 'title': 'The Only True Surprise? Trump’s an Idiot', | |
84 | 'uploader': 'gq', | |
85 | 'upload_date': '20170321', | |
86 | 'timestamp': 1490126427, | |
8bdd16b4 | 87 | 'description': 'How much grimmer would things be if these people were competent?', |
391d076d | 88 | }, |
c6b8f4d0 S |
89 | }, { |
90 | # JS embed | |
91 | 'url': 'http://player.cnevids.com/embedjs/55f9cf8b61646d1acf00000c/5511d76261646d5566020000.js', | |
92 | 'md5': 'f1a6f9cafb7083bab74a710f65d08999', | |
93 | 'info_dict': { | |
94 | 'id': '55f9cf8b61646d1acf00000c', | |
95 | 'ext': 'mp4', | |
96 | 'title': '3D printed TSA Travel Sentry keys really do open TSA locks', | |
4f427c4b RA |
97 | 'uploader': 'arstechnica', |
98 | 'upload_date': '20150916', | |
8bdd16b4 | 99 | 'timestamp': 1442434920, |
c6b8f4d0 | 100 | } |
6c52477f RA |
101 | }, { |
102 | 'url': 'https://player.cnevids.com/inline/video/59138decb57ac36b83000005.js?target=js-cne-player', | |
103 | 'only_matching': True, | |
104 | }, { | |
105 | 'url': 'http://player-backend.cnevids.com/script/video/59138decb57ac36b83000005.js', | |
106 | 'only_matching': True, | |
c6b8f4d0 | 107 | }] |
e1fb2456 JMF |
108 | |
109 | def _extract_series(self, url, webpage): | |
4f427c4b RA |
110 | title = self._html_search_regex( |
111 | r'(?s)<div class="cne-series-info">.*?<h1>(.+?)</h1>', | |
112 | webpage, 'series title') | |
e1fb2456 JMF |
113 | url_object = compat_urllib_parse_urlparse(url) |
114 | base_url = '%s://%s' % (url_object.scheme, url_object.netloc) | |
4f427c4b RA |
115 | m_paths = re.finditer( |
116 | r'(?s)<p class="cne-thumb-title">.*?<a href="(/watch/.+?)["\?]', webpage) | |
e1fb2456 JMF |
117 | paths = orderedSet(m.group(1) for m in m_paths) |
118 | build_url = lambda path: compat_urlparse.urljoin(base_url, path) | |
119 | entries = [self.url_result(build_url(path), 'CondeNast') for path in paths] | |
120 | return self.playlist_result(entries, playlist_title=title) | |
121 | ||
c46680fb S |
122 | def _extract_video_params(self, webpage, display_id): |
123 | query = self._parse_json( | |
124 | self._search_regex( | |
125 | r'(?s)var\s+params\s*=\s*({.+?})[;,]', webpage, 'player params', | |
126 | default='{}'), | |
127 | display_id, transform_source=js_to_json, fatal=False) | |
128 | if query: | |
129 | query['videoId'] = self._search_regex( | |
130 | r'(?:data-video-id=|currentVideoId\s*=\s*)["\']([\da-f]+)', | |
131 | webpage, 'video id', default=None) | |
fa35cdad | 132 | else: |
4f427c4b RA |
133 | params = extract_attributes(self._search_regex( |
134 | r'(<[^>]+data-js="video-player"[^>]+>)', | |
135 | webpage, 'player params element')) | |
136 | query.update({ | |
137 | 'videoId': params['data-video'], | |
138 | 'playerId': params['data-player'], | |
139 | 'target': params['id'], | |
140 | }) | |
6c52477f RA |
141 | return query |
142 | ||
143 | def _extract_video(self, params): | |
144 | video_id = params['videoId'] | |
145 | ||
e563c0d7 | 146 | video_info = None |
c46680fb S |
147 | |
148 | # New API path | |
149 | query = params.copy() | |
150 | query['embedType'] = 'inline' | |
151 | info_page = self._download_json( | |
152 | 'http://player.cnevids.com/embed-api.json', video_id, | |
153 | 'Downloading embed info', fatal=False, query=query) | |
154 | ||
155 | # Old fallbacks | |
156 | if not info_page: | |
157 | if params.get('playerId'): | |
158 | info_page = self._download_json( | |
159 | 'http://player.cnevids.com/player/video.js', video_id, | |
160 | 'Downloading video info', fatal=False, query=params) | |
161 | if info_page: | |
162 | video_info = info_page.get('video') | |
163 | if not video_info: | |
164 | info_page = self._download_webpage( | |
165 | 'http://player.cnevids.com/player/loader.js', | |
166 | video_id, 'Downloading loader info', query=params) | |
167 | if not video_info: | |
e563c0d7 | 168 | info_page = self._download_webpage( |
6c52477f RA |
169 | 'https://player.cnevids.com/inline/video/%s.js' % video_id, |
170 | video_id, 'Downloading inline info', query={ | |
171 | 'target': params.get('target', 'embedplayer') | |
172 | }) | |
173 | ||
174 | if not video_info: | |
391d076d S |
175 | video_info = self._parse_json( |
176 | self._search_regex( | |
177 | r'(?s)var\s+config\s*=\s*({.+?});', info_page, 'config'), | |
178 | video_id, transform_source=js_to_json)['video'] | |
179 | ||
4f427c4b RA |
180 | title = video_info['title'] |
181 | ||
182 | formats = [] | |
391d076d | 183 | for fdata in video_info['sources']: |
4f427c4b RA |
184 | src = fdata.get('src') |
185 | if not src: | |
186 | continue | |
187 | ext = mimetype2ext(fdata.get('type')) or determine_ext(src) | |
391d076d S |
188 | if ext == 'm3u8': |
189 | formats.extend(self._extract_m3u8_formats( | |
190 | src, video_id, 'mp4', entry_protocol='m3u8_native', | |
191 | m3u8_id='hls', fatal=False)) | |
192 | continue | |
4f427c4b RA |
193 | quality = fdata.get('quality') |
194 | formats.append({ | |
195 | 'format_id': ext + ('-%s' % quality if quality else ''), | |
196 | 'url': src, | |
197 | 'ext': ext, | |
198 | 'quality': 1 if quality == 'high' else 0, | |
199 | }) | |
d0da491e | 200 | self._sort_formats(formats) |
e1fb2456 | 201 | |
8bdd16b4 | 202 | subtitles = {} |
203 | for t, caption in video_info.get('captions', {}).items(): | |
204 | caption_url = caption.get('src') | |
205 | if not (t in ('vtt', 'srt', 'tml') and caption_url): | |
206 | continue | |
207 | subtitles.setdefault('en', []).append({'url': caption_url}) | |
208 | ||
6c52477f | 209 | return { |
d0da491e PH |
210 | 'id': video_id, |
211 | 'formats': formats, | |
4f427c4b RA |
212 | 'title': title, |
213 | 'thumbnail': video_info.get('poster_frame'), | |
214 | 'uploader': video_info.get('brand'), | |
215 | 'duration': int_or_none(video_info.get('duration')), | |
216 | 'tags': video_info.get('tags'), | |
217 | 'series': video_info.get('series_title'), | |
218 | 'season': video_info.get('season_title'), | |
219 | 'timestamp': parse_iso8601(video_info.get('premiere_date')), | |
6c52477f | 220 | 'categories': video_info.get('categories'), |
8bdd16b4 | 221 | 'subtitles': subtitles, |
6c52477f | 222 | } |
e1fb2456 JMF |
223 | |
224 | def _real_extract(self, url): | |
5ad28e7f | 225 | video_id, player_id, target, url_type, display_id = self._match_valid_url(url).groups() |
e1fb2456 | 226 | |
6c52477f RA |
227 | if video_id: |
228 | return self._extract_video({ | |
229 | 'videoId': video_id, | |
230 | 'playerId': player_id, | |
231 | 'target': target, | |
232 | }) | |
c6b8f4d0 | 233 | |
6c52477f | 234 | webpage = self._download_webpage(url, display_id) |
e1fb2456 JMF |
235 | |
236 | if url_type == 'series': | |
237 | return self._extract_series(url, webpage) | |
238 | else: | |
8bdd16b4 | 239 | video = try_get(self._parse_json(self._search_regex( |
240 | r'__PRELOADED_STATE__\s*=\s*({.+?});', webpage, | |
241 | 'preload state', '{}'), display_id), | |
242 | lambda x: x['transformed']['video']) | |
243 | if video: | |
244 | params = {'videoId': video['id']} | |
245 | info = {'description': strip_or_none(video.get('description'))} | |
246 | else: | |
247 | params = self._extract_video_params(webpage, display_id) | |
248 | info = self._search_json_ld( | |
249 | webpage, display_id, fatal=False) | |
6c52477f RA |
250 | info.update(self._extract_video(params)) |
251 | return info |