]>
Commit | Line | Data |
---|---|---|
beaf832c | 1 | import functools |
92790f4e | 2 | import itertools |
2ab47fa3 | 3 | import json |
97362712 | 4 | import re |
aad0d6d5 | 5 | |
97362712 | 6 | from .common import InfoExtractor, SearchInfoExtractor |
97362712 | 7 | from ..networking import HEADRequest |
3d2623a8 | 8 | from ..networking.exceptions import HTTPError |
1cc79574 | 9 | from ..utils import ( |
97362712 | 10 | KNOWN_EXTENSIONS, |
aad0d6d5 | 11 | ExtractorError, |
e09965d5 | 12 | float_or_none, |
eb920777 | 13 | int_or_none, |
beaf832c | 14 | join_nonempty, |
e09965d5 | 15 | mimetype2ext, |
aa6c2530 | 16 | parse_qs, |
e09965d5 | 17 | str_or_none, |
97362712 | 18 | try_call, |
f516f440 | 19 | unified_timestamp, |
d7c7100e | 20 | update_url_query, |
7c5307f4 | 21 | url_or_none, |
a6c5859d | 22 | urlhandle_detect_ext, |
d7c7100e | 23 | ) |
246571ae | 24 | from ..utils.traversal import traverse_obj |
aad0d6d5 PH |
25 | |
26 | ||
548c3957 | 27 | class SoundcloudEmbedIE(InfoExtractor): |
cf80ff18 | 28 | _VALID_URL = r'https?://(?:w|player|p)\.soundcloud\.com/player/?.*?\burl=(?P<id>.+)' |
bfd973ec | 29 | _EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?://)?(?:w\.)?soundcloud\.com/player.+?)\1'] |
cf80ff18 RA |
30 | _TEST = { |
31 | # from https://www.soundi.fi/uutiset/ennakkokuuntelussa-timo-kaukolammen-station-to-station-to-station-julkaisua-juhlitaan-tanaan-g-livelabissa/ | |
32 | 'url': 'https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F922213810&show_artwork=true&maxwidth=640&maxheight=960&dnt=1&secret_token=s-ziYey', | |
33 | 'only_matching': True, | |
34 | } | |
548c3957 | 35 | |
548c3957 | 36 | def _real_extract(self, url): |
aa6c2530 | 37 | query = parse_qs(url) |
cf80ff18 RA |
38 | api_url = query['url'][0] |
39 | secret_token = query.get('secret_token') | |
40 | if secret_token: | |
41 | api_url = update_url_query(api_url, {'secret_token': secret_token[0]}) | |
42 | return self.url_result(api_url) | |
548c3957 RA |
43 | |
44 | ||
63ccf4ff | 45 | class SoundcloudBaseIE(InfoExtractor): |
08d30158 | 46 | _NETRC_MACHINE = 'soundcloud' |
47 | ||
63ccf4ff | 48 | _API_V2_BASE = 'https://api-v2.soundcloud.com/' |
49 | _BASE_URL = 'https://soundcloud.com/' | |
08d30158 | 50 | _USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36' |
51 | _API_AUTH_QUERY_TEMPLATE = '?client_id=%s' | |
52 | _API_AUTH_URL_PW = 'https://api-auth.soundcloud.com/web-auth/sign-in/password%s' | |
53 | _API_VERIFY_AUTH_TOKEN = 'https://api-auth.soundcloud.com/connect/session%s' | |
08d30158 | 54 | _HEADERS = {} |
63ccf4ff | 55 | |
c04cc2e2 KAW |
56 | _IMAGE_REPL_RE = r'-([0-9a-z]+)\.jpg' |
57 | ||
58 | _ARTWORK_MAP = { | |
59 | 'mini': 16, | |
60 | 'tiny': 20, | |
61 | 'small': 32, | |
62 | 'badge': 47, | |
63 | 't67x67': 67, | |
64 | 'large': 100, | |
65 | 't300x300': 300, | |
66 | 'crop': 400, | |
67 | 't500x500': 500, | |
68 | 'original': 0, | |
69 | } | |
70 | ||
beaf832c | 71 | _DEFAULT_FORMATS = ['http_aac', 'hls_aac', 'http_opus', 'hls_opus', 'http_mp3', 'hls_mp3'] |
72 | ||
73 | @functools.cached_property | |
74 | def _is_requested(self): | |
75 | return re.compile(r'|'.join(set( | |
76 | re.escape(pattern).replace(r'\*', r'.*') if pattern != 'default' | |
77 | else '|'.join(map(re.escape, self._DEFAULT_FORMATS)) | |
78 | for pattern in self._configuration_arg('formats', ['default'], ie_key=SoundcloudIE) | |
79 | ))).fullmatch | |
80 | ||
63ccf4ff | 81 | def _store_client_id(self, client_id): |
9809740b | 82 | self.cache.store('soundcloud', 'client_id', client_id) |
63ccf4ff | 83 | |
84 | def _update_client_id(self): | |
85 | webpage = self._download_webpage('https://soundcloud.com/', None) | |
86 | for src in reversed(re.findall(r'<script[^>]+src="([^"]+)"', webpage)): | |
87 | script = self._download_webpage(src, None, fatal=False) | |
88 | if script: | |
89 | client_id = self._search_regex( | |
90 | r'client_id\s*:\s*"([0-9a-zA-Z]{32})"', | |
91 | script, 'client id', default=None) | |
92 | if client_id: | |
93 | self._CLIENT_ID = client_id | |
94 | self._store_client_id(client_id) | |
95 | return | |
96 | raise ExtractorError('Unable to extract client id') | |
97 | ||
98 | def _download_json(self, *args, **kwargs): | |
99 | non_fatal = kwargs.get('fatal') is False | |
100 | if non_fatal: | |
101 | del kwargs['fatal'] | |
102 | query = kwargs.get('query', {}).copy() | |
103 | for _ in range(2): | |
104 | query['client_id'] = self._CLIENT_ID | |
105 | kwargs['query'] = query | |
106 | try: | |
f9934b96 | 107 | return super()._download_json(*args, **kwargs) |
63ccf4ff | 108 | except ExtractorError as e: |
3d2623a8 | 109 | if isinstance(e.cause, HTTPError) and e.cause.status in (401, 403): |
63ccf4ff | 110 | self._store_client_id(None) |
111 | self._update_client_id() | |
112 | continue | |
113 | elif non_fatal: | |
add96eb9 | 114 | self.report_warning(str(e)) |
63ccf4ff | 115 | return False |
116 | raise | |
117 | ||
52efa4b3 | 118 | def _initialize_pre_login(self): |
9809740b | 119 | self._CLIENT_ID = self.cache.load('soundcloud', 'client_id') or 'a3e059563d7fd3372b49b37f00a00bcf' |
63ccf4ff | 120 | |
97362712 | 121 | def _verify_oauth_token(self, token): |
122 | if self._request_webpage( | |
123 | self._API_VERIFY_AUTH_TOKEN % (self._API_AUTH_QUERY_TEMPLATE % self._CLIENT_ID), | |
124 | None, note='Verifying login token...', fatal=False, | |
125 | data=json.dumps({'session': {'access_token': token}}).encode()): | |
126 | self._HEADERS['Authorization'] = f'OAuth {token}' | |
127 | self.report_login() | |
128 | else: | |
129 | self.report_warning('Provided authorization token is invalid. Continuing as guest') | |
130 | ||
131 | def _real_initialize(self): | |
132 | if self._HEADERS: | |
133 | return | |
134 | if token := try_call(lambda: self._get_cookies(self._BASE_URL)['oauth_token'].value): | |
135 | self._verify_oauth_token(token) | |
136 | ||
52efa4b3 | 137 | def _perform_login(self, username, password): |
138 | if username != 'oauth': | |
97362712 | 139 | raise ExtractorError( |
63ccf4ff | 140 | 'Login using username and password is not currently supported. ' |
97362712 | 141 | 'Use "--username oauth --password <oauth_token>" to login using an oauth token, ' |
142 | f'or else {self._login_hint(method="cookies")}', expected=True) | |
143 | if self._HEADERS: | |
144 | return | |
145 | self._verify_oauth_token(password) | |
63ccf4ff | 146 | |
147 | r''' | |
148 | def genDevId(): | |
149 | def genNumBlock(): | |
150 | return ''.join([str(random.randrange(10)) for i in range(6)]) | |
151 | return '-'.join([genNumBlock() for i in range(4)]) | |
152 | ||
153 | payload = { | |
154 | 'client_id': self._CLIENT_ID, | |
155 | 'recaptcha_pubkey': 'null', | |
156 | 'recaptcha_response': 'null', | |
157 | 'credentials': { | |
158 | 'identifier': username, | |
159 | 'password': password | |
160 | }, | |
161 | 'signature': self.sign(username, password, self._CLIENT_ID), | |
162 | 'device_id': genDevId(), | |
163 | 'user_agent': self._USER_AGENT | |
164 | } | |
165 | ||
97362712 | 166 | response = self._download_json( |
167 | self._API_AUTH_URL_PW % (self._API_AUTH_QUERY_TEMPLATE % self._CLIENT_ID), | |
168 | None, note='Verifying login token...', fatal=False, | |
169 | data=json.dumps(payload).encode()) | |
170 | ||
171 | if token := traverse_obj(response, ('session', 'access_token', {str})): | |
172 | self._HEADERS['Authorization'] = f'OAuth {token}' | |
173 | self.report_login() | |
174 | return | |
175 | ||
176 | raise ExtractorError('Unable to get access token, login may have failed', expected=True) | |
63ccf4ff | 177 | ''' |
178 | ||
179 | # signature generation | |
180 | def sign(self, user, pw, clid): | |
181 | a = 33 | |
182 | i = 1 | |
183 | s = 440123 | |
184 | w = 117 | |
185 | u = 1800000 | |
186 | l = 1042 | |
187 | b = 37 | |
188 | k = 37 | |
189 | c = 5 | |
190 | n = '0763ed7314c69015fd4a0dc16bbf4b90' # _KEY | |
191 | y = '8' # _REV | |
192 | r = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36' # _USER_AGENT | |
193 | e = user # _USERNAME | |
194 | t = clid # _CLIENT_ID | |
195 | ||
196 | d = '-'.join([str(mInt) for mInt in [a, i, s, w, u, l, b, k]]) | |
add96eb9 | 197 | h = n + y + d + r + e + t + d + n |
63ccf4ff | 198 | |
199 | m = 8011470 | |
63ccf4ff | 200 | |
add96eb9 | 201 | for f in range(len(h)): |
63ccf4ff | 202 | m = (m >> 1) + ((1 & m) << 23) |
203 | m += ord(h[f]) | |
204 | m &= 16777215 | |
205 | ||
206 | # c is not even needed | |
add96eb9 | 207 | return f'{y}:{d}:{m:x}:{c}' |
63ccf4ff | 208 | |
c04cc2e2 | 209 | def _extract_info_dict(self, info, full_title=None, secret_token=None, extract_flat=False): |
add96eb9 | 210 | track_id = str(info['id']) |
c04cc2e2 KAW |
211 | title = info['title'] |
212 | ||
213 | format_urls = set() | |
214 | formats = [] | |
215 | query = {'client_id': self._CLIENT_ID} | |
216 | if secret_token: | |
217 | query['secret_token'] = secret_token | |
218 | ||
219 | if not extract_flat and info.get('downloadable') and info.get('has_downloads_left'): | |
220 | download_url = update_url_query( | |
221 | self._API_V2_BASE + 'tracks/' + track_id + '/download', query) | |
222 | redirect_url = (self._download_json(download_url, track_id, fatal=False) or {}).get('redirectUri') | |
223 | if redirect_url: | |
224 | urlh = self._request_webpage( | |
beaf832c | 225 | HEADRequest(redirect_url), track_id, 'Checking for original download format', fatal=False) |
c04cc2e2 | 226 | if urlh: |
3d2623a8 | 227 | format_url = urlh.url |
c04cc2e2 KAW |
228 | format_urls.add(format_url) |
229 | formats.append({ | |
230 | 'format_id': 'download', | |
231 | 'ext': urlhandle_detect_ext(urlh) or 'mp3', | |
232 | 'filesize': int_or_none(urlh.headers.get('Content-Length')), | |
233 | 'url': format_url, | |
234 | 'quality': 10, | |
a2d08407 | 235 | 'format_note': 'Original', |
c04cc2e2 KAW |
236 | }) |
237 | ||
238 | def invalid_url(url): | |
239 | return not url or url in format_urls | |
240 | ||
241 | def add_format(f, protocol, is_preview=False): | |
242 | mobj = re.search(r'\.(?P<abr>\d+)\.(?P<ext>[0-9a-z]{3,4})(?=[/?])', stream_url) | |
243 | if mobj: | |
244 | for k, v in mobj.groupdict().items(): | |
245 | if not f.get(k): | |
246 | f[k] = v | |
247 | format_id_list = [] | |
248 | if protocol: | |
249 | format_id_list.append(protocol) | |
250 | ext = f.get('ext') | |
251 | if ext == 'aac': | |
a2d08407 | 252 | f.update({ |
253 | 'abr': 256, | |
254 | 'quality': 5, | |
255 | 'format_note': 'Premium', | |
256 | }) | |
c04cc2e2 | 257 | for k in ('ext', 'abr'): |
a2d08407 | 258 | v = str_or_none(f.get(k)) |
c04cc2e2 KAW |
259 | if v: |
260 | format_id_list.append(v) | |
261 | preview = is_preview or re.search(r'/(?:preview|playlist)/0/30/', f['url']) | |
262 | if preview: | |
263 | format_id_list.append('preview') | |
264 | abr = f.get('abr') | |
265 | if abr: | |
266 | f['abr'] = int(abr) | |
beaf832c | 267 | if protocol in ('hls', 'hls-aes'): |
c04cc2e2 KAW |
268 | protocol = 'm3u8' if ext == 'aac' else 'm3u8_native' |
269 | else: | |
270 | protocol = 'http' | |
271 | f.update({ | |
272 | 'format_id': '_'.join(format_id_list), | |
273 | 'protocol': protocol, | |
274 | 'preference': -10 if preview else None, | |
275 | }) | |
276 | formats.append(f) | |
277 | ||
278 | # New API | |
246571ae | 279 | for t in traverse_obj(info, ('media', 'transcodings', lambda _, v: url_or_none(v['url']))): |
280 | if extract_flat: | |
281 | break | |
282 | format_url = t['url'] | |
246571ae | 283 | |
beaf832c | 284 | protocol = traverse_obj(t, ('format', 'protocol', {str})) |
285 | if protocol == 'progressive': | |
286 | protocol = 'http' | |
287 | if protocol != 'hls' and '/hls' in format_url: | |
288 | protocol = 'hls' | |
289 | if protocol == 'encrypted-hls' or '/encrypted-hls' in format_url: | |
290 | protocol = 'hls-aes' | |
291 | ||
292 | ext = None | |
293 | if preset := traverse_obj(t, ('preset', {str_or_none})): | |
294 | ext = preset.split('_')[0] | |
295 | if ext not in KNOWN_EXTENSIONS: | |
296 | ext = mimetype2ext(traverse_obj(t, ('format', 'mime_type', {str}))) | |
297 | ||
298 | identifier = join_nonempty(protocol, ext, delim='_') | |
299 | if not self._is_requested(identifier): | |
300 | self.write_debug(f'"{identifier}" is not a requested format, skipping') | |
301 | continue | |
302 | ||
303 | stream = None | |
246571ae | 304 | for retry in self.RetryManager(fatal=False): |
305 | try: | |
beaf832c | 306 | stream = self._download_json( |
307 | format_url, track_id, f'Downloading {identifier} format info JSON', | |
308 | query=query, headers=self._HEADERS) | |
246571ae | 309 | except ExtractorError as e: |
310 | if isinstance(e.cause, HTTPError) and e.cause.status == 429: | |
311 | self.report_warning( | |
312 | 'You have reached the API rate limit, which is ~600 requests per ' | |
313 | '10 minutes. Use the --extractor-retries and --retry-sleep options ' | |
314 | 'to configure an appropriate retry count and wait time', only_once=True) | |
315 | retry.error = e.cause | |
316 | else: | |
317 | self.report_warning(e.msg) | |
318 | ||
beaf832c | 319 | stream_url = traverse_obj(stream, ('url', {url_or_none})) |
c04cc2e2 KAW |
320 | if invalid_url(stream_url): |
321 | continue | |
322 | format_urls.add(stream_url) | |
c04cc2e2 KAW |
323 | add_format({ |
324 | 'url': stream_url, | |
325 | 'ext': ext, | |
beaf832c | 326 | }, protocol, t.get('snipped') or '/preview/' in format_url) |
c04cc2e2 KAW |
327 | |
328 | for f in formats: | |
329 | f['vcodec'] = 'none' | |
330 | ||
331 | if not formats and info.get('policy') == 'BLOCK': | |
332 | self.raise_geo_restricted(metadata_available=True) | |
c04cc2e2 KAW |
333 | |
334 | user = info.get('user') or {} | |
335 | ||
336 | thumbnails = [] | |
337 | artwork_url = info.get('artwork_url') | |
338 | thumbnail = artwork_url or user.get('avatar_url') | |
add96eb9 | 339 | if isinstance(thumbnail, str): |
c04cc2e2 KAW |
340 | if re.search(self._IMAGE_REPL_RE, thumbnail): |
341 | for image_id, size in self._ARTWORK_MAP.items(): | |
342 | i = { | |
343 | 'id': image_id, | |
add96eb9 | 344 | 'url': re.sub(self._IMAGE_REPL_RE, f'-{image_id}.jpg', thumbnail), |
c04cc2e2 KAW |
345 | } |
346 | if image_id == 'tiny' and not artwork_url: | |
347 | size = 18 | |
348 | elif image_id == 'original': | |
349 | i['preference'] = 10 | |
350 | if size: | |
351 | i.update({ | |
352 | 'width': size, | |
353 | 'height': size, | |
354 | }) | |
355 | thumbnails.append(i) | |
356 | else: | |
357 | thumbnails = [{'url': thumbnail}] | |
358 | ||
359 | def extract_count(key): | |
add96eb9 | 360 | return int_or_none(info.get(f'{key}_count')) |
c04cc2e2 KAW |
361 | |
362 | return { | |
363 | 'id': track_id, | |
364 | 'uploader': user.get('username'), | |
365 | 'uploader_id': str_or_none(user.get('id')) or user.get('permalink'), | |
366 | 'uploader_url': user.get('permalink_url'), | |
367 | 'timestamp': unified_timestamp(info.get('created_at')), | |
368 | 'title': title, | |
369 | 'description': info.get('description'), | |
370 | 'thumbnails': thumbnails, | |
371 | 'duration': float_or_none(info.get('duration'), 1000), | |
372 | 'webpage_url': info.get('permalink_url'), | |
373 | 'license': info.get('license'), | |
374 | 'view_count': extract_count('playback'), | |
375 | 'like_count': extract_count('favoritings') or extract_count('likes'), | |
376 | 'comment_count': extract_count('comment'), | |
377 | 'repost_count': extract_count('reposts'), | |
231c2eac | 378 | 'genres': traverse_obj(info, ('genre', {str}, {lambda x: x or None}, all)), |
add96eb9 | 379 | 'formats': formats if not extract_flat else None, |
c04cc2e2 KAW |
380 | } |
381 | ||
63ccf4ff | 382 | @classmethod |
383 | def _resolv_url(cls, url): | |
384 | return cls._API_V2_BASE + 'resolve?url=' + url | |
385 | ||
386 | ||
387 | class SoundcloudIE(SoundcloudBaseIE): | |
aad0d6d5 PH |
388 | """Information extractor for soundcloud.com |
389 | To access the media, the uid of the song and a stream token | |
390 | must be extracted from the page source and the script must make | |
391 | a request to media.soundcloud.com/crossdomain.xml. Then | |
392 | the media can be grabbed by requesting from an url composed | |
393 | of the stream token and uid | |
394 | """ | |
395 | ||
20991253 | 396 | _VALID_URL = r'''(?x)^(?:https?://)? |
71507a11 | 397 | (?:(?:(?:www\.|m\.)?soundcloud\.com/ |
836ef264 | 398 | (?!stations/track) |
4ff50ef8 | 399 | (?P<uploader>[\w\d-]+)/ |
3ef2da2d | 400 | (?!(?:tracks|albums|sets(?:/.+?)?|reposts|likes|spotlight)/?(?:$|[?#])) |
c2dedf12 L |
401 | (?P<title>[\w\d-]+) |
402 | (?:/(?P<token>(?!(?:albums|sets|recommended))[^?]+?))? | |
403 | (?:[?].*)?$) | |
548c3957 | 404 | |(?:api(?:-v2)?\.soundcloud\.com/tracks/(?P<track_id>\d+) |
0403b069 | 405 | (?:/?\?secret_token=(?P<secret_token>[^&]+))?) |
eb6a41ba JMF |
406 | ) |
407 | ''' | |
fbcd7b5f | 408 | IE_NAME = 'soundcloud' |
12c167c8 JMF |
409 | _TESTS = [ |
410 | { | |
fbcd7b5f | 411 | 'url': 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy', |
231c2eac | 412 | 'md5': 'de9bac153e7427a7333b4b0c1b6a18d2', |
fbcd7b5f | 413 | 'info_dict': { |
0eb9fb9f | 414 | 'id': '62986583', |
231c2eac | 415 | 'ext': 'opus', |
f516f440 | 416 | 'title': 'Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1', |
0eb9fb9f JMF |
417 | 'description': 'No Downloads untill we record the finished version this weekend, i was too pumped n i had to post it , earl is prolly gonna b hella p.o\'d', |
418 | 'uploader': 'E.T. ExTerrestrial Music', | |
548c3957 | 419 | 'uploader_id': '1571244', |
f516f440 S |
420 | 'timestamp': 1349920598, |
421 | 'upload_date': '20121011', | |
e09965d5 | 422 | 'duration': 143.216, |
4bfd294e | 423 | 'license': 'all-rights-reserved', |
f516f440 S |
424 | 'view_count': int, |
425 | 'like_count': int, | |
426 | 'comment_count': int, | |
427 | 'repost_count': int, | |
231c2eac | 428 | 'thumbnail': 'https://i1.sndcdn.com/artworks-000031955188-rwb18x-original.jpg', |
429 | 'uploader_url': 'https://soundcloud.com/ethmusic', | |
430 | 'genres': [], | |
add96eb9 | 431 | }, |
12c167c8 | 432 | }, |
a6c5859d | 433 | # geo-restricted |
12c167c8 | 434 | { |
fbcd7b5f PH |
435 | 'url': 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep', |
436 | 'info_dict': { | |
437 | 'id': '47127627', | |
231c2eac | 438 | 'ext': 'opus', |
fbcd7b5f | 439 | 'title': 'Goldrushed', |
63ad0315 | 440 | 'description': 'From Stockholm Sweden\r\nPovel / Magnus / Filip / David\r\nwww.theroyalconcept.com', |
fbcd7b5f | 441 | 'uploader': 'The Royal Concept', |
548c3957 | 442 | 'uploader_id': '9615865', |
f516f440 | 443 | 'timestamp': 1337635207, |
fbcd7b5f | 444 | 'upload_date': '20120521', |
a6c5859d | 445 | 'duration': 227.155, |
4bfd294e | 446 | 'license': 'all-rights-reserved', |
f516f440 S |
447 | 'view_count': int, |
448 | 'like_count': int, | |
449 | 'comment_count': int, | |
450 | 'repost_count': int, | |
231c2eac | 451 | 'uploader_url': 'https://soundcloud.com/the-concept-band', |
452 | 'thumbnail': 'https://i1.sndcdn.com/artworks-v8bFHhXm7Au6-0-original.jpg', | |
453 | 'genres': ['Alternative'], | |
12c167c8 | 454 | }, |
12c167c8 | 455 | }, |
de2dd4c5 JMF |
456 | # private link |
457 | { | |
7a5c1cfe | 458 | 'url': 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp', |
fbcd7b5f PH |
459 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', |
460 | 'info_dict': { | |
461 | 'id': '123998367', | |
462 | 'ext': 'mp3', | |
463 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
add96eb9 | 464 | 'description': 'test chars: "\'/\\ä↭', |
f516f440 | 465 | 'uploader': 'jaimeMF', |
548c3957 | 466 | 'uploader_id': '69767071', |
f516f440 | 467 | 'timestamp': 1386604920, |
fbcd7b5f | 468 | 'upload_date': '20131209', |
e09965d5 | 469 | 'duration': 9.927, |
4bfd294e | 470 | 'license': 'all-rights-reserved', |
f516f440 S |
471 | 'view_count': int, |
472 | 'like_count': int, | |
473 | 'comment_count': int, | |
474 | 'repost_count': int, | |
231c2eac | 475 | 'uploader_url': 'https://soundcloud.com/jaimemf', |
476 | 'thumbnail': 'https://a1.sndcdn.com/images/default_avatar_large.png', | |
477 | 'genres': ['youtubedl'], | |
de2dd4c5 JMF |
478 | }, |
479 | }, | |
9296738f | 480 | # private link (alt format) |
481 | { | |
482 | 'url': 'https://api.soundcloud.com/tracks/123998367?secret_token=s-8Pjrp', | |
483 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
484 | 'info_dict': { | |
485 | 'id': '123998367', | |
486 | 'ext': 'mp3', | |
487 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
add96eb9 | 488 | 'description': 'test chars: "\'/\\ä↭', |
f516f440 | 489 | 'uploader': 'jaimeMF', |
548c3957 | 490 | 'uploader_id': '69767071', |
f516f440 | 491 | 'timestamp': 1386604920, |
9296738f | 492 | 'upload_date': '20131209', |
e09965d5 | 493 | 'duration': 9.927, |
4bfd294e | 494 | 'license': 'all-rights-reserved', |
f516f440 S |
495 | 'view_count': int, |
496 | 'like_count': int, | |
497 | 'comment_count': int, | |
498 | 'repost_count': int, | |
231c2eac | 499 | 'uploader_url': 'https://soundcloud.com/jaimemf', |
500 | 'thumbnail': 'https://a1.sndcdn.com/images/default_avatar_large.png', | |
501 | 'genres': ['youtubedl'], | |
9296738f | 502 | }, |
503 | }, | |
f67ca84d JMF |
504 | # downloadable song |
505 | { | |
be05d5cf TOH |
506 | 'url': 'https://soundcloud.com/the80m/the-following', |
507 | 'md5': '9ffcddb08c87d74fb5808a3c183a1d04', | |
fbcd7b5f | 508 | 'info_dict': { |
be05d5cf TOH |
509 | 'id': '343609555', |
510 | 'ext': 'wav', | |
231c2eac | 511 | 'title': 'The Following', |
512 | 'description': '', | |
513 | 'uploader': '80M', | |
514 | 'uploader_id': '312384765', | |
515 | 'uploader_url': 'https://soundcloud.com/the80m', | |
516 | 'upload_date': '20170922', | |
517 | 'timestamp': 1506120436, | |
518 | 'duration': 397.228, | |
519 | 'thumbnail': 'https://i1.sndcdn.com/artworks-000243916348-ktoo7d-original.jpg', | |
520 | 'license': 'all-rights-reserved', | |
521 | 'like_count': int, | |
522 | 'comment_count': int, | |
523 | 'repost_count': int, | |
524 | 'view_count': int, | |
525 | 'genres': ['Dance & EDM'], | |
f67ca84d JMF |
526 | }, |
527 | }, | |
d7c7100e S |
528 | # private link, downloadable format |
529 | { | |
530 | 'url': 'https://soundcloud.com/oriuplift/uponly-238-no-talking-wav/s-AyZUd', | |
531 | 'md5': '64a60b16e617d41d0bef032b7f55441e', | |
532 | 'info_dict': { | |
533 | 'id': '340344461', | |
534 | 'ext': 'wav', | |
535 | 'title': 'Uplifting Only 238 [No Talking] (incl. Alex Feed Guestmix) (Aug 31, 2017) [wav]', | |
536 | 'description': 'md5:fa20ee0fca76a3d6df8c7e57f3715366', | |
537 | 'uploader': 'Ori Uplift Music', | |
548c3957 | 538 | 'uploader_id': '12563093', |
f516f440 | 539 | 'timestamp': 1504206263, |
d7c7100e | 540 | 'upload_date': '20170831', |
e09965d5 | 541 | 'duration': 7449.096, |
d7c7100e | 542 | 'license': 'all-rights-reserved', |
f516f440 S |
543 | 'view_count': int, |
544 | 'like_count': int, | |
545 | 'comment_count': int, | |
546 | 'repost_count': int, | |
231c2eac | 547 | 'thumbnail': 'https://i1.sndcdn.com/artworks-000240712245-kedn4p-original.jpg', |
548 | 'uploader_url': 'https://soundcloud.com/oriuplift', | |
549 | 'genres': ['Trance'], | |
d7c7100e S |
550 | }, |
551 | }, | |
0b0870f9 PV |
552 | # no album art, use avatar pic for thumbnail |
553 | { | |
554 | 'url': 'https://soundcloud.com/garyvee/sideways-prod-mad-real', | |
555 | 'md5': '59c7872bc44e5d99b7211891664760c2', | |
556 | 'info_dict': { | |
557 | 'id': '309699954', | |
558 | 'ext': 'mp3', | |
559 | 'title': 'Sideways (Prod. Mad Real)', | |
560 | 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e', | |
561 | 'uploader': 'garyvee', | |
548c3957 | 562 | 'uploader_id': '2366352', |
f516f440 | 563 | 'timestamp': 1488152409, |
0b0870f9 | 564 | 'upload_date': '20170226', |
e09965d5 | 565 | 'duration': 207.012, |
0b0870f9 PV |
566 | 'thumbnail': r're:https?://.*\.jpg', |
567 | 'license': 'all-rights-reserved', | |
f516f440 S |
568 | 'view_count': int, |
569 | 'like_count': int, | |
570 | 'comment_count': int, | |
571 | 'repost_count': int, | |
231c2eac | 572 | 'uploader_url': 'https://soundcloud.com/garyvee', |
573 | 'genres': [], | |
0b0870f9 PV |
574 | }, |
575 | 'params': { | |
576 | 'skip_download': True, | |
577 | }, | |
578 | }, | |
e09965d5 S |
579 | { |
580 | 'url': 'https://soundcloud.com/giovannisarani/mezzo-valzer', | |
231c2eac | 581 | 'md5': '8227c3473a4264df6b02ad7e5b7527ac', |
e09965d5 S |
582 | 'info_dict': { |
583 | 'id': '583011102', | |
231c2eac | 584 | 'ext': 'opus', |
e09965d5 | 585 | 'title': 'Mezzo Valzer', |
231c2eac | 586 | 'description': 'md5:f4d5f39d52e0ccc2b4f665326428901a', |
587 | 'uploader': 'Giovanni Sarani', | |
548c3957 | 588 | 'uploader_id': '3352531', |
e09965d5 S |
589 | 'timestamp': 1551394171, |
590 | 'upload_date': '20190228', | |
591 | 'duration': 180.157, | |
592 | 'thumbnail': r're:https?://.*\.jpg', | |
593 | 'license': 'all-rights-reserved', | |
594 | 'view_count': int, | |
595 | 'like_count': int, | |
596 | 'comment_count': int, | |
597 | 'repost_count': int, | |
231c2eac | 598 | 'genres': ['Piano'], |
599 | 'uploader_url': 'https://soundcloud.com/giovannisarani', | |
e09965d5 | 600 | }, |
75294a5e S |
601 | }, |
602 | { | |
bc842c27 | 603 | # AAC HQ format available (account with active subscription needed) |
75294a5e S |
604 | 'url': 'https://soundcloud.com/wandw/the-chainsmokers-ft-daya-dont-let-me-down-ww-remix-1', |
605 | 'only_matching': True, | |
606 | }, | |
bc842c27 U |
607 | { |
608 | # Go+ (account with active subscription needed) | |
609 | 'url': 'https://soundcloud.com/taylorswiftofficial/look-what-you-made-me-do', | |
610 | 'only_matching': True, | |
611 | }, | |
12c167c8 | 612 | ] |
aad0d6d5 | 613 | |
aad0d6d5 | 614 | def _real_extract(self, url): |
5ad28e7f | 615 | mobj = self._match_valid_url(url) |
aad0d6d5 | 616 | |
eb6a41ba | 617 | track_id = mobj.group('track_id') |
4bfd294e | 618 | |
3bed6217 | 619 | query = {} |
548c3957 RA |
620 | if track_id: |
621 | info_json_url = self._API_V2_BASE + 'tracks/' + track_id | |
eb6a41ba | 622 | full_title = track_id |
9296738f | 623 | token = mobj.group('secret_token') |
624 | if token: | |
548c3957 | 625 | query['secret_token'] = token |
eb6a41ba | 626 | else: |
add96eb9 | 627 | full_title = resolve_title = '{}/{}'.format(*mobj.group('uploader', 'title')) |
de2dd4c5 | 628 | token = mobj.group('token') |
de2dd4c5 | 629 | if token: |
add96eb9 | 630 | resolve_title += f'/{token}' |
548c3957 | 631 | info_json_url = self._resolv_url(self._BASE_URL + resolve_title) |
5f6a1245 | 632 | |
e09965d5 | 633 | info = self._download_json( |
fb4126a1 | 634 | info_json_url, full_title, 'Downloading info JSON', query=query, headers=self._HEADERS) |
a6c5859d RA |
635 | |
636 | return self._extract_info_dict(info, full_title, token) | |
aad0d6d5 | 637 | |
20991253 | 638 | |
63ccf4ff | 639 | class SoundcloudPlaylistBaseIE(SoundcloudBaseIE): |
2a5c26c9 | 640 | def _extract_set(self, playlist, token=None): |
add96eb9 | 641 | playlist_id = str(playlist['id']) |
2a5c26c9 | 642 | tracks = playlist.get('tracks') or [] |
add96eb9 | 643 | if not all(t.get('permalink_url') for t in tracks) and token: |
2a5c26c9 RA |
644 | tracks = self._download_json( |
645 | self._API_V2_BASE + 'tracks', playlist_id, | |
646 | 'Downloading tracks', query={ | |
add96eb9 | 647 | 'ids': ','.join([str(t['id']) for t in tracks]), |
2a5c26c9 RA |
648 | 'playlistId': playlist_id, |
649 | 'playlistSecretToken': token, | |
fb4126a1 | 650 | }, headers=self._HEADERS) |
548c3957 RA |
651 | entries = [] |
652 | for track in tracks: | |
653 | track_id = str_or_none(track.get('id')) | |
654 | url = track.get('permalink_url') | |
655 | if not url: | |
656 | if not track_id: | |
657 | continue | |
658 | url = self._API_V2_BASE + 'tracks/' + track_id | |
659 | if token: | |
660 | url += '?secret_token=' + token | |
661 | entries.append(self.url_result( | |
662 | url, SoundcloudIE.ie_key(), track_id)) | |
2a5c26c9 RA |
663 | return self.playlist_result( |
664 | entries, playlist_id, | |
665 | playlist.get('title'), | |
666 | playlist.get('description')) | |
8e45e1cc S |
667 | |
668 | ||
7518a61d | 669 | class SoundcloudSetIE(SoundcloudPlaylistBaseIE): |
906f980a | 670 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<uploader>[\w\d-]+)/sets/(?P<slug_title>[:\w\d-]+)(?:/(?P<token>[^?/]+))?' |
fbcd7b5f | 671 | IE_NAME = 'soundcloud:set' |
22a6f150 PH |
672 | _TESTS = [{ |
673 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep', | |
674 | 'info_dict': { | |
a9551e90 | 675 | 'id': '2284613', |
22a6f150 | 676 | 'title': 'The Royal Concept EP', |
2a5c26c9 | 677 | 'description': 'md5:71d07087c7a449e8941a70a29e34671e', |
22a6f150 | 678 | }, |
bf2dc9cc | 679 | 'playlist_mincount': 5, |
f7043ef3 S |
680 | }, { |
681 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep/token', | |
682 | 'only_matching': True, | |
906f980a U |
683 | }, { |
684 | 'url': 'https://soundcloud.com/discover/sets/weekly::flacmatic', | |
685 | 'only_matching': True, | |
686 | }, { | |
687 | 'url': 'https://soundcloud.com/discover/sets/charts-top:all-music:de', | |
688 | 'only_matching': True, | |
689 | }, { | |
690 | 'url': 'https://soundcloud.com/discover/sets/charts-top:hiphoprap:kr', | |
691 | 'only_matching': True, | |
22a6f150 | 692 | }] |
aad0d6d5 | 693 | |
aad0d6d5 | 694 | def _real_extract(self, url): |
5ad28e7f | 695 | mobj = self._match_valid_url(url) |
aad0d6d5 | 696 | |
add96eb9 | 697 | full_title = '{}/sets/{}'.format(*mobj.group('uploader', 'slug_title')) |
2f834e93 | 698 | token = mobj.group('token') |
699 | if token: | |
700 | full_title += '/' + token | |
aad0d6d5 | 701 | |
548c3957 | 702 | info = self._download_json(self._resolv_url( |
fb4126a1 | 703 | self._BASE_URL + full_title), full_title, headers=self._HEADERS) |
aad0d6d5 | 704 | |
aad0d6d5 | 705 | if 'errors' in info: |
add96eb9 | 706 | msgs = (str(err['error_message']) for err in info['errors']) |
707 | raise ExtractorError('unable to download video webpage: {}'.format(','.join(msgs))) | |
aad0d6d5 | 708 | |
2a5c26c9 | 709 | return self._extract_set(info, token) |
92790f4e JMF |
710 | |
711 | ||
63ccf4ff | 712 | class SoundcloudPagedPlaylistBaseIE(SoundcloudBaseIE): |
836ef264 | 713 | def _extract_playlist(self, base_url, playlist_id, playlist_title): |
aa6c2530 | 714 | return { |
715 | '_type': 'playlist', | |
716 | 'id': playlist_id, | |
717 | 'title': playlist_title, | |
718 | 'entries': self._entries(base_url, playlist_id), | |
719 | } | |
720 | ||
e04a1ff9 | 721 | def _entries(self, url, playlist_id): |
a0566bbf | 722 | # Per the SoundCloud documentation, the maximum limit for a linked partitioning query is 200. |
aa272535 | 723 | # https://developers.soundcloud.com/blog/offset-pagination-deprecated |
aa6c2530 | 724 | query = { |
38970916 | 725 | 'limit': 200, |
836ef264 | 726 | 'linked_partitioning': '1', |
aa6c2530 | 727 | 'offset': 0, |
836ef264 S |
728 | } |
729 | ||
836ef264 | 730 | for i in itertools.count(): |
be5c1ae8 | 731 | for retry in self.RetryManager(): |
e04a1ff9 | 732 | try: |
733 | response = self._download_json( | |
734 | url, playlist_id, query=query, headers=self._HEADERS, | |
be5c1ae8 | 735 | note=f'Downloading track page {i + 1}') |
e04a1ff9 | 736 | break |
737 | except ExtractorError as e: | |
738 | # Downloading page may result in intermittent 502 HTTP error | |
739 | # See https://github.com/yt-dlp/yt-dlp/issues/872 | |
3d2623a8 | 740 | if not isinstance(e.cause, HTTPError) or e.cause.status != 502: |
e04a1ff9 | 741 | raise |
be5c1ae8 | 742 | retry.error = e |
743 | continue | |
836ef264 | 744 | |
aa6c2530 | 745 | def resolve_entry(*candidates): |
7c5307f4 S |
746 | for cand in candidates: |
747 | if not isinstance(cand, dict): | |
748 | continue | |
749 | permalink_url = url_or_none(cand.get('permalink_url')) | |
aa6c2530 | 750 | if permalink_url: |
751 | return self.url_result( | |
752 | permalink_url, | |
753 | SoundcloudIE.ie_key() if SoundcloudIE.suitable(permalink_url) else None, | |
754 | str_or_none(cand.get('id')), cand.get('title')) | |
836ef264 | 755 | |
aa6c2530 | 756 | for e in response['collection'] or []: |
757 | yield resolve_entry(e, e.get('track'), e.get('playlist')) | |
836ef264 | 758 | |
e04a1ff9 | 759 | url = response.get('next_href') |
644149af | 760 | if not url: |
761 | break | |
aa6c2530 | 762 | query.pop('offset', None) |
836ef264 | 763 | |
836ef264 | 764 | |
836ef264 | 765 | class SoundcloudUserIE(SoundcloudPagedPlaylistBaseIE): |
16a08978 S |
766 | _VALID_URL = r'''(?x) |
767 | https?:// | |
768 | (?:(?:www|m)\.)?soundcloud\.com/ | |
769 | (?P<user>[^/]+) | |
770 | (?:/ | |
3ef2da2d | 771 | (?P<rsrc>tracks|albums|sets|reposts|likes|spotlight) |
16a08978 S |
772 | )? |
773 | /?(?:[?#].*)?$ | |
774 | ''' | |
fbcd7b5f | 775 | IE_NAME = 'soundcloud:user' |
22a6f150 | 776 | _TESTS = [{ |
b6423e6c | 777 | 'url': 'https://soundcloud.com/soft-cell-official', |
22a6f150 | 778 | 'info_dict': { |
b6423e6c S |
779 | 'id': '207965082', |
780 | 'title': 'Soft Cell (All)', | |
22a6f150 | 781 | }, |
b6423e6c | 782 | 'playlist_mincount': 28, |
22a6f150 | 783 | }, { |
b6423e6c | 784 | 'url': 'https://soundcloud.com/soft-cell-official/tracks', |
22a6f150 | 785 | 'info_dict': { |
b6423e6c S |
786 | 'id': '207965082', |
787 | 'title': 'Soft Cell (Tracks)', | |
22a6f150 | 788 | }, |
b6423e6c | 789 | 'playlist_mincount': 27, |
03b9c944 | 790 | }, { |
b6423e6c S |
791 | 'url': 'https://soundcloud.com/soft-cell-official/albums', |
792 | 'info_dict': { | |
793 | 'id': '207965082', | |
794 | 'title': 'Soft Cell (Albums)', | |
795 | }, | |
796 | 'playlist_mincount': 1, | |
797 | }, { | |
798 | 'url': 'https://soundcloud.com/jcv246/sets', | |
80fb6d4a | 799 | 'info_dict': { |
b6423e6c | 800 | 'id': '12982173', |
548c3957 | 801 | 'title': 'Jordi / cv (Sets)', |
80fb6d4a | 802 | }, |
8e45e1cc | 803 | 'playlist_mincount': 2, |
80fb6d4a | 804 | }, { |
b6423e6c | 805 | 'url': 'https://soundcloud.com/jcv246/reposts', |
80fb6d4a | 806 | 'info_dict': { |
b6423e6c S |
807 | 'id': '12982173', |
808 | 'title': 'Jordi / cv (Reposts)', | |
80fb6d4a | 809 | }, |
b6423e6c | 810 | 'playlist_mincount': 6, |
80fb6d4a | 811 | }, { |
b6423e6c | 812 | 'url': 'https://soundcloud.com/clalberg/likes', |
80fb6d4a | 813 | 'info_dict': { |
b6423e6c S |
814 | 'id': '11817582', |
815 | 'title': 'clalberg (Likes)', | |
80fb6d4a | 816 | }, |
b6423e6c | 817 | 'playlist_mincount': 5, |
80fb6d4a S |
818 | }, { |
819 | 'url': 'https://soundcloud.com/grynpyret/spotlight', | |
820 | 'info_dict': { | |
821 | 'id': '7098329', | |
bf2dc9cc | 822 | 'title': 'Grynpyret (Spotlight)', |
80fb6d4a S |
823 | }, |
824 | 'playlist_mincount': 1, | |
22a6f150 | 825 | }] |
92790f4e | 826 | |
80fb6d4a | 827 | _BASE_URL_MAP = { |
548c3957 RA |
828 | 'all': 'stream/users/%s', |
829 | 'tracks': 'users/%s/tracks', | |
830 | 'albums': 'users/%s/albums', | |
831 | 'sets': 'users/%s/playlists', | |
832 | 'reposts': 'stream/users/%s/reposts', | |
833 | 'likes': 'users/%s/likes', | |
834 | 'spotlight': 'users/%s/spotlight', | |
80fb6d4a S |
835 | } |
836 | ||
92790f4e | 837 | def _real_extract(self, url): |
5ad28e7f | 838 | mobj = self._match_valid_url(url) |
92790f4e JMF |
839 | uploader = mobj.group('user') |
840 | ||
20991253 | 841 | user = self._download_json( |
548c3957 | 842 | self._resolv_url(self._BASE_URL + uploader), |
fb4126a1 | 843 | uploader, 'Downloading user info', headers=self._HEADERS) |
80fb6d4a S |
844 | |
845 | resource = mobj.group('rsrc') or 'all' | |
80fb6d4a | 846 | |
836ef264 | 847 | return self._extract_playlist( |
548c3957 RA |
848 | self._API_V2_BASE + self._BASE_URL_MAP[resource] % user['id'], |
849 | str_or_none(user.get('id')), | |
add96eb9 | 850 | '{} ({})'.format(user['username'], resource.capitalize())) |
97afd99a | 851 | |
92790f4e | 852 | |
e107c2b8 | 853 | class SoundcloudUserPermalinkIE(SoundcloudPagedPlaylistBaseIE): |
854 | _VALID_URL = r'https?://api\.soundcloud\.com/users/(?P<id>\d+)' | |
855 | IE_NAME = 'soundcloud:user:permalink' | |
856 | _TESTS = [{ | |
857 | 'url': 'https://api.soundcloud.com/users/30909869', | |
858 | 'info_dict': { | |
859 | 'id': '30909869', | |
860 | 'title': 'neilcic', | |
861 | }, | |
862 | 'playlist_mincount': 23, | |
863 | }] | |
864 | ||
865 | def _real_extract(self, url): | |
866 | user_id = self._match_id(url) | |
867 | user = self._download_json( | |
868 | self._resolv_url(url), user_id, 'Downloading user info', headers=self._HEADERS) | |
869 | ||
870 | return self._extract_playlist( | |
871 | f'{self._API_V2_BASE}stream/users/{user["id"]}', str(user['id']), user.get('username')) | |
872 | ||
873 | ||
836ef264 S |
874 | class SoundcloudTrackStationIE(SoundcloudPagedPlaylistBaseIE): |
875 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/stations/track/[^/]+/(?P<id>[^/?#&]+)' | |
876 | IE_NAME = 'soundcloud:trackstation' | |
877 | _TESTS = [{ | |
878 | 'url': 'https://soundcloud.com/stations/track/officialsundial/your-text', | |
879 | 'info_dict': { | |
880 | 'id': '286017854', | |
548c3957 | 881 | 'title': 'Track station: your text', |
836ef264 S |
882 | }, |
883 | 'playlist_mincount': 47, | |
884 | }] | |
80fb6d4a | 885 | |
836ef264 S |
886 | def _real_extract(self, url): |
887 | track_name = self._match_id(url) | |
80fb6d4a | 888 | |
fb4126a1 | 889 | track = self._download_json(self._resolv_url(url), track_name, headers=self._HEADERS) |
836ef264 | 890 | track_id = self._search_regex( |
548c3957 | 891 | r'soundcloud:track-stations:(\d+)', track['id'], 'track id') |
92790f4e | 892 | |
836ef264 | 893 | return self._extract_playlist( |
add96eb9 | 894 | self._API_V2_BASE + 'stations/{}/tracks'.format(track['id']), |
895 | track_id, 'Track station: {}'.format(track['title'])) | |
20991253 PH |
896 | |
897 | ||
c2dedf12 L |
898 | class SoundcloudRelatedIE(SoundcloudPagedPlaylistBaseIE): |
899 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<slug>[\w\d-]+/[\w\d-]+)/(?P<relation>albums|sets|recommended)' | |
900 | IE_NAME = 'soundcloud:related' | |
901 | _TESTS = [{ | |
902 | 'url': 'https://soundcloud.com/wajang/sexapil-pingers-5/recommended', | |
903 | 'info_dict': { | |
904 | 'id': '1084577272', | |
905 | 'title': 'Sexapil - Pingers 5 (Recommended)', | |
906 | }, | |
907 | 'playlist_mincount': 50, | |
908 | }, { | |
909 | 'url': 'https://soundcloud.com/wajang/sexapil-pingers-5/albums', | |
910 | 'info_dict': { | |
911 | 'id': '1084577272', | |
912 | 'title': 'Sexapil - Pingers 5 (Albums)', | |
913 | }, | |
914 | 'playlist_mincount': 1, | |
915 | }, { | |
916 | 'url': 'https://soundcloud.com/wajang/sexapil-pingers-5/sets', | |
917 | 'info_dict': { | |
918 | 'id': '1084577272', | |
919 | 'title': 'Sexapil - Pingers 5 (Sets)', | |
920 | }, | |
921 | 'playlist_mincount': 4, | |
922 | }] | |
923 | ||
924 | _BASE_URL_MAP = { | |
925 | 'albums': 'tracks/%s/albums', | |
926 | 'sets': 'tracks/%s/playlists_without_albums', | |
927 | 'recommended': 'tracks/%s/related', | |
928 | } | |
929 | ||
930 | def _real_extract(self, url): | |
931 | slug, relation = self._match_valid_url(url).group('slug', 'relation') | |
932 | ||
933 | track = self._download_json( | |
934 | self._resolv_url(self._BASE_URL + slug), | |
935 | slug, 'Downloading track info', headers=self._HEADERS) | |
936 | ||
937 | if track.get('errors'): | |
938 | raise ExtractorError(f'{self.IE_NAME} said: %s' % ','.join( | |
939 | str(err['error_message']) for err in track['errors']), expected=True) | |
940 | ||
941 | return self._extract_playlist( | |
942 | self._API_V2_BASE + self._BASE_URL_MAP[relation] % track['id'], str(track['id']), | |
add96eb9 | 943 | '{} ({})'.format(track.get('title') or slug, relation.capitalize())) |
c2dedf12 L |
944 | |
945 | ||
7518a61d | 946 | class SoundcloudPlaylistIE(SoundcloudPlaylistBaseIE): |
548c3957 | 947 | _VALID_URL = r'https?://api(?:-v2)?\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$' |
20991253 | 948 | IE_NAME = 'soundcloud:playlist' |
46f74bcf | 949 | _TESTS = [{ |
f1c05100 | 950 | 'url': 'https://api.soundcloud.com/playlists/4110309', |
46f74bcf PH |
951 | 'info_dict': { |
952 | 'id': '4110309', | |
953 | 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]', | |
954 | 'description': 're:.*?TILT Brass - Bowery Poetry Club', | |
955 | }, | |
956 | 'playlist_count': 6, | |
957 | }] | |
20991253 PH |
958 | |
959 | def _real_extract(self, url): | |
5ad28e7f | 960 | mobj = self._match_valid_url(url) |
20991253 | 961 | playlist_id = mobj.group('id') |
20991253 | 962 | |
3bed6217 | 963 | query = {} |
2f834e93 | 964 | token = mobj.group('token') |
2f834e93 | 965 | if token: |
548c3957 | 966 | query['secret_token'] = token |
2f834e93 | 967 | |
20991253 | 968 | data = self._download_json( |
548c3957 | 969 | self._API_V2_BASE + 'playlists/' + playlist_id, |
fb4126a1 | 970 | playlist_id, 'Downloading playlist', query=query, headers=self._HEADERS) |
20991253 | 971 | |
2a5c26c9 | 972 | return self._extract_set(data, token) |
2abf7cab | 973 | |
974 | ||
63ccf4ff | 975 | class SoundcloudSearchIE(SoundcloudBaseIE, SearchInfoExtractor): |
2abf7cab | 976 | IE_NAME = 'soundcloud:search' |
96565c7e | 977 | IE_DESC = 'Soundcloud search' |
978 | _SEARCH_KEY = 'scsearch' | |
2abf7cab | 979 | _TESTS = [{ |
980 | 'url': 'scsearch15:post-avant jazzcore', | |
981 | 'info_dict': { | |
c04cc2e2 | 982 | 'id': 'post-avant jazzcore', |
2abf7cab | 983 | 'title': 'post-avant jazzcore', |
984 | }, | |
985 | 'playlist_count': 15, | |
986 | }] | |
987 | ||
328a22e1 | 988 | _MAX_RESULTS_PER_PAGE = 200 |
989 | _DEFAULT_RESULTS_PER_PAGE = 50 | |
2abf7cab | 990 | |
991 | def _get_collection(self, endpoint, collection_id, **query): | |
a3372437 | 992 | limit = min( |
328a22e1 | 993 | query.get('limit', self._DEFAULT_RESULTS_PER_PAGE), |
994 | self._MAX_RESULTS_PER_PAGE) | |
548c3957 RA |
995 | query.update({ |
996 | 'limit': limit, | |
548c3957 RA |
997 | 'linked_partitioning': 1, |
998 | 'offset': 0, | |
999 | }) | |
1000 | next_url = update_url_query(self._API_V2_BASE + endpoint, query) | |
2abf7cab | 1001 | |
f6c903e7 | 1002 | for i in itertools.count(1): |
7e347275 | 1003 | response = self._download_json( |
cc16383f | 1004 | next_url, collection_id, f'Downloading page {i}', |
fb4126a1 | 1005 | 'Unable to download API page', headers=self._HEADERS) |
2abf7cab | 1006 | |
cc16383f | 1007 | for item in response.get('collection') or []: |
1008 | if item: | |
c04cc2e2 KAW |
1009 | yield self.url_result( |
1010 | item['uri'], SoundcloudIE.ie_key(), **self._extract_info_dict(item, extract_flat=True)) | |
2abf7cab | 1011 | |
7e347275 | 1012 | next_url = response.get('next_href') |
f6c903e7 S |
1013 | if not next_url: |
1014 | break | |
2abf7cab | 1015 | |
1016 | def _get_n_results(self, query, n): | |
8863c8f0 | 1017 | return self.playlist_result(itertools.islice( |
1018 | self._get_collection('search/tracks', query, limit=n, q=query), | |
1019 | 0, None if n == float('inf') else n), query, query) |