]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/condenast.py
[condenast] fix video info regex
[yt-dlp.git] / youtube_dl / extractor / condenast.py
CommitLineData
e1fb2456 1# coding: utf-8
6e249060 2from __future__ import unicode_literals
e1fb2456
JMF
3
4import re
e1fb2456
JMF
5
6from .common import InfoExtractor
1cc79574 7from ..compat import (
e1fb2456 8 compat_urllib_parse,
e1fb2456
JMF
9 compat_urllib_parse_urlparse,
10 compat_urlparse,
11)
1cc79574
PH
12from ..utils import (
13 orderedSet,
14)
e1fb2456
JMF
15
16
17class 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.
6e249060 25 _SITES = {
882fc905 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',
6e249060 37 'gq': 'GQ',
882fc905 38 'newyorker': 'The New Yorker',
39 'self': 'SELF',
40 'teenvogue': 'Teen Vogue',
41 'vanityfair': 'Vanity Fair',
6e249060 42 'vogue': 'Vogue',
882fc905 43 'wired': 'WIRED',
6e249060 44 'wmagazine': 'W Magazine',
6e249060 45 }
e1fb2456 46
fa35cdad 47 _VALID_URL = r'http://(video|www|player)\.(?P<site>%s)\.com/(?P<type>watch|series|video|embed)/(?P<id>[^/?#]+)' % '|'.join(_SITES.keys())
6e249060 48 IE_DESC = 'Condé Nast media group: %s' % ', '.join(sorted(_SITES.values()))
e1fb2456 49
1419fafd
S
50 EMBED_URL = r'(?:https?:)?//player\.(?P<site>%s)\.com/(?P<type>embed)/.+?' % '|'.join(_SITES.keys())
51
e1fb2456 52 _TEST = {
6e249060 53 'url': 'http://video.wired.com/watch/3d-printed-speakers-lit-with-led',
6e249060
PH
54 'md5': '1921f713ed48aabd715691f774c451f7',
55 'info_dict': {
fa35cdad
PH
56 'id': '5171b343c2b4c00dd0c1ccb3',
57 'ext': 'mp4',
6e249060
PH
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.',
e1fb2456
JMF
60 }
61 }
62
63 def _extract_series(self, url, webpage):
64 title = self._html_search_regex(r'<div class="cne-series-info">.*?<h1>(.+?)</h1>',
6e249060 65 webpage, 'series title', flags=re.DOTALL)
e1fb2456
JMF
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
fa35cdad
PH
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
e1fb2456 85 params = self._search_regex(r'var params = {(.+?)}[;,]', webpage,
6e249060
PH
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')
e1fb2456
JMF
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 = [\'"](.+?)[\'"][,;]',
6e249060 95 webpage, 'base info url',
e1fb2456
JMF
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,
6e249060 99 'Downloading video info')
2949a6cd 100 video_info = self._search_regex(r'var\s+video\s*=\s*({.+?});', info_page, 'video info')
882fc905 101 video_info = self._parse_json(video_info, video_id)
e1fb2456 102
d0da491e
PH
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)
e1fb2456 110
d0da491e
PH
111 return {
112 'id': video_id,
113 'formats': formats,
114 'title': video_info['title'],
115 'thumbnail': video_info['poster_frame'],
116 'description': description,
117 }
e1fb2456
JMF
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')
fa35cdad 123 item_id = mobj.group('id')
e1fb2456 124
fa35cdad
PH
125 self.to_screen('Extracting from %s with the Condé Nast extractor' % self._SITES[site])
126 webpage = self._download_webpage(url, item_id)
e1fb2456
JMF
127
128 if url_type == 'series':
129 return self._extract_series(url, webpage)
130 else:
fa35cdad 131 return self._extract_video(webpage, url_type)