Merge remote-tracking branch 'naglis/wistia'
[youtube-dl] / 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 ExtractorError, compat_urllib_request
7
8
9 class WistiaIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:fast\.)?wistia\.net/embed/iframe/(?P<id>[a-z0-9]+)'
11     _API_URL = 'http://fast.wistia.com/embed/medias/{0:}.json'
12
13     _TEST = {
14         'url': 'http://fast.wistia.net/embed/iframe/sh7fpupwlt',
15         'md5': 'cafeb56ec0c53c18c97405eecb3133df',
16         'info_dict': {
17             'id': 'sh7fpupwlt',
18             'ext': 'mov',
19             'title': 'Being Resourceful',
20             'duration': 117,
21         },
22     }
23
24     def _real_extract(self, url):
25         mobj = re.match(self._VALID_URL, url)
26         video_id = mobj.group('id')
27
28         request = compat_urllib_request.Request(self._API_URL.format(video_id))
29         request.add_header('Referer', url)  # Some videos require this.
30         data_json = self._download_json(request, video_id)
31         if data_json.get('error'):
32             raise ExtractorError('Error while getting the playlist',
33                                  expected=True)
34         data = data_json['media']
35
36         formats = []
37         thumbnails = []
38         for atype, a in data['assets'].items():
39             if atype == 'still':
40                 thumbnails.append({
41                     'url': a['url'],
42                     'resolution': '%dx%d' % (a['width'], a['height']),
43                 })
44                 continue
45             if atype == 'preview':
46                 continue
47             formats.append({
48                 'format_id': atype,
49                 'url': a['url'],
50                 'width': a['width'],
51                 'height': a['height'],
52                 'filesize': a['size'],
53                 'ext': a['ext'],
54                 'preference': 1 if atype == 'original' else None,
55             })
56
57         self._sort_formats(formats)
58
59         return {
60             'id': video_id,
61             'title': data['name'],
62             'formats': formats,
63             'thumbnails': thumbnails,
64             'duration': data.get('duration'),
65         }