]>
Commit | Line | Data |
---|---|---|
e423e0ba S |
1 | from __future__ import unicode_literals |
2 | ||
58bb4402 S |
3 | import re |
4 | ||
ef4fd848 | 5 | from .common import InfoExtractor |
5c2266df S |
6 | from ..utils import ( |
7 | ExtractorError, | |
cf45ed78 | 8 | int_or_none, |
7ded6545 | 9 | float_or_none, |
58bb4402 | 10 | unescapeHTML, |
5c2266df | 11 | ) |
ef4fd848 PH |
12 | |
13 | ||
14 | class WistiaIE(InfoExtractor): | |
ed8db0a2 | 15 | _VALID_URL = r'(?:wistia:|https?://(?:fast\.)?wistia\.(?:net|com)/embed/(?:iframe|medias)/)(?P<id>[a-z0-9]+)' |
f0c96af9 S |
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 | |
58bb4402 S |
46 | @staticmethod |
47 | def _extract_url(webpage): | |
48 | match = re.search( | |
641e86e3 | 49 | r'<(?:meta[^>]+?content|iframe[^>]+?src)=(["\'])(?P<url>(?:https?:)?//(?:fast\.)?wistia\.(?:net|com)/embed/iframe/.+?)\1', webpage) |
58bb4402 S |
50 | if match: |
51 | return unescapeHTML(match.group('url')) | |
52 | ||
53 | match = re.search(r'(?:id=["\']wistia_|data-wistia-?id=["\']|Wistia\.embed\(["\'])(?P<id>[^"\']+)', webpage) | |
54 | if match: | |
55 | return 'wistia:%s' % match.group('id') | |
56 | ||
57 | match = re.search( | |
58 | r'''(?sx) | |
59 | <script[^>]+src=(["'])(?:https?:)?//fast\.wistia\.com/assets/external/E-v1\.js\1[^>]*>.*? | |
60 | <div[^>]+class=(["']).*?\bwistia_async_(?P<id>[a-z0-9]+)\b.*?\2 | |
61 | ''', webpage) | |
62 | if match: | |
63 | return 'wistia:%s' % match.group('id') | |
64 | ||
ef4fd848 | 65 | def _real_extract(self, url): |
1cc79574 | 66 | video_id = self._match_id(url) |
ef4fd848 | 67 | |
f0c96af9 S |
68 | data_json = self._download_json( |
69 | self._API_URL % video_id, video_id, | |
70 | # Some videos require this. | |
71 | headers={ | |
72 | 'Referer': url if url.startswith('http') else self._IFRAME_URL % video_id, | |
73 | }) | |
74 | ||
746c67d7 | 75 | if data_json.get('error'): |
f0c96af9 S |
76 | raise ExtractorError( |
77 | 'Error while getting the playlist', expected=True) | |
78 | ||
746c67d7 | 79 | data = data_json['media'] |
cf45ed78 | 80 | title = data['name'] |
ef4fd848 PH |
81 | |
82 | formats = [] | |
83 | thumbnails = [] | |
66ca2cfd | 84 | for a in data['assets']: |
36ca2c55 S |
85 | aurl = a.get('url') |
86 | if not aurl: | |
87 | continue | |
cf45ed78 | 88 | astatus = a.get('status') |
66ca2cfd | 89 | atype = a.get('type') |
36ca2c55 | 90 | if (astatus is not None and astatus != 2) or atype in ('preview', 'storyboard'): |
cf45ed78 | 91 | continue |
92 | elif atype in ('still', 'still_image'): | |
ef4fd848 | 93 | thumbnails.append({ |
36ca2c55 S |
94 | 'url': aurl, |
95 | 'width': int_or_none(a.get('width')), | |
96 | 'height': int_or_none(a.get('height')), | |
ef4fd848 | 97 | }) |
cf45ed78 | 98 | else: |
45f160a4 S |
99 | aext = a.get('ext') |
100 | is_m3u8 = a.get('container') == 'm3u8' or aext == 'm3u8' | |
cf45ed78 | 101 | formats.append({ |
102 | 'format_id': atype, | |
36ca2c55 | 103 | 'url': aurl, |
cf45ed78 | 104 | 'tbr': int_or_none(a.get('bitrate')), |
105 | 'vbr': int_or_none(a.get('opt_vbitrate')), | |
106 | 'width': int_or_none(a.get('width')), | |
107 | 'height': int_or_none(a.get('height')), | |
108 | 'filesize': int_or_none(a.get('size')), | |
109 | 'vcodec': a.get('codec'), | |
110 | 'container': a.get('container'), | |
45f160a4 S |
111 | 'ext': 'mp4' if is_m3u8 else aext, |
112 | 'protocol': 'm3u8' if is_m3u8 else None, | |
cf45ed78 | 113 | 'preference': 1 if atype == 'original' else None, |
114 | }) | |
539179f4 PH |
115 | |
116 | self._sort_formats(formats) | |
ef4fd848 PH |
117 | |
118 | return { | |
119 | 'id': video_id, | |
cf45ed78 | 120 | 'title': title, |
121 | 'description': data.get('seoDescription'), | |
ef4fd848 PH |
122 | 'formats': formats, |
123 | 'thumbnails': thumbnails, | |
7ded6545 | 124 | 'duration': float_or_none(data.get('duration')), |
cf45ed78 | 125 | 'timestamp': int_or_none(data.get('createdAt')), |
ef4fd848 | 126 | } |