[condenast] Use unicode_literals
[youtube-dl] / youtube_dl / extractor / condenast.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import json
6
7 from .common import InfoExtractor
8 from ..utils import (
9     compat_urllib_parse,
10     orderedSet,
11     compat_urllib_parse_urlparse,
12     compat_urlparse,
13 )
14
15
16 class CondeNastIE(InfoExtractor):
17     """
18     Condé Nast is a media group, some of its sites use a custom HTML5 player
19     that works the same in all of them.
20     """
21
22     # The keys are the supported sites and the values are the name to be shown
23     # to the user and in the extractor description.
24     _SITES = {
25         'wired': 'WIRED',
26         'gq': 'GQ',
27         'vogue': 'Vogue',
28         'glamour': 'Glamour',
29         'wmagazine': 'W Magazine',
30         'vanityfair': 'Vanity Fair',
31     }
32
33     _VALID_URL = r'http://(video|www).(?P<site>%s).com/(?P<type>watch|series|video)/(?P<id>.+)' % '|'.join(_SITES.keys())
34     IE_DESC = 'Condé Nast media group: %s' % ', '.join(sorted(_SITES.values()))
35
36     _TEST = {
37         'url': 'http://video.wired.com/watch/3d-printed-speakers-lit-with-led',
38         'file': '5171b343c2b4c00dd0c1ccb3.mp4',
39         'md5': '1921f713ed48aabd715691f774c451f7',
40         'info_dict': {
41             'title': '3D Printed Speakers Lit With LED',
42             '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.',
43         }
44     }
45
46     def _extract_series(self, url, webpage):
47         title = self._html_search_regex(r'<div class="cne-series-info">.*?<h1>(.+?)</h1>',
48                                         webpage, 'series title', flags=re.DOTALL)
49         url_object = compat_urllib_parse_urlparse(url)
50         base_url = '%s://%s' % (url_object.scheme, url_object.netloc)
51         m_paths = re.finditer(r'<p class="cne-thumb-title">.*?<a href="(/watch/.+?)["\?]',
52                               webpage, flags=re.DOTALL)
53         paths = orderedSet(m.group(1) for m in m_paths)
54         build_url = lambda path: compat_urlparse.urljoin(base_url, path)
55         entries = [self.url_result(build_url(path), 'CondeNast') for path in paths]
56         return self.playlist_result(entries, playlist_title=title)
57
58     def _extract_video(self, webpage):
59         description = self._html_search_regex([r'<div class="cne-video-description">(.+?)</div>',
60                                                r'<div class="video-post-content">(.+?)</div>',
61                                                ],
62                                               webpage, 'description',
63                                               fatal=False, flags=re.DOTALL)
64         params = self._search_regex(r'var params = {(.+?)}[;,]', webpage,
65                                     'player params', flags=re.DOTALL)
66         video_id = self._search_regex(r'videoId: [\'"](.+?)[\'"]', params, 'video id')
67         player_id = self._search_regex(r'playerId: [\'"](.+?)[\'"]', params, 'player id')
68         target = self._search_regex(r'target: [\'"](.+?)[\'"]', params, 'target')
69         data = compat_urllib_parse.urlencode({'videoId': video_id,
70                                               'playerId': player_id,
71                                               'target': target,
72                                               })
73         base_info_url = self._search_regex(r'url = [\'"](.+?)[\'"][,;]',
74                                            webpage, 'base info url',
75                                            default='http://player.cnevids.com/player/loader.js?')
76         info_url = base_info_url + data
77         info_page = self._download_webpage(info_url, video_id,
78                                            'Downloading video info')
79         video_info = self._search_regex(r'var video = ({.+?});', info_page, 'video info')
80         video_info = json.loads(video_info)
81
82         def _formats_sort_key(f):
83             type_ord = 1 if f['type'] == 'video/mp4' else 0
84             quality_ord = 1 if f['quality'] == 'high' else 0
85             return (quality_ord, type_ord)
86         best_format = sorted(video_info['sources'][0], key=_formats_sort_key)[-1]
87
88         return {'id': video_id,
89                 'url': best_format['src'],
90                 'ext': best_format['type'].split('/')[-1],
91                 'title': video_info['title'],
92                 'thumbnail': video_info['poster_frame'],
93                 'description': description,
94                 }
95
96     def _real_extract(self, url):
97         mobj = re.match(self._VALID_URL, url)
98         site = mobj.group('site')
99         url_type = mobj.group('type')
100         id = mobj.group('id')
101
102         self.to_screen(u'Extracting from %s with the Condé Nast extractor' % self._SITES[site])
103         webpage = self._download_webpage(url, id)
104
105         if url_type == 'series':
106             return self._extract_series(url, webpage)
107         else:
108             return self._extract_video(webpage)