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