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