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