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