]>
Commit | Line | Data |
---|---|---|
f096ec26 RA |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
a206ef62 RA |
4 | import re |
5 | ||
f096ec26 RA |
6 | from .common import InfoExtractor |
7 | from ..utils import ( | |
8 | int_or_none, | |
9 | urlencode_postdata, | |
10 | compat_str, | |
11 | ExtractorError, | |
12 | ) | |
13 | ||
14 | ||
15 | class CuriosityStreamBaseIE(InfoExtractor): | |
16 | _NETRC_MACHINE = 'curiositystream' | |
17 | _auth_token = None | |
f096ec26 RA |
18 | |
19 | def _handle_errors(self, result): | |
20 | error = result.get('error', {}).get('message') | |
21 | if error: | |
22 | if isinstance(error, dict): | |
23 | error = ', '.join(error.values()) | |
24 | raise ExtractorError( | |
25 | '%s said: %s' % (self.IE_NAME, error), expected=True) | |
26 | ||
f7ad7160 | 27 | def _call_api(self, path, video_id, query=None): |
f096ec26 RA |
28 | headers = {} |
29 | if self._auth_token: | |
30 | headers['X-Auth-Token'] = self._auth_token | |
31 | result = self._download_json( | |
f7ad7160 | 32 | self._API_BASE_URL + path, video_id, headers=headers, query=query) |
f096ec26 RA |
33 | self._handle_errors(result) |
34 | return result['data'] | |
35 | ||
52efa4b3 | 36 | def _perform_login(self, username, password): |
b207d5eb | 37 | result = self._download_json( |
d0e6121a | 38 | 'https://api.curiositystream.com/v1/login', None, |
39 | note='Logging in', data=urlencode_postdata({ | |
52efa4b3 | 40 | 'email': username, |
b207d5eb RA |
41 | 'password': password, |
42 | })) | |
43 | self._handle_errors(result) | |
92775d8a | 44 | CuriosityStreamBaseIE._auth_token = result['message']['auth_token'] |
f096ec26 | 45 | |
3b983ee4 RA |
46 | |
47 | class CuriosityStreamIE(CuriosityStreamBaseIE): | |
48 | IE_NAME = 'curiositystream' | |
49 | _VALID_URL = r'https?://(?:app\.)?curiositystream\.com/video/(?P<id>\d+)' | |
9ac24e23 | 50 | _TESTS = [{ |
3b983ee4 | 51 | 'url': 'https://app.curiositystream.com/video/2', |
3b983ee4 RA |
52 | 'info_dict': { |
53 | 'id': '2', | |
54 | 'ext': 'mp4', | |
55 | 'title': 'How Did You Develop The Internet?', | |
56 | 'description': 'Vint Cerf, Google\'s Chief Internet Evangelist, describes how he and Bob Kahn created the internet.', | |
9ac24e23 | 57 | 'channel': 'Curiosity Stream', |
58 | 'categories': ['Technology', 'Interview'], | |
59 | 'average_rating': 96.79, | |
60 | 'series_id': '2', | |
f7ad7160 | 61 | }, |
62 | 'params': { | |
f7ad7160 | 63 | # m3u8 download |
64 | 'skip_download': True, | |
65 | }, | |
9ac24e23 | 66 | }] |
3b983ee4 | 67 | |
d0e6121a | 68 | _API_BASE_URL = 'https://api.curiositystream.com/v1/media/' |
69 | ||
3b983ee4 RA |
70 | def _real_extract(self, url): |
71 | video_id = self._match_id(url) | |
f096ec26 | 72 | |
a206ef62 | 73 | formats = [] |
f7ad7160 | 74 | for encoding_format in ('m3u8', 'mpd'): |
d0e6121a | 75 | media = self._call_api(video_id, video_id, query={ |
f7ad7160 | 76 | 'encodingsNew': 'true', |
77 | 'encodingsFormat': encoding_format, | |
78 | }) | |
79 | for encoding in media.get('encodings', []): | |
80 | playlist_url = encoding.get('master_playlist_url') | |
81 | if encoding_format == 'm3u8': | |
82 | # use `m3u8` entry_protocol until EXT-X-MAP is properly supported by `m3u8_native` entry_protocol | |
83 | formats.extend(self._extract_m3u8_formats( | |
84 | playlist_url, video_id, 'mp4', | |
85 | m3u8_id='hls', fatal=False)) | |
86 | elif encoding_format == 'mpd': | |
87 | formats.extend(self._extract_mpd_formats( | |
88 | playlist_url, video_id, mpd_id='dash', fatal=False)) | |
89 | encoding_url = encoding.get('url') | |
90 | file_url = encoding.get('file_url') | |
91 | if not encoding_url and not file_url: | |
a206ef62 | 92 | continue |
f7ad7160 | 93 | f = { |
94 | 'width': int_or_none(encoding.get('width')), | |
95 | 'height': int_or_none(encoding.get('height')), | |
96 | 'vbr': int_or_none(encoding.get('video_bitrate')), | |
97 | 'abr': int_or_none(encoding.get('audio_bitrate')), | |
98 | 'filesize': int_or_none(encoding.get('size_in_bytes')), | |
99 | 'vcodec': encoding.get('video_codec'), | |
100 | 'acodec': encoding.get('audio_codec'), | |
101 | 'container': encoding.get('container_type'), | |
102 | } | |
103 | for f_url in (encoding_url, file_url): | |
104 | if not f_url: | |
105 | continue | |
106 | fmt = f.copy() | |
107 | rtmp = re.search(r'^(?P<url>rtmpe?://(?P<host>[^/]+)/(?P<app>.+))/(?P<playpath>mp[34]:.+)$', f_url) | |
108 | if rtmp: | |
109 | fmt.update({ | |
110 | 'url': rtmp.group('url'), | |
111 | 'play_path': rtmp.group('playpath'), | |
112 | 'app': rtmp.group('app'), | |
113 | 'ext': 'flv', | |
114 | 'format_id': 'rtmp', | |
115 | }) | |
116 | else: | |
117 | fmt.update({ | |
118 | 'url': f_url, | |
119 | 'format_id': 'http', | |
120 | }) | |
121 | formats.append(fmt) | |
a206ef62 RA |
122 | self._sort_formats(formats) |
123 | ||
f7ad7160 | 124 | title = media['title'] |
125 | ||
f096ec26 RA |
126 | subtitles = {} |
127 | for closed_caption in media.get('closed_captions', []): | |
128 | sub_url = closed_caption.get('file') | |
129 | if not sub_url: | |
130 | continue | |
131 | lang = closed_caption.get('code') or closed_caption.get('language') or 'en' | |
132 | subtitles.setdefault(lang, []).append({ | |
133 | 'url': sub_url, | |
134 | }) | |
135 | ||
136 | return { | |
f096ec26 | 137 | 'id': video_id, |
a206ef62 | 138 | 'formats': formats, |
f096ec26 RA |
139 | 'title': title, |
140 | 'description': media.get('description'), | |
141 | 'thumbnail': media.get('image_large') or media.get('image_medium') or media.get('image_small'), | |
142 | 'duration': int_or_none(media.get('duration')), | |
143 | 'tags': media.get('tags'), | |
144 | 'subtitles': subtitles, | |
9ac24e23 | 145 | 'channel': media.get('producer'), |
146 | 'categories': [media.get('primary_category'), media.get('type')], | |
147 | 'average_rating': media.get('rating_percentage'), | |
148 | 'series_id': str(media.get('collection_id') or '') or None, | |
f096ec26 RA |
149 | } |
150 | ||
151 | ||
92775d8a | 152 | class CuriosityStreamCollectionBaseIE(CuriosityStreamBaseIE): |
153 | ||
154 | def _real_extract(self, url): | |
155 | collection_id = self._match_id(url) | |
156 | collection = self._call_api(collection_id, collection_id) | |
157 | entries = [] | |
158 | for media in collection.get('media', []): | |
159 | media_id = compat_str(media.get('id')) | |
160 | media_type, ie = ('series', CuriosityStreamSeriesIE) if media.get('is_collection') else ('video', CuriosityStreamIE) | |
161 | entries.append(self.url_result( | |
162 | 'https://curiositystream.com/%s/%s' % (media_type, media_id), | |
163 | ie=ie.ie_key(), video_id=media_id)) | |
164 | return self.playlist_result( | |
165 | entries, collection_id, | |
166 | collection.get('title'), collection.get('description')) | |
167 | ||
168 | ||
169 | class CuriosityStreamCollectionsIE(CuriosityStreamCollectionBaseIE): | |
170 | IE_NAME = 'curiositystream:collections' | |
171 | _VALID_URL = r'https?://(?:app\.)?curiositystream\.com/collections/(?P<id>\d+)' | |
ef39f860 | 172 | _API_BASE_URL = 'https://api.curiositystream.com/v2/collections/' |
3b983ee4 | 173 | _TESTS = [{ |
ef39f860 | 174 | 'url': 'https://curiositystream.com/collections/86', |
f096ec26 | 175 | 'info_dict': { |
ef39f860 | 176 | 'id': '86', |
177 | 'title': 'Staff Picks', | |
178 | 'description': 'Wondering where to start? Here are a few of our favorite series and films... from our couch to yours.', | |
f096ec26 | 179 | }, |
ef39f860 | 180 | 'playlist_mincount': 7, |
ed807c18 | 181 | }, { |
92775d8a | 182 | 'url': 'https://curiositystream.com/collections/36', |
183 | 'only_matching': True, | |
184 | }] | |
185 | ||
186 | ||
187 | class CuriosityStreamSeriesIE(CuriosityStreamCollectionBaseIE): | |
188 | IE_NAME = 'curiositystream:series' | |
189 | _VALID_URL = r'https?://(?:app\.)?curiositystream\.com/(?:series|collection)/(?P<id>\d+)' | |
190 | _API_BASE_URL = 'https://api.curiositystream.com/v2/series/' | |
191 | _TESTS = [{ | |
192 | 'url': 'https://curiositystream.com/series/2', | |
ed807c18 | 193 | 'info_dict': { |
194 | 'id': '2', | |
195 | 'title': 'Curious Minds: The Internet', | |
196 | 'description': 'How is the internet shaping our lives in the 21st Century?', | |
197 | }, | |
198 | 'playlist_mincount': 16, | |
199 | }, { | |
92775d8a | 200 | 'url': 'https://curiositystream.com/collection/2', |
ed807c18 | 201 | 'only_matching': True, |
3b983ee4 | 202 | }] |