[wistia] extract more metadata
[youtube-dl] / youtube_dl / extractor / wistia.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5     ExtractorError,
6     sanitized_Request,
7     int_or_none,
8 )
9
10
11 class WistiaIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:fast\.)?wistia\.net/embed/iframe/(?P<id>[a-z0-9]+)'
13     _API_URL = 'http://fast.wistia.com/embed/medias/{0:}.json'
14
15     _TEST = {
16         'url': 'http://fast.wistia.net/embed/iframe/sh7fpupwlt',
17         'md5': 'cafeb56ec0c53c18c97405eecb3133df',
18         'info_dict': {
19             'id': 'sh7fpupwlt',
20             'ext': 'mov',
21             'title': 'Being Resourceful',
22             'description': 'a Clients From Hell Video Series video from worldwidewebhosting',
23             'upload_date': '20131204',
24             'timestamp': 1386185018,
25             'duration': 117,
26         },
27     }
28
29     def _real_extract(self, url):
30         video_id = self._match_id(url)
31
32         request = sanitized_Request(self._API_URL.format(video_id))
33         request.add_header('Referer', url)  # Some videos require this.
34         data_json = self._download_json(request, video_id)
35         if data_json.get('error'):
36             raise ExtractorError('Error while getting the playlist',
37                                  expected=True)
38         data = data_json['media']
39         title = data['name']
40
41         formats = []
42         thumbnails = []
43         for a in data['assets']:
44             astatus = a.get('status')
45             atype = a.get('type')
46             if (astatus is not None and astatus != 2) or atype == 'preview':
47                 continue
48             elif atype in ('still', 'still_image'):
49                 thumbnails.append({
50                     'url': a['url'],
51                     'resolution': '%dx%d' % (a['width'], a['height']),
52                 })
53             else:
54                 formats.append({
55                     'format_id': atype,
56                     'url': a['url'],
57                     'tbr': int_or_none(a.get('bitrate')),
58                     'vbr': int_or_none(a.get('opt_vbitrate')),
59                     'width': int_or_none(a.get('width')),
60                     'height': int_or_none(a.get('height')),
61                     'filesize': int_or_none(a.get('size')),
62                     'vcodec': a.get('codec'),
63                     'container': a.get('container'),
64                     'ext': a.get('ext'),
65                     'preference': 1 if atype == 'original' else None,
66                 })
67
68         self._sort_formats(formats)
69
70         return {
71             'id': video_id,
72             'title': title,
73             'description': data.get('seoDescription'),
74             'formats': formats,
75             'thumbnails': thumbnails,
76             'duration': int_or_none(data.get('duration')),
77             'timestamp': int_or_none(data.get('createdAt')),
78         }