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