1
0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2026-04-28 01:33:24 +00:00
Files
youtube-dl/youtube_dl/extractor/wistia.py
T

121 lines
4.1 KiB
Python
Raw Normal View History

2014-06-24 19:34:39 +07:00
from __future__ import unicode_literals
import re
2013-12-06 09:15:04 +01:00
from .common import InfoExtractor
from ..utils import (
ExtractorError,
2016-03-17 17:48:17 +01:00
int_or_none,
float_or_none,
unescapeHTML,
)
2013-12-06 09:15:04 +01:00
class WistiaIE(InfoExtractor):
2016-05-20 20:55:10 +06:00
_VALID_URL = r'(?:wistia:|https?://(?:fast\.)?wistia\.net/embed/iframe/)(?P<id>[a-z0-9]+)'
_API_URL = 'http://fast.wistia.com/embed/medias/%s.json'
_IFRAME_URL = 'http://fast.wistia.net/embed/iframe/%s'
2013-12-06 09:15:04 +01:00
2016-05-20 20:55:10 +06:00
_TESTS = [{
2014-06-24 19:34:39 +07:00
'url': 'http://fast.wistia.net/embed/iframe/sh7fpupwlt',
'md5': 'cafeb56ec0c53c18c97405eecb3133df',
'info_dict': {
'id': 'sh7fpupwlt',
'ext': 'mov',
'title': 'Being Resourceful',
2016-03-17 17:48:17 +01:00
'description': 'a Clients From Hell Video Series video from worldwidewebhosting',
'upload_date': '20131204',
'timestamp': 1386185018,
2014-06-24 19:34:39 +07:00
'duration': 117,
2013-12-06 09:15:04 +01:00
},
2016-05-20 20:55:10 +06:00
}, {
'url': 'wistia:sh7fpupwlt',
'only_matching': True,
2016-05-20 21:16:08 +06:00
}, {
# with hls video
'url': 'wistia:807fafadvk',
'only_matching': True,
2016-05-20 20:55:10 +06:00
}]
2013-12-06 09:15:04 +01:00
@staticmethod
def _extract_url(webpage):
match = re.search(
r'<(?:meta[^>]+?content|iframe[^>]+?src)=(["\'])(?P<url>(?:https?:)?//(?:fast\.)?wistia\.net/embed/iframe/.+?)\1', webpage)
if match:
return unescapeHTML(match.group('url'))
match = re.search(r'(?:id=["\']wistia_|data-wistia-?id=["\']|Wistia\.embed\(["\'])(?P<id>[^"\']+)', webpage)
if match:
return 'wistia:%s' % match.group('id')
match = re.search(
r'''(?sx)
<script[^>]+src=(["'])(?:https?:)?//fast\.wistia\.com/assets/external/E-v1\.js\1[^>]*>.*?
<div[^>]+class=(["']).*?\bwistia_async_(?P<id>[a-z0-9]+)\b.*?\2
''', webpage)
if match:
return 'wistia:%s' % match.group('id')
2013-12-06 09:15:04 +01:00
def _real_extract(self, url):
2014-12-13 12:24:42 +01:00
video_id = self._match_id(url)
2013-12-06 09:15:04 +01:00
2016-05-20 20:55:10 +06:00
data_json = self._download_json(
self._API_URL % video_id, video_id,
# Some videos require this.
headers={
'Referer': url if url.startswith('http') else self._IFRAME_URL % video_id,
})
2014-09-20 03:02:11 +03:00
if data_json.get('error'):
2016-05-20 20:55:10 +06:00
raise ExtractorError(
'Error while getting the playlist', expected=True)
2014-09-20 03:02:11 +03:00
data = data_json['media']
2016-03-17 17:48:17 +01:00
title = data['name']
2013-12-06 09:15:04 +01:00
formats = []
thumbnails = []
2016-03-01 23:26:53 +06:00
for a in data['assets']:
aurl = a.get('url')
if not aurl:
continue
2016-03-17 17:48:17 +01:00
astatus = a.get('status')
2016-03-01 23:26:53 +06:00
atype = a.get('type')
if (astatus is not None and astatus != 2) or atype in ('preview', 'storyboard'):
2016-03-17 17:48:17 +01:00
continue
elif atype in ('still', 'still_image'):
2013-12-06 09:15:04 +01:00
thumbnails.append({
'url': aurl,
'width': int_or_none(a.get('width')),
'height': int_or_none(a.get('height')),
2013-12-06 09:15:04 +01:00
})
2016-03-17 17:48:17 +01:00
else:
2016-05-20 21:16:08 +06:00
aext = a.get('ext')
is_m3u8 = a.get('container') == 'm3u8' or aext == 'm3u8'
2016-03-17 17:48:17 +01:00
formats.append({
'format_id': atype,
'url': aurl,
2016-03-17 17:48:17 +01:00
'tbr': int_or_none(a.get('bitrate')),
'vbr': int_or_none(a.get('opt_vbitrate')),
'width': int_or_none(a.get('width')),
'height': int_or_none(a.get('height')),
'filesize': int_or_none(a.get('size')),
'vcodec': a.get('codec'),
'container': a.get('container'),
2016-05-20 21:16:08 +06:00
'ext': 'mp4' if is_m3u8 else aext,
'protocol': 'm3u8' if is_m3u8 else None,
2016-03-17 17:48:17 +01:00
'preference': 1 if atype == 'original' else None,
})
2013-12-25 15:20:14 +01:00
self._sort_formats(formats)
2013-12-06 09:15:04 +01:00
return {
'id': video_id,
2016-03-17 17:48:17 +01:00
'title': title,
'description': data.get('seoDescription'),
2013-12-06 09:15:04 +01:00
'formats': formats,
'thumbnails': thumbnails,
'duration': float_or_none(data.get('duration')),
2016-03-17 17:48:17 +01:00
'timestamp': int_or_none(data.get('createdAt')),
2013-12-06 09:15:04 +01:00
}