]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/wistia.py
New upstream version 2017.10.15.1
[youtubedl] / youtube_dl / extractor / wistia.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7 ExtractorError,
8 int_or_none,
9 float_or_none,
10 unescapeHTML,
11 )
12
13
14 class WistiaIE(InfoExtractor):
15 _VALID_URL = r'(?:wistia:|https?://(?:fast\.)?wistia\.net/embed/iframe/)(?P<id>[a-z0-9]+)'
16 _API_URL = 'http://fast.wistia.com/embed/medias/%s.json'
17 _IFRAME_URL = 'http://fast.wistia.net/embed/iframe/%s'
18
19 _TESTS = [{
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',
26 'description': 'a Clients From Hell Video Series video from worldwidewebhosting',
27 'upload_date': '20131204',
28 'timestamp': 1386185018,
29 'duration': 117,
30 },
31 }, {
32 'url': 'wistia:sh7fpupwlt',
33 'only_matching': True,
34 }, {
35 # with hls video
36 'url': 'wistia:807fafadvk',
37 'only_matching': True,
38 }]
39
40 @staticmethod
41 def _extract_url(webpage):
42 match = re.search(
43 r'<(?:meta[^>]+?content|iframe[^>]+?src)=(["\'])(?P<url>(?:https?:)?//(?:fast\.)?wistia\.net/embed/iframe/.+?)\1', webpage)
44 if match:
45 return unescapeHTML(match.group('url'))
46
47 match = re.search(r'(?:id=["\']wistia_|data-wistia-?id=["\']|Wistia\.embed\(["\'])(?P<id>[^"\']+)', webpage)
48 if match:
49 return 'wistia:%s' % match.group('id')
50
51 match = re.search(
52 r'''(?sx)
53 <script[^>]+src=(["'])(?:https?:)?//fast\.wistia\.com/assets/external/E-v1\.js\1[^>]*>.*?
54 <div[^>]+class=(["']).*?\bwistia_async_(?P<id>[a-z0-9]+)\b.*?\2
55 ''', webpage)
56 if match:
57 return 'wistia:%s' % match.group('id')
58
59 def _real_extract(self, url):
60 video_id = self._match_id(url)
61
62 data_json = self._download_json(
63 self._API_URL % video_id, video_id,
64 # Some videos require this.
65 headers={
66 'Referer': url if url.startswith('http') else self._IFRAME_URL % video_id,
67 })
68
69 if data_json.get('error'):
70 raise ExtractorError(
71 'Error while getting the playlist', expected=True)
72
73 data = data_json['media']
74 title = data['name']
75
76 formats = []
77 thumbnails = []
78 for a in data['assets']:
79 aurl = a.get('url')
80 if not aurl:
81 continue
82 astatus = a.get('status')
83 atype = a.get('type')
84 if (astatus is not None and astatus != 2) or atype in ('preview', 'storyboard'):
85 continue
86 elif atype in ('still', 'still_image'):
87 thumbnails.append({
88 'url': aurl,
89 'width': int_or_none(a.get('width')),
90 'height': int_or_none(a.get('height')),
91 })
92 else:
93 aext = a.get('ext')
94 is_m3u8 = a.get('container') == 'm3u8' or aext == 'm3u8'
95 formats.append({
96 'format_id': atype,
97 'url': aurl,
98 'tbr': int_or_none(a.get('bitrate')),
99 'vbr': int_or_none(a.get('opt_vbitrate')),
100 'width': int_or_none(a.get('width')),
101 'height': int_or_none(a.get('height')),
102 'filesize': int_or_none(a.get('size')),
103 'vcodec': a.get('codec'),
104 'container': a.get('container'),
105 'ext': 'mp4' if is_m3u8 else aext,
106 'protocol': 'm3u8' if is_m3u8 else None,
107 'preference': 1 if atype == 'original' else None,
108 })
109
110 self._sort_formats(formats)
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 }