1 from __future__ import unicode_literals
5 from .common import InfoExtractor
15 class WistiaBaseIE(InfoExtractor):
16 _VALID_ID_REGEX = r'(?P<id>[a-z0-9]{10})'
17 _VALID_URL_BASE = r'https?://(?:fast\.)?wistia\.(?:net|com)/embed/'
18 _EMBED_BASE_URL = 'http://fast.wistia.com/embed/'
20 def _download_embed_config(self, config_type, config_id, referer):
21 base_url = self._EMBED_BASE_URL + '%ss/%s' % (config_type, config_id)
22 embed_config = self._download_json(
23 base_url + '.json', config_id, headers={
24 'Referer': referer if referer.startswith('http') else base_url, # Some videos require this.
27 if isinstance(embed_config, dict) and embed_config.get('error'):
29 'Error while getting the playlist', expected=True)
33 def _extract_media(self, embed_config):
34 data = embed_config['media']
35 video_id = data['hashedId']
40 for a in data['assets']:
44 astatus = a.get('status')
46 if (astatus is not None and astatus != 2) or atype in ('preview', 'storyboard'):
48 elif atype in ('still', 'still_image'):
51 'width': int_or_none(a.get('width')),
52 'height': int_or_none(a.get('height')),
53 'filesize': int_or_none(a.get('size')),
57 display_name = a.get('display_name')
59 if atype and atype.endswith('_video') and display_name:
60 format_id = '%s-%s' % (atype[:-6], display_name)
62 'format_id': format_id,
64 'tbr': int_or_none(a.get('bitrate')) or None,
65 'preference': 1 if atype == 'original' else None,
67 if display_name == 'Audio':
73 'width': int_or_none(a.get('width')),
74 'height': int_or_none(a.get('height')),
75 'vcodec': a.get('codec'),
77 if a.get('container') == 'm3u8' or aext == 'm3u8':
81 'format_id': f['format_id'].replace('hls-', 'ts-'),
82 'url': f['url'].replace('.bin', '.ts'),
87 'protocol': 'm3u8_native',
91 'container': a.get('container'),
93 'filesize': int_or_none(a.get('size')),
97 self._sort_formats(formats)
100 for caption in data.get('captions', []):
101 language = caption.get('language')
104 subtitles[language] = [{
105 'url': self._EMBED_BASE_URL + 'captions/' + video_id + '.vtt?language=' + language,
111 'description': data.get('seoDescription'),
113 'thumbnails': thumbnails,
114 'duration': float_or_none(data.get('duration')),
115 'timestamp': int_or_none(data.get('createdAt')),
116 'subtitles': subtitles,
120 class WistiaIE(WistiaBaseIE):
121 _VALID_URL = r'(?:wistia:|%s(?:iframe|medias)/)%s' % (WistiaBaseIE._VALID_URL_BASE, WistiaBaseIE._VALID_ID_REGEX)
125 'url': 'wistia:807fafadvk',
126 'md5': 'daff0f3687a41d9a71b40e0e8c2610fe',
130 'title': 'Drip Brennan Dunn Workshop',
131 'description': 'a JV Webinars video',
132 'upload_date': '20160518',
133 'timestamp': 1463607249,
137 'url': 'wistia:sh7fpupwlt',
138 'only_matching': True,
140 'url': 'http://fast.wistia.net/embed/iframe/sh7fpupwlt',
141 'only_matching': True,
143 'url': 'http://fast.wistia.com/embed/iframe/sh7fpupwlt',
144 'only_matching': True,
146 'url': 'http://fast.wistia.net/embed/medias/sh7fpupwlt.json',
147 'only_matching': True,
150 # https://wistia.com/support/embed-and-share/video-on-your-website
152 def _extract_url(webpage):
153 urls = WistiaIE._extract_urls(webpage)
154 return urls[0] if urls else None
157 def _extract_urls(webpage):
159 for match in re.finditer(
160 r'<(?:meta[^>]+?content|(?:iframe|script)[^>]+?src)=["\'](?P<url>(?:https?:)?//(?:fast\.)?wistia\.(?:net|com)/embed/(?:iframe|medias)/[a-z0-9]{10})', webpage):
161 urls.append(unescapeHTML(match.group('url')))
162 for match in re.finditer(
164 <div[^>]+class=(["'])(?:(?!\1).)*?\bwistia_async_(?P<id>[a-z0-9]{10})\b(?:(?!\1).)*?\1
166 urls.append('wistia:%s' % match.group('id'))
167 for match in re.finditer(r'(?:data-wistia-?id=["\']|Wistia\.embed\(["\']|id=["\']wistia_)(?P<id>[a-z0-9]{10})', webpage):
168 urls.append('wistia:%s' % match.group('id'))
171 def _real_extract(self, url):
172 video_id = self._match_id(url)
173 embed_config = self._download_embed_config('media', video_id, url)
174 return self._extract_media(embed_config)
177 class WistiaPlaylistIE(WistiaBaseIE):
178 _VALID_URL = r'%splaylists/%s' % (WistiaIE._VALID_URL_BASE, WistiaIE._VALID_ID_REGEX)
181 'url': 'https://fast.wistia.net/embed/playlists/aodt9etokc',
188 def _real_extract(self, url):
189 playlist_id = self._match_id(url)
190 playlist = self._download_embed_config('playlist', playlist_id, url)
193 for media in (try_get(playlist, lambda x: x[0]['medias']) or []):
194 embed_config = media.get('embed_config')
197 entries.append(self._extract_media(embed_config))
199 return self.playlist_result(entries, playlist_id)