]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/wistia.py
Revert "pull changes from remote master (#190)" (#193)
[yt-dlp.git] / youtube_dl / extractor / wistia.py
CommitLineData
e423e0ba
S
1from __future__ import unicode_literals
2
58bb4402
S
3import re
4
ef4fd848 5from .common import InfoExtractor
5c2266df
S
6from ..utils import (
7 ExtractorError,
cf45ed78 8 int_or_none,
7ded6545 9 float_or_none,
58bb4402 10 unescapeHTML,
5c2266df 11)
ef4fd848
PH
12
13
14class WistiaIE(InfoExtractor):
152f2292 15 _VALID_URL = r'(?:wistia:|https?://(?:fast\.)?wistia\.(?:net|com)/embed/(?:iframe|medias)/)(?P<id>[a-z0-9]{10})'
19a107f2
AG
16 _API_URL = 'http://fast.wistia.com/embed/medias/%s.json'
17 _IFRAME_URL = 'http://fast.wistia.net/embed/iframe/%s'
ef4fd848 18
f0c96af9 19 _TESTS = [{
e423e0ba
S
20 'url': 'http://fast.wistia.net/embed/iframe/sh7fpupwlt',
21 'md5': 'cafeb56ec0c53c18c97405eecb3133df',
22 'info_dict': {
23 'id': 'sh7fpupwlt',
24 'ext': 'mov',
25 'title': 'Being Resourceful',
cf45ed78 26 'description': 'a Clients From Hell Video Series video from worldwidewebhosting',
27 'upload_date': '20131204',
28 'timestamp': 1386185018,
e423e0ba 29 'duration': 117,
ef4fd848 30 },
f0c96af9
S
31 }, {
32 'url': 'wistia:sh7fpupwlt',
33 'only_matching': True,
45f160a4
S
34 }, {
35 # with hls video
36 'url': 'wistia:807fafadvk',
37 'only_matching': True,
641e86e3
S
38 }, {
39 'url': 'http://fast.wistia.com/embed/iframe/sh7fpupwlt',
40 'only_matching': True,
ed8db0a2
S
41 }, {
42 'url': 'http://fast.wistia.net/embed/medias/sh7fpupwlt.json',
43 'only_matching': True,
f0c96af9 44 }]
ef4fd848 45
152f2292 46 # https://wistia.com/support/embed-and-share/video-on-your-website
58bb4402
S
47 @staticmethod
48 def _extract_url(webpage):
19a107f2
AG
49 match = re.search(
50 r'<(?:meta[^>]+?content|(?:iframe|script)[^>]+?src)=["\'](?P<url>(?:https?:)?//(?:fast\.)?wistia\.(?:net|com)/embed/(?:iframe|medias)/[a-z0-9]{10})', webpage)
51 if match:
52 return unescapeHTML(match.group('url'))
58bb4402 53
19a107f2
AG
54 match = re.search(
55 r'''(?sx)
56 <script[^>]+src=(["'])(?:https?:)?//fast\.wistia\.com/assets/external/E-v1\.js\1[^>]*>.*?
57 <div[^>]+class=(["']).*?\bwistia_async_(?P<id>[a-z0-9]{10})\b.*?\2
58 ''', webpage)
59 if match:
60 return 'wistia:%s' % match.group('id')
61
62 match = re.search(r'(?:data-wistia-?id=["\']|Wistia\.embed\(["\']|id=["\']wistia_)(?P<id>[a-z0-9]{10})', webpage)
63 if match:
64 return 'wistia:%s' % match.group('id')
152f2292 65
ef4fd848 66 def _real_extract(self, url):
1cc79574 67 video_id = self._match_id(url)
ef4fd848 68
f0c96af9 69 data_json = self._download_json(
19a107f2 70 self._API_URL % video_id, video_id,
f0c96af9
S
71 # Some videos require this.
72 headers={
19a107f2 73 'Referer': url if url.startswith('http') else self._IFRAME_URL % video_id,
f0c96af9
S
74 })
75
746c67d7 76 if data_json.get('error'):
f0c96af9
S
77 raise ExtractorError(
78 'Error while getting the playlist', expected=True)
79
746c67d7 80 data = data_json['media']
cf45ed78 81 title = data['name']
ef4fd848
PH
82
83 formats = []
84 thumbnails = []
66ca2cfd 85 for a in data['assets']:
36ca2c55
S
86 aurl = a.get('url')
87 if not aurl:
88 continue
cf45ed78 89 astatus = a.get('status')
66ca2cfd 90 atype = a.get('type')
36ca2c55 91 if (astatus is not None and astatus != 2) or atype in ('preview', 'storyboard'):
cf45ed78 92 continue
93 elif atype in ('still', 'still_image'):
ef4fd848 94 thumbnails.append({
36ca2c55
S
95 'url': aurl,
96 'width': int_or_none(a.get('width')),
97 'height': int_or_none(a.get('height')),
ef4fd848 98 })
cf45ed78 99 else:
45f160a4 100 aext = a.get('ext')
19a107f2
AG
101 is_m3u8 = a.get('container') == 'm3u8' or aext == 'm3u8'
102 formats.append({
103 'format_id': atype,
36ca2c55 104 'url': aurl,
19a107f2
AG
105 'tbr': int_or_none(a.get('bitrate')),
106 'vbr': int_or_none(a.get('opt_vbitrate')),
107 'width': int_or_none(a.get('width')),
108 'height': int_or_none(a.get('height')),
109 'filesize': int_or_none(a.get('size')),
110 'vcodec': a.get('codec'),
111 'container': a.get('container'),
112 'ext': 'mp4' if is_m3u8 else aext,
113 'protocol': 'm3u8' if is_m3u8 else None,
cf45ed78 114 'preference': 1 if atype == 'original' else None,
19a107f2 115 })
539179f4
PH
116
117 self._sort_formats(formats)
ef4fd848
PH
118
119 return {
120 'id': video_id,
cf45ed78 121 'title': title,
122 'description': data.get('seoDescription'),
ef4fd848
PH
123 'formats': formats,
124 'thumbnails': thumbnails,
7ded6545 125 'duration': float_or_none(data.get('duration')),
cf45ed78 126 'timestamp': int_or_none(data.get('createdAt')),
ef4fd848 127 }