]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/condenast.py
d3463b8747a2109e8efef6b93d70843e542b4590
[youtubedl] / youtube_dl / extractor / condenast.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8 compat_urllib_parse_urlparse,
9 compat_urlparse,
10 )
11 from ..utils import (
12 determine_ext,
13 extract_attributes,
14 int_or_none,
15 js_to_json,
16 mimetype2ext,
17 orderedSet,
18 parse_iso8601,
19 remove_end,
20 )
21
22
23 class CondeNastIE(InfoExtractor):
24 """
25 Condé Nast is a media group, some of its sites use a custom HTML5 player
26 that works the same in all of them.
27 """
28
29 # The keys are the supported sites and the values are the name to be shown
30 # to the user and in the extractor description.
31 _SITES = {
32 'allure': 'Allure',
33 'architecturaldigest': 'Architectural Digest',
34 'arstechnica': 'Ars Technica',
35 'bonappetit': 'Bon Appétit',
36 'brides': 'Brides',
37 'cnevids': 'Condé Nast',
38 'cntraveler': 'Condé Nast Traveler',
39 'details': 'Details',
40 'epicurious': 'Epicurious',
41 'glamour': 'Glamour',
42 'golfdigest': 'Golf Digest',
43 'gq': 'GQ',
44 'newyorker': 'The New Yorker',
45 'self': 'SELF',
46 'teenvogue': 'Teen Vogue',
47 'vanityfair': 'Vanity Fair',
48 'vogue': 'Vogue',
49 'wired': 'WIRED',
50 'wmagazine': 'W Magazine',
51 }
52
53 _VALID_URL = r'https?://(?:video|www|player)\.(?P<site>%s)\.com/(?P<type>watch|series|video|embed(?:js)?)/(?P<id>[^/?#]+)' % '|'.join(_SITES.keys())
54 IE_DESC = 'Condé Nast media group: %s' % ', '.join(sorted(_SITES.values()))
55
56 EMBED_URL = r'(?:https?:)?//player\.(?P<site>%s)\.com/(?P<type>embed(?:js)?)/.+?' % '|'.join(_SITES.keys())
57
58 _TESTS = [{
59 'url': 'http://video.wired.com/watch/3d-printed-speakers-lit-with-led',
60 'md5': '1921f713ed48aabd715691f774c451f7',
61 'info_dict': {
62 'id': '5171b343c2b4c00dd0c1ccb3',
63 'ext': 'mp4',
64 'title': '3D Printed Speakers Lit With LED',
65 '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.',
66 'uploader': 'wired',
67 'upload_date': '20130314',
68 'timestamp': 1363219200,
69 }
70 }, {
71 'url': 'http://video.gq.com/watch/the-closer-with-keith-olbermann-the-only-true-surprise-trump-s-an-idiot?c=series',
72 'info_dict': {
73 'id': '58d1865bfd2e6126e2000015',
74 'ext': 'mp4',
75 'title': 'The Only True Surprise? Trump’s an Idiot',
76 'uploader': 'gq',
77 'upload_date': '20170321',
78 'timestamp': 1490126427,
79 },
80 }, {
81 # JS embed
82 'url': 'http://player.cnevids.com/embedjs/55f9cf8b61646d1acf00000c/5511d76261646d5566020000.js',
83 'md5': 'f1a6f9cafb7083bab74a710f65d08999',
84 'info_dict': {
85 'id': '55f9cf8b61646d1acf00000c',
86 'ext': 'mp4',
87 'title': '3D printed TSA Travel Sentry keys really do open TSA locks',
88 'uploader': 'arstechnica',
89 'upload_date': '20150916',
90 'timestamp': 1442434955,
91 }
92 }]
93
94 def _extract_series(self, url, webpage):
95 title = self._html_search_regex(
96 r'(?s)<div class="cne-series-info">.*?<h1>(.+?)</h1>',
97 webpage, 'series title')
98 url_object = compat_urllib_parse_urlparse(url)
99 base_url = '%s://%s' % (url_object.scheme, url_object.netloc)
100 m_paths = re.finditer(
101 r'(?s)<p class="cne-thumb-title">.*?<a href="(/watch/.+?)["\?]', webpage)
102 paths = orderedSet(m.group(1) for m in m_paths)
103 build_url = lambda path: compat_urlparse.urljoin(base_url, path)
104 entries = [self.url_result(build_url(path), 'CondeNast') for path in paths]
105 return self.playlist_result(entries, playlist_title=title)
106
107 def _extract_video(self, webpage, url_type):
108 query = {}
109 params = self._search_regex(
110 r'(?s)var params = {(.+?)}[;,]', webpage, 'player params', default=None)
111 if params:
112 query.update({
113 'videoId': self._search_regex(r'videoId: [\'"](.+?)[\'"]', params, 'video id'),
114 'playerId': self._search_regex(r'playerId: [\'"](.+?)[\'"]', params, 'player id'),
115 'target': self._search_regex(r'target: [\'"](.+?)[\'"]', params, 'target'),
116 })
117 else:
118 params = extract_attributes(self._search_regex(
119 r'(<[^>]+data-js="video-player"[^>]+>)',
120 webpage, 'player params element'))
121 query.update({
122 'videoId': params['data-video'],
123 'playerId': params['data-player'],
124 'target': params['id'],
125 })
126 video_id = query['videoId']
127 video_info = None
128 info_page = self._download_json(
129 'http://player.cnevids.com/player/video.js',
130 video_id, 'Downloading video info', fatal=False, query=query)
131 if info_page:
132 video_info = info_page.get('video')
133 if not video_info:
134 info_page = self._download_webpage(
135 'http://player.cnevids.com/player/loader.js',
136 video_id, 'Downloading loader info', query=query)
137 video_info = self._parse_json(
138 self._search_regex(
139 r'(?s)var\s+config\s*=\s*({.+?});', info_page, 'config'),
140 video_id, transform_source=js_to_json)['video']
141
142 title = video_info['title']
143
144 formats = []
145 for fdata in video_info['sources']:
146 src = fdata.get('src')
147 if not src:
148 continue
149 ext = mimetype2ext(fdata.get('type')) or determine_ext(src)
150 if ext == 'm3u8':
151 formats.extend(self._extract_m3u8_formats(
152 src, video_id, 'mp4', entry_protocol='m3u8_native',
153 m3u8_id='hls', fatal=False))
154 continue
155 quality = fdata.get('quality')
156 formats.append({
157 'format_id': ext + ('-%s' % quality if quality else ''),
158 'url': src,
159 'ext': ext,
160 'quality': 1 if quality == 'high' else 0,
161 })
162 self._sort_formats(formats)
163
164 info = self._search_json_ld(
165 webpage, video_id, fatal=False) if url_type != 'embed' else {}
166 info.update({
167 'id': video_id,
168 'formats': formats,
169 'title': title,
170 'thumbnail': video_info.get('poster_frame'),
171 'uploader': video_info.get('brand'),
172 'duration': int_or_none(video_info.get('duration')),
173 'tags': video_info.get('tags'),
174 'series': video_info.get('series_title'),
175 'season': video_info.get('season_title'),
176 'timestamp': parse_iso8601(video_info.get('premiere_date')),
177 })
178 return info
179
180 def _real_extract(self, url):
181 site, url_type, item_id = re.match(self._VALID_URL, url).groups()
182
183 # Convert JS embed to regular embed
184 if url_type == 'embedjs':
185 parsed_url = compat_urlparse.urlparse(url)
186 url = compat_urlparse.urlunparse(parsed_url._replace(
187 path=remove_end(parsed_url.path, '.js').replace('/embedjs/', '/embed/')))
188 url_type = 'embed'
189
190 webpage = self._download_webpage(url, item_id)
191
192 if url_type == 'series':
193 return self._extract_series(url, webpage)
194 else:
195 return self._extract_video(webpage, url_type)