]>
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 | ||
e53e56b7 | 98 | def _call_api(self, *args, **kwargs): |
63ccf4ff | 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: | |
e53e56b7 | 107 | return self._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 | ||
e53e56b7 | 166 | response = self._call_api( |
97362712 | 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'): | |
e53e56b7 | 220 | try: |
221 | # Do not use _call_api(); HTTP Error codes have different meanings for this request | |
222 | download_data = self._download_json( | |
223 | f'{self._API_V2_BASE}tracks/{track_id}/download', track_id, | |
224 | 'Downloading original download format info JSON', query=query, headers=self._HEADERS) | |
225 | except ExtractorError as e: | |
226 | if isinstance(e.cause, HTTPError) and e.cause.status == 401: | |
227 | self.report_warning( | |
228 | 'Original download format is only available ' | |
229 | f'for registered users. {self._login_hint()}') | |
230 | elif isinstance(e.cause, HTTPError) and e.cause.status == 403: | |
231 | self.write_debug('Original download format is not available for this client') | |
232 | else: | |
233 | self.report_warning(e.msg) | |
234 | download_data = None | |
235 | ||
236 | if redirect_url := traverse_obj(download_data, ('redirectUri', {url_or_none})): | |
c04cc2e2 | 237 | urlh = self._request_webpage( |
e53e56b7 | 238 | HEADRequest(redirect_url), track_id, 'Checking original download format availability', |
239 | 'Original download format is not available', fatal=False) | |
c04cc2e2 | 240 | if urlh: |
3d2623a8 | 241 | format_url = urlh.url |
c04cc2e2 KAW |
242 | format_urls.add(format_url) |
243 | formats.append({ | |
244 | 'format_id': 'download', | |
245 | 'ext': urlhandle_detect_ext(urlh) or 'mp3', | |
246 | 'filesize': int_or_none(urlh.headers.get('Content-Length')), | |
247 | 'url': format_url, | |
248 | 'quality': 10, | |
a2d08407 | 249 | 'format_note': 'Original', |
c04cc2e2 KAW |
250 | }) |
251 | ||
252 | def invalid_url(url): | |
253 | return not url or url in format_urls | |
254 | ||
255 | def add_format(f, protocol, is_preview=False): | |
256 | mobj = re.search(r'\.(?P<abr>\d+)\.(?P<ext>[0-9a-z]{3,4})(?=[/?])', stream_url) | |
257 | if mobj: | |
258 | for k, v in mobj.groupdict().items(): | |
259 | if not f.get(k): | |
260 | f[k] = v | |
261 | format_id_list = [] | |
262 | if protocol: | |
263 | format_id_list.append(protocol) | |
264 | ext = f.get('ext') | |
265 | if ext == 'aac': | |
a2d08407 | 266 | f.update({ |
267 | 'abr': 256, | |
268 | 'quality': 5, | |
269 | 'format_note': 'Premium', | |
270 | }) | |
c04cc2e2 | 271 | for k in ('ext', 'abr'): |
a2d08407 | 272 | v = str_or_none(f.get(k)) |
c04cc2e2 KAW |
273 | if v: |
274 | format_id_list.append(v) | |
275 | preview = is_preview or re.search(r'/(?:preview|playlist)/0/30/', f['url']) | |
276 | if preview: | |
277 | format_id_list.append('preview') | |
278 | abr = f.get('abr') | |
279 | if abr: | |
280 | f['abr'] = int(abr) | |
beaf832c | 281 | if protocol in ('hls', 'hls-aes'): |
c04cc2e2 KAW |
282 | protocol = 'm3u8' if ext == 'aac' else 'm3u8_native' |
283 | else: | |
284 | protocol = 'http' | |
285 | f.update({ | |
286 | 'format_id': '_'.join(format_id_list), | |
287 | 'protocol': protocol, | |
288 | 'preference': -10 if preview else None, | |
289 | }) | |
290 | formats.append(f) | |
291 | ||
292 | # New API | |
246571ae | 293 | for t in traverse_obj(info, ('media', 'transcodings', lambda _, v: url_or_none(v['url']))): |
294 | if extract_flat: | |
295 | break | |
296 | format_url = t['url'] | |
246571ae | 297 | |
beaf832c | 298 | protocol = traverse_obj(t, ('format', 'protocol', {str})) |
299 | if protocol == 'progressive': | |
300 | protocol = 'http' | |
301 | if protocol != 'hls' and '/hls' in format_url: | |
302 | protocol = 'hls' | |
303 | if protocol == 'encrypted-hls' or '/encrypted-hls' in format_url: | |
304 | protocol = 'hls-aes' | |
305 | ||
306 | ext = None | |
307 | if preset := traverse_obj(t, ('preset', {str_or_none})): | |
308 | ext = preset.split('_')[0] | |
309 | if ext not in KNOWN_EXTENSIONS: | |
310 | ext = mimetype2ext(traverse_obj(t, ('format', 'mime_type', {str}))) | |
311 | ||
312 | identifier = join_nonempty(protocol, ext, delim='_') | |
313 | if not self._is_requested(identifier): | |
314 | self.write_debug(f'"{identifier}" is not a requested format, skipping') | |
315 | continue | |
316 | ||
317 | stream = None | |
246571ae | 318 | for retry in self.RetryManager(fatal=False): |
319 | try: | |
e53e56b7 | 320 | stream = self._call_api( |
beaf832c | 321 | format_url, track_id, f'Downloading {identifier} format info JSON', |
322 | query=query, headers=self._HEADERS) | |
246571ae | 323 | except ExtractorError as e: |
324 | if isinstance(e.cause, HTTPError) and e.cause.status == 429: | |
325 | self.report_warning( | |
326 | 'You have reached the API rate limit, which is ~600 requests per ' | |
327 | '10 minutes. Use the --extractor-retries and --retry-sleep options ' | |
328 | 'to configure an appropriate retry count and wait time', only_once=True) | |
329 | retry.error = e.cause | |
330 | else: | |
331 | self.report_warning(e.msg) | |
332 | ||
beaf832c | 333 | stream_url = traverse_obj(stream, ('url', {url_or_none})) |
c04cc2e2 KAW |
334 | if invalid_url(stream_url): |
335 | continue | |
336 | format_urls.add(stream_url) | |
c04cc2e2 KAW |
337 | add_format({ |
338 | 'url': stream_url, | |
339 | 'ext': ext, | |
beaf832c | 340 | }, protocol, t.get('snipped') or '/preview/' in format_url) |
c04cc2e2 KAW |
341 | |
342 | for f in formats: | |
343 | f['vcodec'] = 'none' | |
344 | ||
345 | if not formats and info.get('policy') == 'BLOCK': | |
346 | self.raise_geo_restricted(metadata_available=True) | |
c04cc2e2 KAW |
347 | |
348 | user = info.get('user') or {} | |
349 | ||
350 | thumbnails = [] | |
351 | artwork_url = info.get('artwork_url') | |
352 | thumbnail = artwork_url or user.get('avatar_url') | |
add96eb9 | 353 | if isinstance(thumbnail, str): |
c04cc2e2 KAW |
354 | if re.search(self._IMAGE_REPL_RE, thumbnail): |
355 | for image_id, size in self._ARTWORK_MAP.items(): | |
356 | i = { | |
357 | 'id': image_id, | |
add96eb9 | 358 | 'url': re.sub(self._IMAGE_REPL_RE, f'-{image_id}.jpg', thumbnail), |
c04cc2e2 KAW |
359 | } |
360 | if image_id == 'tiny' and not artwork_url: | |
361 | size = 18 | |
362 | elif image_id == 'original': | |
363 | i['preference'] = 10 | |
364 | if size: | |
365 | i.update({ | |
366 | 'width': size, | |
367 | 'height': size, | |
368 | }) | |
369 | thumbnails.append(i) | |
370 | else: | |
371 | thumbnails = [{'url': thumbnail}] | |
372 | ||
373 | def extract_count(key): | |
add96eb9 | 374 | return int_or_none(info.get(f'{key}_count')) |
c04cc2e2 KAW |
375 | |
376 | return { | |
377 | 'id': track_id, | |
378 | 'uploader': user.get('username'), | |
379 | 'uploader_id': str_or_none(user.get('id')) or user.get('permalink'), | |
380 | 'uploader_url': user.get('permalink_url'), | |
381 | 'timestamp': unified_timestamp(info.get('created_at')), | |
382 | 'title': title, | |
383 | 'description': info.get('description'), | |
384 | 'thumbnails': thumbnails, | |
385 | 'duration': float_or_none(info.get('duration'), 1000), | |
386 | 'webpage_url': info.get('permalink_url'), | |
387 | 'license': info.get('license'), | |
388 | 'view_count': extract_count('playback'), | |
389 | 'like_count': extract_count('favoritings') or extract_count('likes'), | |
390 | 'comment_count': extract_count('comment'), | |
391 | 'repost_count': extract_count('reposts'), | |
231c2eac | 392 | 'genres': traverse_obj(info, ('genre', {str}, {lambda x: x or None}, all)), |
add96eb9 | 393 | 'formats': formats if not extract_flat else None, |
c04cc2e2 KAW |
394 | } |
395 | ||
63ccf4ff | 396 | @classmethod |
397 | def _resolv_url(cls, url): | |
398 | return cls._API_V2_BASE + 'resolve?url=' + url | |
399 | ||
400 | ||
401 | class SoundcloudIE(SoundcloudBaseIE): | |
aad0d6d5 PH |
402 | """Information extractor for soundcloud.com |
403 | To access the media, the uid of the song and a stream token | |
404 | must be extracted from the page source and the script must make | |
405 | a request to media.soundcloud.com/crossdomain.xml. Then | |
406 | the media can be grabbed by requesting from an url composed | |
407 | of the stream token and uid | |
408 | """ | |
409 | ||
20991253 | 410 | _VALID_URL = r'''(?x)^(?:https?://)? |
71507a11 | 411 | (?:(?:(?:www\.|m\.)?soundcloud\.com/ |
836ef264 | 412 | (?!stations/track) |
4ff50ef8 | 413 | (?P<uploader>[\w\d-]+)/ |
3ef2da2d | 414 | (?!(?:tracks|albums|sets(?:/.+?)?|reposts|likes|spotlight)/?(?:$|[?#])) |
c2dedf12 L |
415 | (?P<title>[\w\d-]+) |
416 | (?:/(?P<token>(?!(?:albums|sets|recommended))[^?]+?))? | |
417 | (?:[?].*)?$) | |
548c3957 | 418 | |(?:api(?:-v2)?\.soundcloud\.com/tracks/(?P<track_id>\d+) |
0403b069 | 419 | (?:/?\?secret_token=(?P<secret_token>[^&]+))?) |
eb6a41ba JMF |
420 | ) |
421 | ''' | |
fbcd7b5f | 422 | IE_NAME = 'soundcloud' |
12c167c8 JMF |
423 | _TESTS = [ |
424 | { | |
fbcd7b5f | 425 | 'url': 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy', |
231c2eac | 426 | 'md5': 'de9bac153e7427a7333b4b0c1b6a18d2', |
fbcd7b5f | 427 | 'info_dict': { |
0eb9fb9f | 428 | 'id': '62986583', |
231c2eac | 429 | 'ext': 'opus', |
f516f440 | 430 | 'title': 'Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1', |
0eb9fb9f JMF |
431 | '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', |
432 | 'uploader': 'E.T. ExTerrestrial Music', | |
548c3957 | 433 | 'uploader_id': '1571244', |
f516f440 S |
434 | 'timestamp': 1349920598, |
435 | 'upload_date': '20121011', | |
e09965d5 | 436 | 'duration': 143.216, |
4bfd294e | 437 | 'license': 'all-rights-reserved', |
f516f440 S |
438 | 'view_count': int, |
439 | 'like_count': int, | |
440 | 'comment_count': int, | |
441 | 'repost_count': int, | |
231c2eac | 442 | 'thumbnail': 'https://i1.sndcdn.com/artworks-000031955188-rwb18x-original.jpg', |
443 | 'uploader_url': 'https://soundcloud.com/ethmusic', | |
444 | 'genres': [], | |
add96eb9 | 445 | }, |
12c167c8 | 446 | }, |
a6c5859d | 447 | # geo-restricted |
12c167c8 | 448 | { |
fbcd7b5f PH |
449 | 'url': 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep', |
450 | 'info_dict': { | |
451 | 'id': '47127627', | |
231c2eac | 452 | 'ext': 'opus', |
fbcd7b5f | 453 | 'title': 'Goldrushed', |
63ad0315 | 454 | 'description': 'From Stockholm Sweden\r\nPovel / Magnus / Filip / David\r\nwww.theroyalconcept.com', |
fbcd7b5f | 455 | 'uploader': 'The Royal Concept', |
548c3957 | 456 | 'uploader_id': '9615865', |
f516f440 | 457 | 'timestamp': 1337635207, |
fbcd7b5f | 458 | 'upload_date': '20120521', |
a6c5859d | 459 | 'duration': 227.155, |
4bfd294e | 460 | 'license': 'all-rights-reserved', |
f516f440 S |
461 | 'view_count': int, |
462 | 'like_count': int, | |
463 | 'comment_count': int, | |
464 | 'repost_count': int, | |
231c2eac | 465 | 'uploader_url': 'https://soundcloud.com/the-concept-band', |
466 | 'thumbnail': 'https://i1.sndcdn.com/artworks-v8bFHhXm7Au6-0-original.jpg', | |
467 | 'genres': ['Alternative'], | |
12c167c8 | 468 | }, |
12c167c8 | 469 | }, |
de2dd4c5 JMF |
470 | # private link |
471 | { | |
7a5c1cfe | 472 | 'url': 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp', |
fbcd7b5f PH |
473 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', |
474 | 'info_dict': { | |
475 | 'id': '123998367', | |
476 | 'ext': 'mp3', | |
477 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
add96eb9 | 478 | 'description': 'test chars: "\'/\\ä↭', |
f516f440 | 479 | 'uploader': 'jaimeMF', |
548c3957 | 480 | 'uploader_id': '69767071', |
f516f440 | 481 | 'timestamp': 1386604920, |
fbcd7b5f | 482 | 'upload_date': '20131209', |
e09965d5 | 483 | 'duration': 9.927, |
4bfd294e | 484 | 'license': 'all-rights-reserved', |
f516f440 S |
485 | 'view_count': int, |
486 | 'like_count': int, | |
487 | 'comment_count': int, | |
488 | 'repost_count': int, | |
231c2eac | 489 | 'uploader_url': 'https://soundcloud.com/jaimemf', |
490 | 'thumbnail': 'https://a1.sndcdn.com/images/default_avatar_large.png', | |
491 | 'genres': ['youtubedl'], | |
de2dd4c5 JMF |
492 | }, |
493 | }, | |
9296738f | 494 | # private link (alt format) |
495 | { | |
496 | 'url': 'https://api.soundcloud.com/tracks/123998367?secret_token=s-8Pjrp', | |
497 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
498 | 'info_dict': { | |
499 | 'id': '123998367', | |
500 | 'ext': 'mp3', | |
501 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
add96eb9 | 502 | 'description': 'test chars: "\'/\\ä↭', |
f516f440 | 503 | 'uploader': 'jaimeMF', |
548c3957 | 504 | 'uploader_id': '69767071', |
f516f440 | 505 | 'timestamp': 1386604920, |
9296738f | 506 | 'upload_date': '20131209', |
e09965d5 | 507 | 'duration': 9.927, |
4bfd294e | 508 | 'license': 'all-rights-reserved', |
f516f440 S |
509 | 'view_count': int, |
510 | 'like_count': int, | |
511 | 'comment_count': int, | |
512 | 'repost_count': int, | |
231c2eac | 513 | 'uploader_url': 'https://soundcloud.com/jaimemf', |
514 | 'thumbnail': 'https://a1.sndcdn.com/images/default_avatar_large.png', | |
515 | 'genres': ['youtubedl'], | |
9296738f | 516 | }, |
517 | }, | |
f67ca84d JMF |
518 | # downloadable song |
519 | { | |
be05d5cf TOH |
520 | 'url': 'https://soundcloud.com/the80m/the-following', |
521 | 'md5': '9ffcddb08c87d74fb5808a3c183a1d04', | |
fbcd7b5f | 522 | 'info_dict': { |
be05d5cf TOH |
523 | 'id': '343609555', |
524 | 'ext': 'wav', | |
231c2eac | 525 | 'title': 'The Following', |
526 | 'description': '', | |
527 | 'uploader': '80M', | |
528 | 'uploader_id': '312384765', | |
529 | 'uploader_url': 'https://soundcloud.com/the80m', | |
530 | 'upload_date': '20170922', | |
531 | 'timestamp': 1506120436, | |
532 | 'duration': 397.228, | |
533 | 'thumbnail': 'https://i1.sndcdn.com/artworks-000243916348-ktoo7d-original.jpg', | |
534 | 'license': 'all-rights-reserved', | |
535 | 'like_count': int, | |
536 | 'comment_count': int, | |
537 | 'repost_count': int, | |
538 | 'view_count': int, | |
539 | 'genres': ['Dance & EDM'], | |
f67ca84d JMF |
540 | }, |
541 | }, | |
d7c7100e S |
542 | # private link, downloadable format |
543 | { | |
544 | 'url': 'https://soundcloud.com/oriuplift/uponly-238-no-talking-wav/s-AyZUd', | |
545 | 'md5': '64a60b16e617d41d0bef032b7f55441e', | |
546 | 'info_dict': { | |
547 | 'id': '340344461', | |
548 | 'ext': 'wav', | |
549 | 'title': 'Uplifting Only 238 [No Talking] (incl. Alex Feed Guestmix) (Aug 31, 2017) [wav]', | |
550 | 'description': 'md5:fa20ee0fca76a3d6df8c7e57f3715366', | |
551 | 'uploader': 'Ori Uplift Music', | |
548c3957 | 552 | 'uploader_id': '12563093', |
f516f440 | 553 | 'timestamp': 1504206263, |
d7c7100e | 554 | 'upload_date': '20170831', |
e09965d5 | 555 | 'duration': 7449.096, |
d7c7100e | 556 | 'license': 'all-rights-reserved', |
f516f440 S |
557 | 'view_count': int, |
558 | 'like_count': int, | |
559 | 'comment_count': int, | |
560 | 'repost_count': int, | |
231c2eac | 561 | 'thumbnail': 'https://i1.sndcdn.com/artworks-000240712245-kedn4p-original.jpg', |
562 | 'uploader_url': 'https://soundcloud.com/oriuplift', | |
563 | 'genres': ['Trance'], | |
d7c7100e S |
564 | }, |
565 | }, | |
0b0870f9 PV |
566 | # no album art, use avatar pic for thumbnail |
567 | { | |
568 | 'url': 'https://soundcloud.com/garyvee/sideways-prod-mad-real', | |
569 | 'md5': '59c7872bc44e5d99b7211891664760c2', | |
570 | 'info_dict': { | |
571 | 'id': '309699954', | |
572 | 'ext': 'mp3', | |
573 | 'title': 'Sideways (Prod. Mad Real)', | |
574 | 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e', | |
575 | 'uploader': 'garyvee', | |
548c3957 | 576 | 'uploader_id': '2366352', |
f516f440 | 577 | 'timestamp': 1488152409, |
0b0870f9 | 578 | 'upload_date': '20170226', |
e09965d5 | 579 | 'duration': 207.012, |
0b0870f9 PV |
580 | 'thumbnail': r're:https?://.*\.jpg', |
581 | 'license': 'all-rights-reserved', | |
f516f440 S |
582 | 'view_count': int, |
583 | 'like_count': int, | |
584 | 'comment_count': int, | |
585 | 'repost_count': int, | |
231c2eac | 586 | 'uploader_url': 'https://soundcloud.com/garyvee', |
587 | 'genres': [], | |
0b0870f9 PV |
588 | }, |
589 | 'params': { | |
590 | 'skip_download': True, | |
591 | }, | |
592 | }, | |
e09965d5 S |
593 | { |
594 | 'url': 'https://soundcloud.com/giovannisarani/mezzo-valzer', | |
231c2eac | 595 | 'md5': '8227c3473a4264df6b02ad7e5b7527ac', |
e09965d5 S |
596 | 'info_dict': { |
597 | 'id': '583011102', | |
231c2eac | 598 | 'ext': 'opus', |
e09965d5 | 599 | 'title': 'Mezzo Valzer', |
231c2eac | 600 | 'description': 'md5:f4d5f39d52e0ccc2b4f665326428901a', |
601 | 'uploader': 'Giovanni Sarani', | |
548c3957 | 602 | 'uploader_id': '3352531', |
e09965d5 S |
603 | 'timestamp': 1551394171, |
604 | 'upload_date': '20190228', | |
605 | 'duration': 180.157, | |
606 | 'thumbnail': r're:https?://.*\.jpg', | |
607 | 'license': 'all-rights-reserved', | |
608 | 'view_count': int, | |
609 | 'like_count': int, | |
610 | 'comment_count': int, | |
611 | 'repost_count': int, | |
231c2eac | 612 | 'genres': ['Piano'], |
613 | 'uploader_url': 'https://soundcloud.com/giovannisarani', | |
e09965d5 | 614 | }, |
75294a5e S |
615 | }, |
616 | { | |
bc842c27 | 617 | # AAC HQ format available (account with active subscription needed) |
75294a5e S |
618 | 'url': 'https://soundcloud.com/wandw/the-chainsmokers-ft-daya-dont-let-me-down-ww-remix-1', |
619 | 'only_matching': True, | |
620 | }, | |
bc842c27 U |
621 | { |
622 | # Go+ (account with active subscription needed) | |
623 | 'url': 'https://soundcloud.com/taylorswiftofficial/look-what-you-made-me-do', | |
624 | 'only_matching': True, | |
625 | }, | |
12c167c8 | 626 | ] |
aad0d6d5 | 627 | |
aad0d6d5 | 628 | def _real_extract(self, url): |
5ad28e7f | 629 | mobj = self._match_valid_url(url) |
aad0d6d5 | 630 | |
eb6a41ba | 631 | track_id = mobj.group('track_id') |
4bfd294e | 632 | |
3bed6217 | 633 | query = {} |
548c3957 RA |
634 | if track_id: |
635 | info_json_url = self._API_V2_BASE + 'tracks/' + track_id | |
eb6a41ba | 636 | full_title = track_id |
9296738f | 637 | token = mobj.group('secret_token') |
638 | if token: | |
548c3957 | 639 | query['secret_token'] = token |
eb6a41ba | 640 | else: |
add96eb9 | 641 | full_title = resolve_title = '{}/{}'.format(*mobj.group('uploader', 'title')) |
de2dd4c5 | 642 | token = mobj.group('token') |
de2dd4c5 | 643 | if token: |
add96eb9 | 644 | resolve_title += f'/{token}' |
548c3957 | 645 | info_json_url = self._resolv_url(self._BASE_URL + resolve_title) |
5f6a1245 | 646 | |
e53e56b7 | 647 | info = self._call_api( |
fb4126a1 | 648 | info_json_url, full_title, 'Downloading info JSON', query=query, headers=self._HEADERS) |
a6c5859d RA |
649 | |
650 | return self._extract_info_dict(info, full_title, token) | |
aad0d6d5 | 651 | |
20991253 | 652 | |
63ccf4ff | 653 | class SoundcloudPlaylistBaseIE(SoundcloudBaseIE): |
2a5c26c9 | 654 | def _extract_set(self, playlist, token=None): |
add96eb9 | 655 | playlist_id = str(playlist['id']) |
2a5c26c9 | 656 | tracks = playlist.get('tracks') or [] |
add96eb9 | 657 | if not all(t.get('permalink_url') for t in tracks) and token: |
e53e56b7 | 658 | tracks = self._call_api( |
2a5c26c9 RA |
659 | self._API_V2_BASE + 'tracks', playlist_id, |
660 | 'Downloading tracks', query={ | |
add96eb9 | 661 | 'ids': ','.join([str(t['id']) for t in tracks]), |
2a5c26c9 RA |
662 | 'playlistId': playlist_id, |
663 | 'playlistSecretToken': token, | |
fb4126a1 | 664 | }, headers=self._HEADERS) |
548c3957 RA |
665 | entries = [] |
666 | for track in tracks: | |
667 | track_id = str_or_none(track.get('id')) | |
668 | url = track.get('permalink_url') | |
669 | if not url: | |
670 | if not track_id: | |
671 | continue | |
672 | url = self._API_V2_BASE + 'tracks/' + track_id | |
673 | if token: | |
674 | url += '?secret_token=' + token | |
675 | entries.append(self.url_result( | |
676 | url, SoundcloudIE.ie_key(), track_id)) | |
2a5c26c9 RA |
677 | return self.playlist_result( |
678 | entries, playlist_id, | |
679 | playlist.get('title'), | |
680 | playlist.get('description')) | |
8e45e1cc S |
681 | |
682 | ||
7518a61d | 683 | class SoundcloudSetIE(SoundcloudPlaylistBaseIE): |
906f980a | 684 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<uploader>[\w\d-]+)/sets/(?P<slug_title>[:\w\d-]+)(?:/(?P<token>[^?/]+))?' |
fbcd7b5f | 685 | IE_NAME = 'soundcloud:set' |
22a6f150 PH |
686 | _TESTS = [{ |
687 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep', | |
688 | 'info_dict': { | |
a9551e90 | 689 | 'id': '2284613', |
22a6f150 | 690 | 'title': 'The Royal Concept EP', |
2a5c26c9 | 691 | 'description': 'md5:71d07087c7a449e8941a70a29e34671e', |
22a6f150 | 692 | }, |
bf2dc9cc | 693 | 'playlist_mincount': 5, |
f7043ef3 S |
694 | }, { |
695 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep/token', | |
696 | 'only_matching': True, | |
906f980a U |
697 | }, { |
698 | 'url': 'https://soundcloud.com/discover/sets/weekly::flacmatic', | |
699 | 'only_matching': True, | |
700 | }, { | |
701 | 'url': 'https://soundcloud.com/discover/sets/charts-top:all-music:de', | |
702 | 'only_matching': True, | |
703 | }, { | |
704 | 'url': 'https://soundcloud.com/discover/sets/charts-top:hiphoprap:kr', | |
705 | 'only_matching': True, | |
22a6f150 | 706 | }] |
aad0d6d5 | 707 | |
aad0d6d5 | 708 | def _real_extract(self, url): |
5ad28e7f | 709 | mobj = self._match_valid_url(url) |
aad0d6d5 | 710 | |
add96eb9 | 711 | full_title = '{}/sets/{}'.format(*mobj.group('uploader', 'slug_title')) |
2f834e93 | 712 | token = mobj.group('token') |
713 | if token: | |
714 | full_title += '/' + token | |
aad0d6d5 | 715 | |
e53e56b7 | 716 | info = self._call_api(self._resolv_url( |
fb4126a1 | 717 | self._BASE_URL + full_title), full_title, headers=self._HEADERS) |
aad0d6d5 | 718 | |
aad0d6d5 | 719 | if 'errors' in info: |
add96eb9 | 720 | msgs = (str(err['error_message']) for err in info['errors']) |
721 | raise ExtractorError('unable to download video webpage: {}'.format(','.join(msgs))) | |
aad0d6d5 | 722 | |
2a5c26c9 | 723 | return self._extract_set(info, token) |
92790f4e JMF |
724 | |
725 | ||
63ccf4ff | 726 | class SoundcloudPagedPlaylistBaseIE(SoundcloudBaseIE): |
836ef264 | 727 | def _extract_playlist(self, base_url, playlist_id, playlist_title): |
aa6c2530 | 728 | return { |
729 | '_type': 'playlist', | |
730 | 'id': playlist_id, | |
731 | 'title': playlist_title, | |
732 | 'entries': self._entries(base_url, playlist_id), | |
733 | } | |
734 | ||
e04a1ff9 | 735 | def _entries(self, url, playlist_id): |
a0566bbf | 736 | # Per the SoundCloud documentation, the maximum limit for a linked partitioning query is 200. |
aa272535 | 737 | # https://developers.soundcloud.com/blog/offset-pagination-deprecated |
aa6c2530 | 738 | query = { |
38970916 | 739 | 'limit': 200, |
836ef264 | 740 | 'linked_partitioning': '1', |
aa6c2530 | 741 | 'offset': 0, |
836ef264 S |
742 | } |
743 | ||
836ef264 | 744 | for i in itertools.count(): |
be5c1ae8 | 745 | for retry in self.RetryManager(): |
e04a1ff9 | 746 | try: |
e53e56b7 | 747 | response = self._call_api( |
e04a1ff9 | 748 | url, playlist_id, query=query, headers=self._HEADERS, |
be5c1ae8 | 749 | note=f'Downloading track page {i + 1}') |
e04a1ff9 | 750 | break |
751 | except ExtractorError as e: | |
752 | # Downloading page may result in intermittent 502 HTTP error | |
753 | # See https://github.com/yt-dlp/yt-dlp/issues/872 | |
3d2623a8 | 754 | if not isinstance(e.cause, HTTPError) or e.cause.status != 502: |
e04a1ff9 | 755 | raise |
be5c1ae8 | 756 | retry.error = e |
757 | continue | |
836ef264 | 758 | |
aa6c2530 | 759 | def resolve_entry(*candidates): |
7c5307f4 S |
760 | for cand in candidates: |
761 | if not isinstance(cand, dict): | |
762 | continue | |
763 | permalink_url = url_or_none(cand.get('permalink_url')) | |
aa6c2530 | 764 | if permalink_url: |
765 | return self.url_result( | |
766 | permalink_url, | |
767 | SoundcloudIE.ie_key() if SoundcloudIE.suitable(permalink_url) else None, | |
768 | str_or_none(cand.get('id')), cand.get('title')) | |
836ef264 | 769 | |
aa6c2530 | 770 | for e in response['collection'] or []: |
771 | yield resolve_entry(e, e.get('track'), e.get('playlist')) | |
836ef264 | 772 | |
e04a1ff9 | 773 | url = response.get('next_href') |
644149af | 774 | if not url: |
775 | break | |
aa6c2530 | 776 | query.pop('offset', None) |
836ef264 | 777 | |
836ef264 | 778 | |
836ef264 | 779 | class SoundcloudUserIE(SoundcloudPagedPlaylistBaseIE): |
16a08978 S |
780 | _VALID_URL = r'''(?x) |
781 | https?:// | |
782 | (?:(?:www|m)\.)?soundcloud\.com/ | |
783 | (?P<user>[^/]+) | |
784 | (?:/ | |
3ef2da2d | 785 | (?P<rsrc>tracks|albums|sets|reposts|likes|spotlight) |
16a08978 S |
786 | )? |
787 | /?(?:[?#].*)?$ | |
788 | ''' | |
fbcd7b5f | 789 | IE_NAME = 'soundcloud:user' |
22a6f150 | 790 | _TESTS = [{ |
b6423e6c | 791 | 'url': 'https://soundcloud.com/soft-cell-official', |
22a6f150 | 792 | 'info_dict': { |
b6423e6c S |
793 | 'id': '207965082', |
794 | 'title': 'Soft Cell (All)', | |
22a6f150 | 795 | }, |
b6423e6c | 796 | 'playlist_mincount': 28, |
22a6f150 | 797 | }, { |
b6423e6c | 798 | 'url': 'https://soundcloud.com/soft-cell-official/tracks', |
22a6f150 | 799 | 'info_dict': { |
b6423e6c S |
800 | 'id': '207965082', |
801 | 'title': 'Soft Cell (Tracks)', | |
22a6f150 | 802 | }, |
b6423e6c | 803 | 'playlist_mincount': 27, |
03b9c944 | 804 | }, { |
b6423e6c S |
805 | 'url': 'https://soundcloud.com/soft-cell-official/albums', |
806 | 'info_dict': { | |
807 | 'id': '207965082', | |
808 | 'title': 'Soft Cell (Albums)', | |
809 | }, | |
810 | 'playlist_mincount': 1, | |
811 | }, { | |
812 | 'url': 'https://soundcloud.com/jcv246/sets', | |
80fb6d4a | 813 | 'info_dict': { |
b6423e6c | 814 | 'id': '12982173', |
548c3957 | 815 | 'title': 'Jordi / cv (Sets)', |
80fb6d4a | 816 | }, |
8e45e1cc | 817 | 'playlist_mincount': 2, |
80fb6d4a | 818 | }, { |
b6423e6c | 819 | 'url': 'https://soundcloud.com/jcv246/reposts', |
80fb6d4a | 820 | 'info_dict': { |
b6423e6c S |
821 | 'id': '12982173', |
822 | 'title': 'Jordi / cv (Reposts)', | |
80fb6d4a | 823 | }, |
b6423e6c | 824 | 'playlist_mincount': 6, |
80fb6d4a | 825 | }, { |
b6423e6c | 826 | 'url': 'https://soundcloud.com/clalberg/likes', |
80fb6d4a | 827 | 'info_dict': { |
b6423e6c S |
828 | 'id': '11817582', |
829 | 'title': 'clalberg (Likes)', | |
80fb6d4a | 830 | }, |
b6423e6c | 831 | 'playlist_mincount': 5, |
80fb6d4a S |
832 | }, { |
833 | 'url': 'https://soundcloud.com/grynpyret/spotlight', | |
834 | 'info_dict': { | |
835 | 'id': '7098329', | |
bf2dc9cc | 836 | 'title': 'Grynpyret (Spotlight)', |
80fb6d4a S |
837 | }, |
838 | 'playlist_mincount': 1, | |
22a6f150 | 839 | }] |
92790f4e | 840 | |
80fb6d4a | 841 | _BASE_URL_MAP = { |
548c3957 RA |
842 | 'all': 'stream/users/%s', |
843 | 'tracks': 'users/%s/tracks', | |
844 | 'albums': 'users/%s/albums', | |
845 | 'sets': 'users/%s/playlists', | |
846 | 'reposts': 'stream/users/%s/reposts', | |
847 | 'likes': 'users/%s/likes', | |
848 | 'spotlight': 'users/%s/spotlight', | |
80fb6d4a S |
849 | } |
850 | ||
92790f4e | 851 | def _real_extract(self, url): |
5ad28e7f | 852 | mobj = self._match_valid_url(url) |
92790f4e JMF |
853 | uploader = mobj.group('user') |
854 | ||
e53e56b7 | 855 | user = self._call_api( |
548c3957 | 856 | self._resolv_url(self._BASE_URL + uploader), |
fb4126a1 | 857 | uploader, 'Downloading user info', headers=self._HEADERS) |
80fb6d4a S |
858 | |
859 | resource = mobj.group('rsrc') or 'all' | |
80fb6d4a | 860 | |
836ef264 | 861 | return self._extract_playlist( |
548c3957 RA |
862 | self._API_V2_BASE + self._BASE_URL_MAP[resource] % user['id'], |
863 | str_or_none(user.get('id')), | |
add96eb9 | 864 | '{} ({})'.format(user['username'], resource.capitalize())) |
97afd99a | 865 | |
92790f4e | 866 | |
e107c2b8 | 867 | class SoundcloudUserPermalinkIE(SoundcloudPagedPlaylistBaseIE): |
868 | _VALID_URL = r'https?://api\.soundcloud\.com/users/(?P<id>\d+)' | |
869 | IE_NAME = 'soundcloud:user:permalink' | |
870 | _TESTS = [{ | |
871 | 'url': 'https://api.soundcloud.com/users/30909869', | |
872 | 'info_dict': { | |
873 | 'id': '30909869', | |
874 | 'title': 'neilcic', | |
875 | }, | |
876 | 'playlist_mincount': 23, | |
877 | }] | |
878 | ||
879 | def _real_extract(self, url): | |
880 | user_id = self._match_id(url) | |
e53e56b7 | 881 | user = self._call_api( |
e107c2b8 | 882 | self._resolv_url(url), user_id, 'Downloading user info', headers=self._HEADERS) |
883 | ||
884 | return self._extract_playlist( | |
885 | f'{self._API_V2_BASE}stream/users/{user["id"]}', str(user['id']), user.get('username')) | |
886 | ||
887 | ||
836ef264 S |
888 | class SoundcloudTrackStationIE(SoundcloudPagedPlaylistBaseIE): |
889 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/stations/track/[^/]+/(?P<id>[^/?#&]+)' | |
890 | IE_NAME = 'soundcloud:trackstation' | |
891 | _TESTS = [{ | |
892 | 'url': 'https://soundcloud.com/stations/track/officialsundial/your-text', | |
893 | 'info_dict': { | |
894 | 'id': '286017854', | |
548c3957 | 895 | 'title': 'Track station: your text', |
836ef264 S |
896 | }, |
897 | 'playlist_mincount': 47, | |
898 | }] | |
80fb6d4a | 899 | |
836ef264 S |
900 | def _real_extract(self, url): |
901 | track_name = self._match_id(url) | |
80fb6d4a | 902 | |
e53e56b7 | 903 | track = self._call_api(self._resolv_url(url), track_name, headers=self._HEADERS) |
836ef264 | 904 | track_id = self._search_regex( |
548c3957 | 905 | r'soundcloud:track-stations:(\d+)', track['id'], 'track id') |
92790f4e | 906 | |
836ef264 | 907 | return self._extract_playlist( |
add96eb9 | 908 | self._API_V2_BASE + 'stations/{}/tracks'.format(track['id']), |
909 | track_id, 'Track station: {}'.format(track['title'])) | |
20991253 PH |
910 | |
911 | ||
c2dedf12 L |
912 | class SoundcloudRelatedIE(SoundcloudPagedPlaylistBaseIE): |
913 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<slug>[\w\d-]+/[\w\d-]+)/(?P<relation>albums|sets|recommended)' | |
914 | IE_NAME = 'soundcloud:related' | |
915 | _TESTS = [{ | |
916 | 'url': 'https://soundcloud.com/wajang/sexapil-pingers-5/recommended', | |
917 | 'info_dict': { | |
918 | 'id': '1084577272', | |
919 | 'title': 'Sexapil - Pingers 5 (Recommended)', | |
920 | }, | |
921 | 'playlist_mincount': 50, | |
922 | }, { | |
923 | 'url': 'https://soundcloud.com/wajang/sexapil-pingers-5/albums', | |
924 | 'info_dict': { | |
925 | 'id': '1084577272', | |
926 | 'title': 'Sexapil - Pingers 5 (Albums)', | |
927 | }, | |
928 | 'playlist_mincount': 1, | |
929 | }, { | |
930 | 'url': 'https://soundcloud.com/wajang/sexapil-pingers-5/sets', | |
931 | 'info_dict': { | |
932 | 'id': '1084577272', | |
933 | 'title': 'Sexapil - Pingers 5 (Sets)', | |
934 | }, | |
935 | 'playlist_mincount': 4, | |
936 | }] | |
937 | ||
938 | _BASE_URL_MAP = { | |
939 | 'albums': 'tracks/%s/albums', | |
940 | 'sets': 'tracks/%s/playlists_without_albums', | |
941 | 'recommended': 'tracks/%s/related', | |
942 | } | |
943 | ||
944 | def _real_extract(self, url): | |
945 | slug, relation = self._match_valid_url(url).group('slug', 'relation') | |
946 | ||
e53e56b7 | 947 | track = self._call_api( |
c2dedf12 L |
948 | self._resolv_url(self._BASE_URL + slug), |
949 | slug, 'Downloading track info', headers=self._HEADERS) | |
950 | ||
951 | if track.get('errors'): | |
952 | raise ExtractorError(f'{self.IE_NAME} said: %s' % ','.join( | |
953 | str(err['error_message']) for err in track['errors']), expected=True) | |
954 | ||
955 | return self._extract_playlist( | |
956 | self._API_V2_BASE + self._BASE_URL_MAP[relation] % track['id'], str(track['id']), | |
add96eb9 | 957 | '{} ({})'.format(track.get('title') or slug, relation.capitalize())) |
c2dedf12 L |
958 | |
959 | ||
7518a61d | 960 | class SoundcloudPlaylistIE(SoundcloudPlaylistBaseIE): |
548c3957 | 961 | _VALID_URL = r'https?://api(?:-v2)?\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$' |
20991253 | 962 | IE_NAME = 'soundcloud:playlist' |
46f74bcf | 963 | _TESTS = [{ |
f1c05100 | 964 | 'url': 'https://api.soundcloud.com/playlists/4110309', |
46f74bcf PH |
965 | 'info_dict': { |
966 | 'id': '4110309', | |
967 | 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]', | |
968 | 'description': 're:.*?TILT Brass - Bowery Poetry Club', | |
969 | }, | |
970 | 'playlist_count': 6, | |
971 | }] | |
20991253 PH |
972 | |
973 | def _real_extract(self, url): | |
5ad28e7f | 974 | mobj = self._match_valid_url(url) |
20991253 | 975 | playlist_id = mobj.group('id') |
20991253 | 976 | |
3bed6217 | 977 | query = {} |
2f834e93 | 978 | token = mobj.group('token') |
2f834e93 | 979 | if token: |
548c3957 | 980 | query['secret_token'] = token |
2f834e93 | 981 | |
e53e56b7 | 982 | data = self._call_api( |
548c3957 | 983 | self._API_V2_BASE + 'playlists/' + playlist_id, |
fb4126a1 | 984 | playlist_id, 'Downloading playlist', query=query, headers=self._HEADERS) |
20991253 | 985 | |
2a5c26c9 | 986 | return self._extract_set(data, token) |
2abf7cab | 987 | |
988 | ||
63ccf4ff | 989 | class SoundcloudSearchIE(SoundcloudBaseIE, SearchInfoExtractor): |
2abf7cab | 990 | IE_NAME = 'soundcloud:search' |
96565c7e | 991 | IE_DESC = 'Soundcloud search' |
992 | _SEARCH_KEY = 'scsearch' | |
2abf7cab | 993 | _TESTS = [{ |
994 | 'url': 'scsearch15:post-avant jazzcore', | |
995 | 'info_dict': { | |
c04cc2e2 | 996 | 'id': 'post-avant jazzcore', |
2abf7cab | 997 | 'title': 'post-avant jazzcore', |
998 | }, | |
999 | 'playlist_count': 15, | |
1000 | }] | |
1001 | ||
328a22e1 | 1002 | _MAX_RESULTS_PER_PAGE = 200 |
1003 | _DEFAULT_RESULTS_PER_PAGE = 50 | |
2abf7cab | 1004 | |
1005 | def _get_collection(self, endpoint, collection_id, **query): | |
a3372437 | 1006 | limit = min( |
328a22e1 | 1007 | query.get('limit', self._DEFAULT_RESULTS_PER_PAGE), |
1008 | self._MAX_RESULTS_PER_PAGE) | |
548c3957 RA |
1009 | query.update({ |
1010 | 'limit': limit, | |
548c3957 RA |
1011 | 'linked_partitioning': 1, |
1012 | 'offset': 0, | |
1013 | }) | |
1014 | next_url = update_url_query(self._API_V2_BASE + endpoint, query) | |
2abf7cab | 1015 | |
f6c903e7 | 1016 | for i in itertools.count(1): |
e53e56b7 | 1017 | response = self._call_api( |
cc16383f | 1018 | next_url, collection_id, f'Downloading page {i}', |
fb4126a1 | 1019 | 'Unable to download API page', headers=self._HEADERS) |
2abf7cab | 1020 | |
cc16383f | 1021 | for item in response.get('collection') or []: |
1022 | if item: | |
c04cc2e2 KAW |
1023 | yield self.url_result( |
1024 | item['uri'], SoundcloudIE.ie_key(), **self._extract_info_dict(item, extract_flat=True)) | |
2abf7cab | 1025 | |
7e347275 | 1026 | next_url = response.get('next_href') |
f6c903e7 S |
1027 | if not next_url: |
1028 | break | |
2abf7cab | 1029 | |
1030 | def _get_n_results(self, query, n): | |
8863c8f0 | 1031 | return self.playlist_result(itertools.islice( |
1032 | self._get_collection('search/tracks', query, limit=n, q=query), | |
1033 | 0, None if n == float('inf') else n), query, query) |