Merge pull request #9395 from pmrowla/afreecatv
[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     int_or_none,
7     float_or_none,
8 )
9
10
11 class WistiaIE(InfoExtractor):
12     _VALID_URL = r'(?:wistia:|https?://(?:fast\.)?wistia\.net/embed/iframe/)(?P<id>[a-z0-9]+)'
13     _API_URL = 'http://fast.wistia.com/embed/medias/%s.json'
14     _IFRAME_URL = 'http://fast.wistia.net/embed/iframe/%s'
15
16     _TESTS = [{
17         'url': 'http://fast.wistia.net/embed/iframe/sh7fpupwlt',
18         'md5': 'cafeb56ec0c53c18c97405eecb3133df',
19         'info_dict': {
20             'id': 'sh7fpupwlt',
21             'ext': 'mov',
22             'title': 'Being Resourceful',
23             'description': 'a Clients From Hell Video Series video from worldwidewebhosting',
24             'upload_date': '20131204',
25             'timestamp': 1386185018,
26             'duration': 117,
27         },
28     }, {
29         'url': 'wistia:sh7fpupwlt',
30         'only_matching': True,
31     }, {
32         # with hls video
33         'url': 'wistia:807fafadvk',
34         'only_matching': True,
35     }]
36
37     def _real_extract(self, url):
38         video_id = self._match_id(url)
39
40         data_json = self._download_json(
41             self._API_URL % video_id, video_id,
42             # Some videos require this.
43             headers={
44                 'Referer': url if url.startswith('http') else self._IFRAME_URL % video_id,
45             })
46
47         if data_json.get('error'):
48             raise ExtractorError(
49                 'Error while getting the playlist', expected=True)
50
51         data = data_json['media']
52         title = data['name']
53
54         formats = []
55         thumbnails = []
56         for a in data['assets']:
57             aurl = a.get('url')
58             if not aurl:
59                 continue
60             astatus = a.get('status')
61             atype = a.get('type')
62             if (astatus is not None and astatus != 2) or atype in ('preview', 'storyboard'):
63                 continue
64             elif atype in ('still', 'still_image'):
65                 thumbnails.append({
66                     'url': aurl,
67                     'width': int_or_none(a.get('width')),
68                     'height': int_or_none(a.get('height')),
69                 })
70             else:
71                 aext = a.get('ext')
72                 is_m3u8 = a.get('container') == 'm3u8' or aext == 'm3u8'
73                 formats.append({
74                     'format_id': atype,
75                     'url': aurl,
76                     'tbr': int_or_none(a.get('bitrate')),
77                     'vbr': int_or_none(a.get('opt_vbitrate')),
78                     'width': int_or_none(a.get('width')),
79                     'height': int_or_none(a.get('height')),
80                     'filesize': int_or_none(a.get('size')),
81                     'vcodec': a.get('codec'),
82                     'container': a.get('container'),
83                     'ext': 'mp4' if is_m3u8 else aext,
84                     'protocol': 'm3u8' if is_m3u8 else None,
85                     'preference': 1 if atype == 'original' else None,
86                 })
87
88         self._sort_formats(formats)
89
90         return {
91             'id': video_id,
92             'title': title,
93             'description': data.get('seoDescription'),
94             'formats': formats,
95             'thumbnails': thumbnails,
96             'duration': float_or_none(data.get('duration')),
97             'timestamp': int_or_none(data.get('createdAt')),
98         }