[condenast] fix extraction and add support for other sites
[youtube-dl] / 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,
9     compat_urllib_parse_urlparse,
10     compat_urlparse,
11 )
12 from ..utils import (
13     orderedSet,
14 )
15
16
17 class CondeNastIE(InfoExtractor):
18     """
19     Condé Nast is a media group, some of its sites use a custom HTML5 player
20     that works the same in all of them.
21     """
22
23     # The keys are the supported sites and the values are the name to be shown
24     # to the user and in the extractor description.
25     _SITES = {
26         'allure': 'Allure',
27         'architecturaldigest': 'Architectural Digest',
28         'arstechnica': 'Ars Technica',
29         'bonappetit': 'Bon Appetit',
30         'brides': 'Brides',
31         'cnevids': 'Condé Nast',
32         'cntraveler': 'Condé Nast Traveler',
33         'details': 'Details',
34         'epicurious': 'Epicurious',
35         'glamour': 'Glamour',
36         'golfdigest': 'Golf Digest',
37         'gq': 'GQ',
38         'newyorker': 'The New Yorker',
39         'self': 'SELF',
40         'teenvogue': 'Teen Vogue',
41         'vanityfair': 'Vanity Fair',
42         'vogue': 'Vogue',
43         'wired': 'WIRED',
44         'wmagazine': 'W Magazine',
45     }
46
47     _VALID_URL = r'http://(video|www|player)\.(?P<site>%s)\.com/(?P<type>watch|series|video|embed)/(?P<id>[^/?#]+)' % '|'.join(_SITES.keys())
48     IE_DESC = 'Condé Nast media group: %s' % ', '.join(sorted(_SITES.values()))
49
50     EMBED_URL = r'(?:https?:)?//player\.(?P<site>%s)\.com/(?P<type>embed)/.+?' % '|'.join(_SITES.keys())
51
52     _TEST = {
53         'url': 'http://video.wired.com/watch/3d-printed-speakers-lit-with-led',
54         'md5': '1921f713ed48aabd715691f774c451f7',
55         'info_dict': {
56             'id': '5171b343c2b4c00dd0c1ccb3',
57             'ext': 'mp4',
58             'title': '3D Printed Speakers Lit With LED',
59             '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.',
60         }
61     }
62
63     def _extract_series(self, url, webpage):
64         title = self._html_search_regex(r'<div class="cne-series-info">.*?<h1>(.+?)</h1>',
65                                         webpage, 'series title', flags=re.DOTALL)
66         url_object = compat_urllib_parse_urlparse(url)
67         base_url = '%s://%s' % (url_object.scheme, url_object.netloc)
68         m_paths = re.finditer(r'<p class="cne-thumb-title">.*?<a href="(/watch/.+?)["\?]',
69                               webpage, flags=re.DOTALL)
70         paths = orderedSet(m.group(1) for m in m_paths)
71         build_url = lambda path: compat_urlparse.urljoin(base_url, path)
72         entries = [self.url_result(build_url(path), 'CondeNast') for path in paths]
73         return self.playlist_result(entries, playlist_title=title)
74
75     def _extract_video(self, webpage, url_type):
76         if url_type != 'embed':
77             description = self._html_search_regex(
78                 [
79                     r'<div class="cne-video-description">(.+?)</div>',
80                     r'<div class="video-post-content">(.+?)</div>',
81                 ],
82                 webpage, 'description', fatal=False, flags=re.DOTALL)
83         else:
84             description = None
85         params = self._search_regex(r'var params = {(.+?)}[;,]', webpage,
86                                     'player params', flags=re.DOTALL)
87         video_id = self._search_regex(r'videoId: [\'"](.+?)[\'"]', params, 'video id')
88         player_id = self._search_regex(r'playerId: [\'"](.+?)[\'"]', params, 'player id')
89         target = self._search_regex(r'target: [\'"](.+?)[\'"]', params, 'target')
90         data = compat_urllib_parse.urlencode({'videoId': video_id,
91                                               'playerId': player_id,
92                                               'target': target,
93                                               })
94         base_info_url = self._search_regex(r'url = [\'"](.+?)[\'"][,;]',
95                                            webpage, 'base info url',
96                                            default='http://player.cnevids.com/player/loader.js?')
97         info_url = base_info_url + data
98         info_page = self._download_webpage(info_url, video_id,
99                                            'Downloading video info')
100         video_info = self._search_regex(r'var\s*video\s*=\s*({.+?});', info_page, 'video info')
101         video_info = self._parse_json(video_info, video_id)
102
103         formats = [{
104             'format_id': '%s-%s' % (fdata['type'].split('/')[-1], fdata['quality']),
105             'url': fdata['src'],
106             'ext': fdata['type'].split('/')[-1],
107             'quality': 1 if fdata['quality'] == 'high' else 0,
108         } for fdata in video_info['sources'][0]]
109         self._sort_formats(formats)
110
111         return {
112             'id': video_id,
113             'formats': formats,
114             'title': video_info['title'],
115             'thumbnail': video_info['poster_frame'],
116             'description': description,
117         }
118
119     def _real_extract(self, url):
120         mobj = re.match(self._VALID_URL, url)
121         site = mobj.group('site')
122         url_type = mobj.group('type')
123         item_id = mobj.group('id')
124
125         self.to_screen('Extracting from %s with the Condé Nast extractor' % self._SITES[site])
126         webpage = self._download_webpage(url, item_id)
127
128         if url_type == 'series':
129             return self._extract_series(url, webpage)
130         else:
131             return self._extract_video(webpage, url_type)