]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/wistia.py
[extractor/wistia] Add support for channels (#4819)
[yt-dlp.git] / yt_dlp / extractor / wistia.py
1 import re
2 import urllib.error
3 import urllib.parse
4 from base64 import b64decode
5
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 float_or_none,
10 int_or_none,
11 parse_qs,
12 traverse_obj,
13 try_get,
14 update_url_query,
15 )
16
17
18 class WistiaBaseIE(InfoExtractor):
19 _VALID_ID_REGEX = r'(?P<id>[a-z0-9]{10})'
20 _VALID_URL_BASE = r'https?://(?:\w+\.)?wistia\.(?:net|com)/(?:embed/)?'
21 _EMBED_BASE_URL = 'http://fast.wistia.net/embed/'
22
23 def _download_embed_config(self, config_type, config_id, referer):
24 base_url = self._EMBED_BASE_URL + '%s/%s' % (config_type, config_id)
25 embed_config = self._download_json(
26 base_url + '.json', config_id, headers={
27 'Referer': referer if referer.startswith('http') else base_url, # Some videos require this.
28 })
29
30 error = traverse_obj(embed_config, 'error')
31 if error:
32 raise ExtractorError(
33 f'Error while getting the playlist: {error}', expected=True)
34
35 return embed_config
36
37 def _extract_media(self, embed_config):
38 data = embed_config['media']
39 video_id = data['hashedId']
40 title = data['name']
41
42 formats = []
43 thumbnails = []
44 for a in data['assets']:
45 aurl = a.get('url')
46 if not aurl:
47 continue
48 astatus = a.get('status')
49 atype = a.get('type')
50 if (astatus is not None and astatus != 2) or atype in ('preview', 'storyboard'):
51 continue
52 elif atype in ('still', 'still_image'):
53 thumbnails.append({
54 'url': aurl,
55 'width': int_or_none(a.get('width')),
56 'height': int_or_none(a.get('height')),
57 'filesize': int_or_none(a.get('size')),
58 })
59 else:
60 aext = a.get('ext')
61 display_name = a.get('display_name')
62 format_id = atype
63 if atype and atype.endswith('_video') and display_name:
64 format_id = '%s-%s' % (atype[:-6], display_name)
65 f = {
66 'format_id': format_id,
67 'url': aurl,
68 'tbr': int_or_none(a.get('bitrate')) or None,
69 'quality': 1 if atype == 'original' else None,
70 }
71 if display_name == 'Audio':
72 f.update({
73 'vcodec': 'none',
74 })
75 else:
76 f.update({
77 'width': int_or_none(a.get('width')),
78 'height': int_or_none(a.get('height')),
79 'vcodec': a.get('codec'),
80 })
81 if a.get('container') == 'm3u8' or aext == 'm3u8':
82 ts_f = f.copy()
83 ts_f.update({
84 'ext': 'ts',
85 'format_id': f['format_id'].replace('hls-', 'ts-'),
86 'url': f['url'].replace('.bin', '.ts'),
87 })
88 formats.append(ts_f)
89 f.update({
90 'ext': 'mp4',
91 'protocol': 'm3u8_native',
92 })
93 else:
94 f.update({
95 'container': a.get('container'),
96 'ext': aext,
97 'filesize': int_or_none(a.get('size')),
98 })
99 formats.append(f)
100
101 self._sort_formats(formats)
102
103 subtitles = {}
104 for caption in data.get('captions', []):
105 language = caption.get('language')
106 if not language:
107 continue
108 subtitles[language] = [{
109 'url': self._EMBED_BASE_URL + 'captions/' + video_id + '.vtt?language=' + language,
110 }]
111
112 return {
113 'id': video_id,
114 'title': title,
115 'description': data.get('seoDescription'),
116 'formats': formats,
117 'thumbnails': thumbnails,
118 'duration': float_or_none(data.get('duration')),
119 'timestamp': int_or_none(data.get('createdAt')),
120 'subtitles': subtitles,
121 }
122
123 @classmethod
124 def _extract_from_webpage(cls, url, webpage):
125 from .teachable import TeachableIE
126
127 if list(TeachableIE._extract_embed_urls(url, webpage)):
128 return
129
130 yield from super()._extract_from_webpage(url, webpage)
131
132 @classmethod
133 def _extract_wistia_async_embed(cls, webpage):
134 # https://wistia.com/support/embed-and-share/video-on-your-website
135 # https://wistia.com/support/embed-and-share/channel-embeds
136 yield from re.finditer(
137 r'''(?sx)
138 <(?:div|section)[^>]+class=([\"'])(?:(?!\1).)*?(?P<type>wistia[a-z_0-9]+)\s*\bwistia_async_(?P<id>[a-z0-9]{10})\b(?:(?!\1).)*?\1
139 ''', webpage)
140
141 @classmethod
142 def _extract_url_media_id(cls, url):
143 mobj = re.search(r'(?:wmediaid|wvideo(?:id)?)]?=(?P<id>[a-z0-9]{10})', urllib.parse.unquote_plus(url))
144 if mobj:
145 return mobj.group('id')
146
147
148 class WistiaIE(WistiaBaseIE):
149 _VALID_URL = r'(?:wistia:|%s(?:iframe|medias)/)%s' % (WistiaBaseIE._VALID_URL_BASE, WistiaBaseIE._VALID_ID_REGEX)
150 _EMBED_REGEX = [
151 r'''(?x)
152 <(?:meta[^>]+?content|(?:iframe|script)[^>]+?src)=["\']
153 (?P<url>(?:https?:)?//(?:fast\.)?wistia\.(?:net|com)/embed/(?:iframe|medias)/[a-z0-9]{10})
154 ''']
155 _TESTS = [{
156 # with hls video
157 'url': 'wistia:807fafadvk',
158 'md5': 'daff0f3687a41d9a71b40e0e8c2610fe',
159 'info_dict': {
160 'id': '807fafadvk',
161 'ext': 'mp4',
162 'title': 'Drip Brennan Dunn Workshop',
163 'description': 'a JV Webinars video',
164 'upload_date': '20160518',
165 'timestamp': 1463607249,
166 'duration': 4987.11,
167 },
168 'skip': 'video unavailable',
169 }, {
170 'url': 'wistia:a6ndpko1wg',
171 'md5': '10c1ce9c4dde638202513ed17a3767bd',
172 'info_dict': {
173 'id': 'a6ndpko1wg',
174 'ext': 'bin',
175 'title': 'Episode 2: Boxed Water\'s retention is thirsty',
176 'upload_date': '20210324',
177 'description': 'md5:da5994c2c2d254833b412469d9666b7a',
178 'duration': 966.0,
179 'timestamp': 1616614369,
180 'thumbnail': 'https://embed-ssl.wistia.com/deliveries/53dc60239348dc9b9fba3755173ea4c2.bin',
181 }
182 }, {
183 'url': 'wistia:5vd7p4bct5',
184 'md5': 'b9676d24bf30945d97060638fbfe77f0',
185 'info_dict': {
186 'id': '5vd7p4bct5',
187 'ext': 'bin',
188 'title': 'md5:eaa9f64c4efd7b5f098b9b6118597679',
189 'description': 'md5:a9bea0315f0616aa5df2dc413ddcdd0f',
190 'upload_date': '20220915',
191 'timestamp': 1663258727,
192 'duration': 623.019,
193 'thumbnail': r're:https?://embed(?:-ssl)?.wistia.com/.+\.(?:jpg|bin)$',
194 },
195 }, {
196 'url': 'wistia:sh7fpupwlt',
197 'only_matching': True,
198 }, {
199 'url': 'http://fast.wistia.net/embed/iframe/sh7fpupwlt',
200 'only_matching': True,
201 }, {
202 'url': 'http://fast.wistia.com/embed/iframe/sh7fpupwlt',
203 'only_matching': True,
204 }, {
205 'url': 'http://fast.wistia.net/embed/medias/sh7fpupwlt.json',
206 'only_matching': True,
207 }]
208
209 _WEBPAGE_TESTS = [{
210 'url': 'https://www.weidert.com/blog/wistia-channels-video-marketing-tool',
211 'info_dict': {
212 'id': 'cqwukac3z1',
213 'ext': 'bin',
214 'title': 'How Wistia Channels Can Help Capture Inbound Value From Your Video Content',
215 'duration': 158.125,
216 'timestamp': 1618974400,
217 'description': 'md5:27abc99a758573560be72600ef95cece',
218 'upload_date': '20210421',
219 'thumbnail': 'https://embed-ssl.wistia.com/deliveries/6c551820ae950cdee2306d6cbe9ef742.bin',
220 }
221 }, {
222 'url': 'https://study.com/academy/lesson/north-american-exploration-failed-colonies-of-spain-france-england.html#lesson',
223 'md5': 'b9676d24bf30945d97060638fbfe77f0',
224 'info_dict': {
225 'id': '5vd7p4bct5',
226 'ext': 'bin',
227 'title': 'paywall_north-american-exploration-failed-colonies-of-spain-france-england',
228 'upload_date': '20220915',
229 'timestamp': 1663258727,
230 'duration': 623.019,
231 'thumbnail': 'https://embed-ssl.wistia.com/deliveries/83e6ec693e2c05a0ce65809cbaead86a.bin',
232 'description': 'a Paywall Videos video',
233 },
234 }]
235
236 def _real_extract(self, url):
237 video_id = self._match_id(url)
238 embed_config = self._download_embed_config('medias', video_id, url)
239 return self._extract_media(embed_config)
240
241 @classmethod
242 def _extract_embed_urls(cls, url, webpage):
243 urls = list(super()._extract_embed_urls(url, webpage))
244 for match in cls._extract_wistia_async_embed(webpage):
245 if match.group('type') != 'wistia_channel':
246 urls.append('wistia:%s' % match.group('id'))
247 for match in re.finditer(r'(?:data-wistia-?id=["\']|Wistia\.embed\(["\']|id=["\']wistia_)(?P<id>[a-z0-9]{10})',
248 webpage):
249 urls.append('wistia:%s' % match.group('id'))
250 if not WistiaChannelIE._extract_embed_urls(url, webpage): # Fallback
251 media_id = cls._extract_url_media_id(url)
252 if media_id:
253 urls.append('wistia:%s' % match.group('id'))
254 return urls
255
256
257 class WistiaPlaylistIE(WistiaBaseIE):
258 _VALID_URL = r'%splaylists/%s' % (WistiaBaseIE._VALID_URL_BASE, WistiaBaseIE._VALID_ID_REGEX)
259
260 _TEST = {
261 'url': 'https://fast.wistia.net/embed/playlists/aodt9etokc',
262 'info_dict': {
263 'id': 'aodt9etokc',
264 },
265 'playlist_count': 3,
266 }
267
268 def _real_extract(self, url):
269 playlist_id = self._match_id(url)
270 playlist = self._download_embed_config('playlists', playlist_id, url)
271
272 entries = []
273 for media in (try_get(playlist, lambda x: x[0]['medias']) or []):
274 embed_config = media.get('embed_config')
275 if not embed_config:
276 continue
277 entries.append(self._extract_media(embed_config))
278
279 return self.playlist_result(entries, playlist_id)
280
281
282 class WistiaChannelIE(WistiaBaseIE):
283 _VALID_URL = r'(?:wistiachannel:|%schannel/)%s' % (WistiaBaseIE._VALID_URL_BASE, WistiaBaseIE._VALID_ID_REGEX)
284
285 _TESTS = [{
286 # JSON Embed API returns 403, should fall back to webpage
287 'url': 'https://fast.wistia.net/embed/channel/yvyvu7wjbg?wchannelid=yvyvu7wjbg',
288 'info_dict': {
289 'id': 'yvyvu7wjbg',
290 'title': 'Copysmith Tutorials and Education!',
291 'description': 'Learn all things Copysmith via short and informative videos!'
292 },
293 'playlist_mincount': 7,
294 'expected_warnings': ['falling back to webpage'],
295 }, {
296 'url': 'https://fast.wistia.net/embed/channel/3802iirk0l',
297 'info_dict': {
298 'id': '3802iirk0l',
299 'title': 'The Roof',
300 },
301 'playlist_mincount': 20,
302 }, {
303 # link to popup video, follow --no-playlist
304 'url': 'https://fast.wistia.net/embed/channel/3802iirk0l?wchannelid=3802iirk0l&wmediaid=sp5dqjzw3n',
305 'info_dict': {
306 'id': 'sp5dqjzw3n',
307 'ext': 'bin',
308 'title': 'The Roof S2: The Modern CRO',
309 'thumbnail': 'https://embed-ssl.wistia.com/deliveries/dadfa9233eaa505d5e0c85c23ff70741.bin',
310 'duration': 86.487,
311 'description': 'A sales leader on The Roof? Man, they really must be letting anyone up here this season.\n',
312 'timestamp': 1619790290,
313 'upload_date': '20210430',
314 },
315 'params': {'noplaylist': True, 'skip_download': True},
316 }]
317 _WEBPAGE_TESTS = [{
318 'url': 'https://www.profitwell.com/recur/boxed-out',
319 'info_dict': {
320 'id': '6jyvmqz6zs',
321 'title': 'Boxed Out',
322 'description': 'md5:14a8a93a1dbe236718e6a59f8c8c7bae',
323 },
324 'playlist_mincount': 30,
325 }, {
326 # section instead of div
327 'url': 'https://360learning.com/studio/onboarding-joei/',
328 'info_dict': {
329 'id': 'z874k93n2o',
330 'title': 'Onboarding Joei.',
331 'description': 'Coming to you weekly starting Feb 19th.',
332 },
333 'playlist_mincount': 20,
334 }, {
335 'url': 'https://amplitude.com/amplify-sessions?amp%5Bwmediaid%5D=pz0m0l0if3&amp%5Bwvideo%5D=pz0m0l0if3&wchannelid=emyjmwjf79&wmediaid=i8um783bdt',
336 'info_dict': {
337 'id': 'pz0m0l0if3',
338 'title': 'A Framework for Improving Product Team Performance',
339 'ext': 'bin',
340 'timestamp': 1653935275,
341 'upload_date': '20220530',
342 'description': 'Learn how to help your company improve and achieve your product related goals.',
343 'duration': 1854.39,
344 'thumbnail': 'https://embed-ssl.wistia.com/deliveries/12fd19e56413d9d6f04e2185c16a6f8854e25226.bin',
345 },
346 'params': {'noplaylist': True, 'skip_download': True},
347 }]
348
349 def _real_extract(self, url):
350 channel_id = self._match_id(url)
351 media_id = self._extract_url_media_id(url)
352 if not self._yes_playlist(channel_id, media_id, playlist_label='channel'):
353 return self.url_result(f'wistia:{media_id}', 'Wistia')
354
355 try:
356 data = self._download_embed_config('channel', channel_id, url)
357 except (ExtractorError, urllib.error.HTTPError):
358 # Some channels give a 403 from the JSON API
359 self.report_warning('Failed to download channel data from API, falling back to webpage.')
360 webpage = self._download_webpage(f'https://fast.wistia.net/embed/channel/{channel_id}', channel_id)
361 data = self._parse_json(
362 self._search_regex(r'wchanneljsonp-%s\'\]\s*=[^\"]*\"([A-Za-z0-9=/]*)' % channel_id, webpage, 'jsonp', channel_id),
363 channel_id, transform_source=lambda x: urllib.parse.unquote_plus(b64decode(x).decode('utf-8')))
364
365 # XXX: can there be more than one series?
366 series = traverse_obj(data, ('series', 0), default={})
367
368 entries = [
369 self.url_result(f'wistia:{video["hashedId"]}', WistiaIE, title=video.get('name'))
370 for video in traverse_obj(series, ('sections', ..., 'videos', ...)) or []
371 if video.get('hashedId')
372 ]
373
374 return self.playlist_result(
375 entries, channel_id, playlist_title=series.get('title'), playlist_description=series.get('description'))
376
377 @classmethod
378 def _extract_embed_urls(cls, url, webpage):
379 yield from super()._extract_embed_urls(url, webpage)
380 for match in cls._extract_wistia_async_embed(webpage):
381 if match.group('type') == 'wistia_channel':
382 # original url may contain wmediaid query param
383 yield update_url_query(f'wistiachannel:{match.group("id")}', parse_qs(url))