]>
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) | |
299 | self._sort_formats(formats) | |
300 | ||
301 | user = info.get('user') or {} | |
302 | ||
303 | thumbnails = [] | |
304 | artwork_url = info.get('artwork_url') | |
305 | thumbnail = artwork_url or user.get('avatar_url') | |
306 | if isinstance(thumbnail, compat_str): | |
307 | if re.search(self._IMAGE_REPL_RE, thumbnail): | |
308 | for image_id, size in self._ARTWORK_MAP.items(): | |
309 | i = { | |
310 | 'id': image_id, | |
311 | 'url': re.sub(self._IMAGE_REPL_RE, '-%s.jpg' % image_id, thumbnail), | |
312 | } | |
313 | if image_id == 'tiny' and not artwork_url: | |
314 | size = 18 | |
315 | elif image_id == 'original': | |
316 | i['preference'] = 10 | |
317 | if size: | |
318 | i.update({ | |
319 | 'width': size, | |
320 | 'height': size, | |
321 | }) | |
322 | thumbnails.append(i) | |
323 | else: | |
324 | thumbnails = [{'url': thumbnail}] | |
325 | ||
326 | def extract_count(key): | |
327 | return int_or_none(info.get('%s_count' % key)) | |
328 | ||
329 | return { | |
330 | 'id': track_id, | |
331 | 'uploader': user.get('username'), | |
332 | 'uploader_id': str_or_none(user.get('id')) or user.get('permalink'), | |
333 | 'uploader_url': user.get('permalink_url'), | |
334 | 'timestamp': unified_timestamp(info.get('created_at')), | |
335 | 'title': title, | |
336 | 'description': info.get('description'), | |
337 | 'thumbnails': thumbnails, | |
338 | 'duration': float_or_none(info.get('duration'), 1000), | |
339 | 'webpage_url': info.get('permalink_url'), | |
340 | 'license': info.get('license'), | |
341 | 'view_count': extract_count('playback'), | |
342 | 'like_count': extract_count('favoritings') or extract_count('likes'), | |
343 | 'comment_count': extract_count('comment'), | |
344 | 'repost_count': extract_count('reposts'), | |
345 | 'genre': info.get('genre'), | |
346 | 'formats': formats if not extract_flat else None | |
347 | } | |
348 | ||
63ccf4ff | 349 | @classmethod |
350 | def _resolv_url(cls, url): | |
351 | return cls._API_V2_BASE + 'resolve?url=' + url | |
352 | ||
353 | ||
354 | class SoundcloudIE(SoundcloudBaseIE): | |
aad0d6d5 PH |
355 | """Information extractor for soundcloud.com |
356 | To access the media, the uid of the song and a stream token | |
357 | must be extracted from the page source and the script must make | |
358 | a request to media.soundcloud.com/crossdomain.xml. Then | |
359 | the media can be grabbed by requesting from an url composed | |
360 | of the stream token and uid | |
361 | """ | |
362 | ||
20991253 | 363 | _VALID_URL = r'''(?x)^(?:https?://)? |
71507a11 | 364 | (?:(?:(?:www\.|m\.)?soundcloud\.com/ |
836ef264 | 365 | (?!stations/track) |
4ff50ef8 | 366 | (?P<uploader>[\w\d-]+)/ |
3ef2da2d | 367 | (?!(?:tracks|albums|sets(?:/.+?)?|reposts|likes|spotlight)/?(?:$|[?#])) |
c2dedf12 L |
368 | (?P<title>[\w\d-]+) |
369 | (?:/(?P<token>(?!(?:albums|sets|recommended))[^?]+?))? | |
370 | (?:[?].*)?$) | |
548c3957 | 371 | |(?:api(?:-v2)?\.soundcloud\.com/tracks/(?P<track_id>\d+) |
0403b069 | 372 | (?:/?\?secret_token=(?P<secret_token>[^&]+))?) |
eb6a41ba JMF |
373 | ) |
374 | ''' | |
fbcd7b5f | 375 | IE_NAME = 'soundcloud' |
12c167c8 JMF |
376 | _TESTS = [ |
377 | { | |
fbcd7b5f | 378 | 'url': 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy', |
fbcd7b5f PH |
379 | 'md5': 'ebef0a451b909710ed1d7787dddbf0d7', |
380 | 'info_dict': { | |
0eb9fb9f JMF |
381 | 'id': '62986583', |
382 | 'ext': 'mp3', | |
f516f440 | 383 | 'title': 'Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1', |
0eb9fb9f JMF |
384 | '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', |
385 | 'uploader': 'E.T. ExTerrestrial Music', | |
548c3957 | 386 | 'uploader_id': '1571244', |
f516f440 S |
387 | 'timestamp': 1349920598, |
388 | 'upload_date': '20121011', | |
e09965d5 | 389 | 'duration': 143.216, |
4bfd294e | 390 | 'license': 'all-rights-reserved', |
f516f440 S |
391 | 'view_count': int, |
392 | 'like_count': int, | |
393 | 'comment_count': int, | |
394 | 'repost_count': int, | |
12c167c8 JMF |
395 | } |
396 | }, | |
a6c5859d | 397 | # geo-restricted |
12c167c8 | 398 | { |
fbcd7b5f PH |
399 | 'url': 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep', |
400 | 'info_dict': { | |
401 | 'id': '47127627', | |
402 | 'ext': 'mp3', | |
403 | 'title': 'Goldrushed', | |
63ad0315 | 404 | 'description': 'From Stockholm Sweden\r\nPovel / Magnus / Filip / David\r\nwww.theroyalconcept.com', |
fbcd7b5f | 405 | 'uploader': 'The Royal Concept', |
548c3957 | 406 | 'uploader_id': '9615865', |
f516f440 | 407 | 'timestamp': 1337635207, |
fbcd7b5f | 408 | 'upload_date': '20120521', |
a6c5859d | 409 | 'duration': 227.155, |
4bfd294e | 410 | 'license': 'all-rights-reserved', |
f516f440 S |
411 | 'view_count': int, |
412 | 'like_count': int, | |
413 | 'comment_count': int, | |
414 | 'repost_count': int, | |
12c167c8 | 415 | }, |
12c167c8 | 416 | }, |
de2dd4c5 JMF |
417 | # private link |
418 | { | |
7a5c1cfe | 419 | 'url': 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp', |
fbcd7b5f PH |
420 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', |
421 | 'info_dict': { | |
422 | 'id': '123998367', | |
423 | 'ext': 'mp3', | |
424 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
fbcd7b5f | 425 | 'description': 'test chars: \"\'/\\ä↭', |
f516f440 | 426 | 'uploader': 'jaimeMF', |
548c3957 | 427 | 'uploader_id': '69767071', |
f516f440 | 428 | 'timestamp': 1386604920, |
fbcd7b5f | 429 | 'upload_date': '20131209', |
e09965d5 | 430 | 'duration': 9.927, |
4bfd294e | 431 | 'license': 'all-rights-reserved', |
f516f440 S |
432 | 'view_count': int, |
433 | 'like_count': int, | |
434 | 'comment_count': int, | |
435 | 'repost_count': int, | |
de2dd4c5 JMF |
436 | }, |
437 | }, | |
9296738f | 438 | # private link (alt format) |
439 | { | |
440 | 'url': 'https://api.soundcloud.com/tracks/123998367?secret_token=s-8Pjrp', | |
441 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
442 | 'info_dict': { | |
443 | 'id': '123998367', | |
444 | 'ext': 'mp3', | |
445 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
9296738f | 446 | 'description': 'test chars: \"\'/\\ä↭', |
f516f440 | 447 | 'uploader': 'jaimeMF', |
548c3957 | 448 | 'uploader_id': '69767071', |
f516f440 | 449 | 'timestamp': 1386604920, |
9296738f | 450 | 'upload_date': '20131209', |
e09965d5 | 451 | 'duration': 9.927, |
4bfd294e | 452 | 'license': 'all-rights-reserved', |
f516f440 S |
453 | 'view_count': int, |
454 | 'like_count': int, | |
455 | 'comment_count': int, | |
456 | 'repost_count': int, | |
9296738f | 457 | }, |
458 | }, | |
f67ca84d JMF |
459 | # downloadable song |
460 | { | |
be05d5cf TOH |
461 | 'url': 'https://soundcloud.com/the80m/the-following', |
462 | 'md5': '9ffcddb08c87d74fb5808a3c183a1d04', | |
fbcd7b5f | 463 | 'info_dict': { |
be05d5cf TOH |
464 | 'id': '343609555', |
465 | 'ext': 'wav', | |
f67ca84d JMF |
466 | }, |
467 | }, | |
d7c7100e S |
468 | # private link, downloadable format |
469 | { | |
470 | 'url': 'https://soundcloud.com/oriuplift/uponly-238-no-talking-wav/s-AyZUd', | |
471 | 'md5': '64a60b16e617d41d0bef032b7f55441e', | |
472 | 'info_dict': { | |
473 | 'id': '340344461', | |
474 | 'ext': 'wav', | |
475 | 'title': 'Uplifting Only 238 [No Talking] (incl. Alex Feed Guestmix) (Aug 31, 2017) [wav]', | |
476 | 'description': 'md5:fa20ee0fca76a3d6df8c7e57f3715366', | |
477 | 'uploader': 'Ori Uplift Music', | |
548c3957 | 478 | 'uploader_id': '12563093', |
f516f440 | 479 | 'timestamp': 1504206263, |
d7c7100e | 480 | 'upload_date': '20170831', |
e09965d5 | 481 | 'duration': 7449.096, |
d7c7100e | 482 | 'license': 'all-rights-reserved', |
f516f440 S |
483 | 'view_count': int, |
484 | 'like_count': int, | |
485 | 'comment_count': int, | |
486 | 'repost_count': int, | |
d7c7100e S |
487 | }, |
488 | }, | |
0b0870f9 PV |
489 | # no album art, use avatar pic for thumbnail |
490 | { | |
491 | 'url': 'https://soundcloud.com/garyvee/sideways-prod-mad-real', | |
492 | 'md5': '59c7872bc44e5d99b7211891664760c2', | |
493 | 'info_dict': { | |
494 | 'id': '309699954', | |
495 | 'ext': 'mp3', | |
496 | 'title': 'Sideways (Prod. Mad Real)', | |
497 | 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e', | |
498 | 'uploader': 'garyvee', | |
548c3957 | 499 | 'uploader_id': '2366352', |
f516f440 | 500 | 'timestamp': 1488152409, |
0b0870f9 | 501 | 'upload_date': '20170226', |
e09965d5 | 502 | 'duration': 207.012, |
0b0870f9 PV |
503 | 'thumbnail': r're:https?://.*\.jpg', |
504 | 'license': 'all-rights-reserved', | |
f516f440 S |
505 | 'view_count': int, |
506 | 'like_count': int, | |
507 | 'comment_count': int, | |
508 | 'repost_count': int, | |
0b0870f9 PV |
509 | }, |
510 | 'params': { | |
511 | 'skip_download': True, | |
512 | }, | |
513 | }, | |
e09965d5 S |
514 | { |
515 | 'url': 'https://soundcloud.com/giovannisarani/mezzo-valzer', | |
516 | 'md5': 'e22aecd2bc88e0e4e432d7dcc0a1abf7', | |
517 | 'info_dict': { | |
518 | 'id': '583011102', | |
519 | 'ext': 'mp3', | |
520 | 'title': 'Mezzo Valzer', | |
521 | 'description': 'md5:4138d582f81866a530317bae316e8b61', | |
2a5c26c9 | 522 | 'uploader': 'Micronie', |
548c3957 | 523 | 'uploader_id': '3352531', |
e09965d5 S |
524 | 'timestamp': 1551394171, |
525 | 'upload_date': '20190228', | |
526 | 'duration': 180.157, | |
527 | 'thumbnail': r're:https?://.*\.jpg', | |
528 | 'license': 'all-rights-reserved', | |
529 | 'view_count': int, | |
530 | 'like_count': int, | |
531 | 'comment_count': int, | |
532 | 'repost_count': int, | |
533 | }, | |
75294a5e S |
534 | }, |
535 | { | |
bc842c27 | 536 | # AAC HQ format available (account with active subscription needed) |
75294a5e S |
537 | 'url': 'https://soundcloud.com/wandw/the-chainsmokers-ft-daya-dont-let-me-down-ww-remix-1', |
538 | 'only_matching': True, | |
539 | }, | |
bc842c27 U |
540 | { |
541 | # Go+ (account with active subscription needed) | |
542 | 'url': 'https://soundcloud.com/taylorswiftofficial/look-what-you-made-me-do', | |
543 | 'only_matching': True, | |
544 | }, | |
12c167c8 | 545 | ] |
aad0d6d5 | 546 | |
aad0d6d5 | 547 | def _real_extract(self, url): |
5ad28e7f | 548 | mobj = self._match_valid_url(url) |
aad0d6d5 | 549 | |
eb6a41ba | 550 | track_id = mobj.group('track_id') |
4bfd294e | 551 | |
3bed6217 | 552 | query = {} |
548c3957 RA |
553 | if track_id: |
554 | info_json_url = self._API_V2_BASE + 'tracks/' + track_id | |
eb6a41ba | 555 | full_title = track_id |
9296738f | 556 | token = mobj.group('secret_token') |
557 | if token: | |
548c3957 | 558 | query['secret_token'] = token |
eb6a41ba | 559 | else: |
548c3957 | 560 | full_title = resolve_title = '%s/%s' % mobj.group('uploader', 'title') |
de2dd4c5 | 561 | token = mobj.group('token') |
de2dd4c5 JMF |
562 | if token: |
563 | resolve_title += '/%s' % token | |
548c3957 | 564 | info_json_url = self._resolv_url(self._BASE_URL + resolve_title) |
5f6a1245 | 565 | |
e09965d5 | 566 | info = self._download_json( |
fb4126a1 | 567 | info_json_url, full_title, 'Downloading info JSON', query=query, headers=self._HEADERS) |
a6c5859d RA |
568 | |
569 | return self._extract_info_dict(info, full_title, token) | |
aad0d6d5 | 570 | |
20991253 | 571 | |
63ccf4ff | 572 | class SoundcloudPlaylistBaseIE(SoundcloudBaseIE): |
2a5c26c9 RA |
573 | def _extract_set(self, playlist, token=None): |
574 | playlist_id = compat_str(playlist['id']) | |
575 | tracks = playlist.get('tracks') or [] | |
576 | if not all([t.get('permalink_url') for t in tracks]) and token: | |
577 | tracks = self._download_json( | |
578 | self._API_V2_BASE + 'tracks', playlist_id, | |
579 | 'Downloading tracks', query={ | |
580 | 'ids': ','.join([compat_str(t['id']) for t in tracks]), | |
581 | 'playlistId': playlist_id, | |
582 | 'playlistSecretToken': token, | |
fb4126a1 | 583 | }, headers=self._HEADERS) |
548c3957 RA |
584 | entries = [] |
585 | for track in tracks: | |
586 | track_id = str_or_none(track.get('id')) | |
587 | url = track.get('permalink_url') | |
588 | if not url: | |
589 | if not track_id: | |
590 | continue | |
591 | url = self._API_V2_BASE + 'tracks/' + track_id | |
592 | if token: | |
593 | url += '?secret_token=' + token | |
594 | entries.append(self.url_result( | |
595 | url, SoundcloudIE.ie_key(), track_id)) | |
2a5c26c9 RA |
596 | return self.playlist_result( |
597 | entries, playlist_id, | |
598 | playlist.get('title'), | |
599 | playlist.get('description')) | |
8e45e1cc S |
600 | |
601 | ||
7518a61d | 602 | class SoundcloudSetIE(SoundcloudPlaylistBaseIE): |
906f980a | 603 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<uploader>[\w\d-]+)/sets/(?P<slug_title>[:\w\d-]+)(?:/(?P<token>[^?/]+))?' |
fbcd7b5f | 604 | IE_NAME = 'soundcloud:set' |
22a6f150 PH |
605 | _TESTS = [{ |
606 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep', | |
607 | 'info_dict': { | |
a9551e90 | 608 | 'id': '2284613', |
22a6f150 | 609 | 'title': 'The Royal Concept EP', |
2a5c26c9 | 610 | 'description': 'md5:71d07087c7a449e8941a70a29e34671e', |
22a6f150 | 611 | }, |
bf2dc9cc | 612 | 'playlist_mincount': 5, |
f7043ef3 S |
613 | }, { |
614 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep/token', | |
615 | 'only_matching': True, | |
906f980a U |
616 | }, { |
617 | 'url': 'https://soundcloud.com/discover/sets/weekly::flacmatic', | |
618 | 'only_matching': True, | |
619 | }, { | |
620 | 'url': 'https://soundcloud.com/discover/sets/charts-top:all-music:de', | |
621 | 'only_matching': True, | |
622 | }, { | |
623 | 'url': 'https://soundcloud.com/discover/sets/charts-top:hiphoprap:kr', | |
624 | 'only_matching': True, | |
22a6f150 | 625 | }] |
aad0d6d5 | 626 | |
aad0d6d5 | 627 | def _real_extract(self, url): |
5ad28e7f | 628 | mobj = self._match_valid_url(url) |
aad0d6d5 | 629 | |
548c3957 | 630 | full_title = '%s/sets/%s' % mobj.group('uploader', 'slug_title') |
2f834e93 | 631 | token = mobj.group('token') |
632 | if token: | |
633 | full_title += '/' + token | |
aad0d6d5 | 634 | |
548c3957 | 635 | info = self._download_json(self._resolv_url( |
fb4126a1 | 636 | self._BASE_URL + full_title), full_title, headers=self._HEADERS) |
aad0d6d5 | 637 | |
aad0d6d5 | 638 | if 'errors' in info: |
214e74bf JMF |
639 | msgs = (compat_str(err['error_message']) for err in info['errors']) |
640 | raise ExtractorError('unable to download video webpage: %s' % ','.join(msgs)) | |
aad0d6d5 | 641 | |
2a5c26c9 | 642 | return self._extract_set(info, token) |
92790f4e JMF |
643 | |
644 | ||
63ccf4ff | 645 | class SoundcloudPagedPlaylistBaseIE(SoundcloudBaseIE): |
836ef264 | 646 | def _extract_playlist(self, base_url, playlist_id, playlist_title): |
aa6c2530 | 647 | return { |
648 | '_type': 'playlist', | |
649 | 'id': playlist_id, | |
650 | 'title': playlist_title, | |
651 | 'entries': self._entries(base_url, playlist_id), | |
652 | } | |
653 | ||
e04a1ff9 | 654 | def _entries(self, url, playlist_id): |
a0566bbf | 655 | # Per the SoundCloud documentation, the maximum limit for a linked partitioning query is 200. |
aa272535 | 656 | # https://developers.soundcloud.com/blog/offset-pagination-deprecated |
aa6c2530 | 657 | query = { |
38970916 | 658 | 'limit': 200, |
836ef264 | 659 | 'linked_partitioning': '1', |
aa6c2530 | 660 | 'offset': 0, |
836ef264 S |
661 | } |
662 | ||
836ef264 | 663 | for i in itertools.count(): |
be5c1ae8 | 664 | for retry in self.RetryManager(): |
e04a1ff9 | 665 | try: |
666 | response = self._download_json( | |
667 | url, playlist_id, query=query, headers=self._HEADERS, | |
be5c1ae8 | 668 | note=f'Downloading track page {i + 1}') |
e04a1ff9 | 669 | break |
670 | except ExtractorError as e: | |
671 | # Downloading page may result in intermittent 502 HTTP error | |
672 | # See https://github.com/yt-dlp/yt-dlp/issues/872 | |
be5c1ae8 | 673 | if not isinstance(e.cause, compat_HTTPError) or e.cause.code != 502: |
e04a1ff9 | 674 | raise |
be5c1ae8 | 675 | retry.error = e |
676 | continue | |
836ef264 | 677 | |
aa6c2530 | 678 | def resolve_entry(*candidates): |
7c5307f4 S |
679 | for cand in candidates: |
680 | if not isinstance(cand, dict): | |
681 | continue | |
682 | permalink_url = url_or_none(cand.get('permalink_url')) | |
aa6c2530 | 683 | if permalink_url: |
684 | return self.url_result( | |
685 | permalink_url, | |
686 | SoundcloudIE.ie_key() if SoundcloudIE.suitable(permalink_url) else None, | |
687 | str_or_none(cand.get('id')), cand.get('title')) | |
836ef264 | 688 | |
aa6c2530 | 689 | for e in response['collection'] or []: |
690 | yield resolve_entry(e, e.get('track'), e.get('playlist')) | |
836ef264 | 691 | |
e04a1ff9 | 692 | url = response.get('next_href') |
644149af | 693 | if not url: |
694 | break | |
aa6c2530 | 695 | query.pop('offset', None) |
836ef264 | 696 | |
836ef264 | 697 | |
836ef264 | 698 | class SoundcloudUserIE(SoundcloudPagedPlaylistBaseIE): |
16a08978 S |
699 | _VALID_URL = r'''(?x) |
700 | https?:// | |
701 | (?:(?:www|m)\.)?soundcloud\.com/ | |
702 | (?P<user>[^/]+) | |
703 | (?:/ | |
3ef2da2d | 704 | (?P<rsrc>tracks|albums|sets|reposts|likes|spotlight) |
16a08978 S |
705 | )? |
706 | /?(?:[?#].*)?$ | |
707 | ''' | |
fbcd7b5f | 708 | IE_NAME = 'soundcloud:user' |
22a6f150 | 709 | _TESTS = [{ |
b6423e6c | 710 | 'url': 'https://soundcloud.com/soft-cell-official', |
22a6f150 | 711 | 'info_dict': { |
b6423e6c S |
712 | 'id': '207965082', |
713 | 'title': 'Soft Cell (All)', | |
22a6f150 | 714 | }, |
b6423e6c | 715 | 'playlist_mincount': 28, |
22a6f150 | 716 | }, { |
b6423e6c | 717 | 'url': 'https://soundcloud.com/soft-cell-official/tracks', |
22a6f150 | 718 | 'info_dict': { |
b6423e6c S |
719 | 'id': '207965082', |
720 | 'title': 'Soft Cell (Tracks)', | |
22a6f150 | 721 | }, |
b6423e6c | 722 | 'playlist_mincount': 27, |
03b9c944 | 723 | }, { |
b6423e6c S |
724 | 'url': 'https://soundcloud.com/soft-cell-official/albums', |
725 | 'info_dict': { | |
726 | 'id': '207965082', | |
727 | 'title': 'Soft Cell (Albums)', | |
728 | }, | |
729 | 'playlist_mincount': 1, | |
730 | }, { | |
731 | 'url': 'https://soundcloud.com/jcv246/sets', | |
80fb6d4a | 732 | 'info_dict': { |
b6423e6c | 733 | 'id': '12982173', |
548c3957 | 734 | 'title': 'Jordi / cv (Sets)', |
80fb6d4a | 735 | }, |
8e45e1cc | 736 | 'playlist_mincount': 2, |
80fb6d4a | 737 | }, { |
b6423e6c | 738 | 'url': 'https://soundcloud.com/jcv246/reposts', |
80fb6d4a | 739 | 'info_dict': { |
b6423e6c S |
740 | 'id': '12982173', |
741 | 'title': 'Jordi / cv (Reposts)', | |
80fb6d4a | 742 | }, |
b6423e6c | 743 | 'playlist_mincount': 6, |
80fb6d4a | 744 | }, { |
b6423e6c | 745 | 'url': 'https://soundcloud.com/clalberg/likes', |
80fb6d4a | 746 | 'info_dict': { |
b6423e6c S |
747 | 'id': '11817582', |
748 | 'title': 'clalberg (Likes)', | |
80fb6d4a | 749 | }, |
b6423e6c | 750 | 'playlist_mincount': 5, |
80fb6d4a S |
751 | }, { |
752 | 'url': 'https://soundcloud.com/grynpyret/spotlight', | |
753 | 'info_dict': { | |
754 | 'id': '7098329', | |
bf2dc9cc | 755 | 'title': 'Grynpyret (Spotlight)', |
80fb6d4a S |
756 | }, |
757 | 'playlist_mincount': 1, | |
22a6f150 | 758 | }] |
92790f4e | 759 | |
80fb6d4a | 760 | _BASE_URL_MAP = { |
548c3957 RA |
761 | 'all': 'stream/users/%s', |
762 | 'tracks': 'users/%s/tracks', | |
763 | 'albums': 'users/%s/albums', | |
764 | 'sets': 'users/%s/playlists', | |
765 | 'reposts': 'stream/users/%s/reposts', | |
766 | 'likes': 'users/%s/likes', | |
767 | 'spotlight': 'users/%s/spotlight', | |
80fb6d4a S |
768 | } |
769 | ||
92790f4e | 770 | def _real_extract(self, url): |
5ad28e7f | 771 | mobj = self._match_valid_url(url) |
92790f4e JMF |
772 | uploader = mobj.group('user') |
773 | ||
20991253 | 774 | user = self._download_json( |
548c3957 | 775 | self._resolv_url(self._BASE_URL + uploader), |
fb4126a1 | 776 | uploader, 'Downloading user info', headers=self._HEADERS) |
80fb6d4a S |
777 | |
778 | resource = mobj.group('rsrc') or 'all' | |
80fb6d4a | 779 | |
836ef264 | 780 | return self._extract_playlist( |
548c3957 RA |
781 | self._API_V2_BASE + self._BASE_URL_MAP[resource] % user['id'], |
782 | str_or_none(user.get('id')), | |
783 | '%s (%s)' % (user['username'], resource.capitalize())) | |
97afd99a | 784 | |
92790f4e | 785 | |
836ef264 S |
786 | class SoundcloudTrackStationIE(SoundcloudPagedPlaylistBaseIE): |
787 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/stations/track/[^/]+/(?P<id>[^/?#&]+)' | |
788 | IE_NAME = 'soundcloud:trackstation' | |
789 | _TESTS = [{ | |
790 | 'url': 'https://soundcloud.com/stations/track/officialsundial/your-text', | |
791 | 'info_dict': { | |
792 | 'id': '286017854', | |
548c3957 | 793 | 'title': 'Track station: your text', |
836ef264 S |
794 | }, |
795 | 'playlist_mincount': 47, | |
796 | }] | |
80fb6d4a | 797 | |
836ef264 S |
798 | def _real_extract(self, url): |
799 | track_name = self._match_id(url) | |
80fb6d4a | 800 | |
fb4126a1 | 801 | track = self._download_json(self._resolv_url(url), track_name, headers=self._HEADERS) |
836ef264 | 802 | track_id = self._search_regex( |
548c3957 | 803 | r'soundcloud:track-stations:(\d+)', track['id'], 'track id') |
92790f4e | 804 | |
836ef264 | 805 | return self._extract_playlist( |
548c3957 RA |
806 | self._API_V2_BASE + 'stations/%s/tracks' % track['id'], |
807 | track_id, 'Track station: %s' % track['title']) | |
20991253 PH |
808 | |
809 | ||
c2dedf12 L |
810 | class SoundcloudRelatedIE(SoundcloudPagedPlaylistBaseIE): |
811 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<slug>[\w\d-]+/[\w\d-]+)/(?P<relation>albums|sets|recommended)' | |
812 | IE_NAME = 'soundcloud:related' | |
813 | _TESTS = [{ | |
814 | 'url': 'https://soundcloud.com/wajang/sexapil-pingers-5/recommended', | |
815 | 'info_dict': { | |
816 | 'id': '1084577272', | |
817 | 'title': 'Sexapil - Pingers 5 (Recommended)', | |
818 | }, | |
819 | 'playlist_mincount': 50, | |
820 | }, { | |
821 | 'url': 'https://soundcloud.com/wajang/sexapil-pingers-5/albums', | |
822 | 'info_dict': { | |
823 | 'id': '1084577272', | |
824 | 'title': 'Sexapil - Pingers 5 (Albums)', | |
825 | }, | |
826 | 'playlist_mincount': 1, | |
827 | }, { | |
828 | 'url': 'https://soundcloud.com/wajang/sexapil-pingers-5/sets', | |
829 | 'info_dict': { | |
830 | 'id': '1084577272', | |
831 | 'title': 'Sexapil - Pingers 5 (Sets)', | |
832 | }, | |
833 | 'playlist_mincount': 4, | |
834 | }] | |
835 | ||
836 | _BASE_URL_MAP = { | |
837 | 'albums': 'tracks/%s/albums', | |
838 | 'sets': 'tracks/%s/playlists_without_albums', | |
839 | 'recommended': 'tracks/%s/related', | |
840 | } | |
841 | ||
842 | def _real_extract(self, url): | |
843 | slug, relation = self._match_valid_url(url).group('slug', 'relation') | |
844 | ||
845 | track = self._download_json( | |
846 | self._resolv_url(self._BASE_URL + slug), | |
847 | slug, 'Downloading track info', headers=self._HEADERS) | |
848 | ||
849 | if track.get('errors'): | |
850 | raise ExtractorError(f'{self.IE_NAME} said: %s' % ','.join( | |
851 | str(err['error_message']) for err in track['errors']), expected=True) | |
852 | ||
853 | return self._extract_playlist( | |
854 | self._API_V2_BASE + self._BASE_URL_MAP[relation] % track['id'], str(track['id']), | |
855 | '%s (%s)' % (track.get('title') or slug, relation.capitalize())) | |
856 | ||
857 | ||
7518a61d | 858 | class SoundcloudPlaylistIE(SoundcloudPlaylistBaseIE): |
548c3957 | 859 | _VALID_URL = r'https?://api(?:-v2)?\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$' |
20991253 | 860 | IE_NAME = 'soundcloud:playlist' |
46f74bcf | 861 | _TESTS = [{ |
f1c05100 | 862 | 'url': 'https://api.soundcloud.com/playlists/4110309', |
46f74bcf PH |
863 | 'info_dict': { |
864 | 'id': '4110309', | |
865 | 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]', | |
866 | 'description': 're:.*?TILT Brass - Bowery Poetry Club', | |
867 | }, | |
868 | 'playlist_count': 6, | |
869 | }] | |
20991253 PH |
870 | |
871 | def _real_extract(self, url): | |
5ad28e7f | 872 | mobj = self._match_valid_url(url) |
20991253 | 873 | playlist_id = mobj.group('id') |
20991253 | 874 | |
3bed6217 | 875 | query = {} |
2f834e93 | 876 | token = mobj.group('token') |
2f834e93 | 877 | if token: |
548c3957 | 878 | query['secret_token'] = token |
2f834e93 | 879 | |
20991253 | 880 | data = self._download_json( |
548c3957 | 881 | self._API_V2_BASE + 'playlists/' + playlist_id, |
fb4126a1 | 882 | playlist_id, 'Downloading playlist', query=query, headers=self._HEADERS) |
20991253 | 883 | |
2a5c26c9 | 884 | return self._extract_set(data, token) |
2abf7cab | 885 | |
886 | ||
63ccf4ff | 887 | class SoundcloudSearchIE(SoundcloudBaseIE, SearchInfoExtractor): |
2abf7cab | 888 | IE_NAME = 'soundcloud:search' |
96565c7e | 889 | IE_DESC = 'Soundcloud search' |
890 | _SEARCH_KEY = 'scsearch' | |
2abf7cab | 891 | _TESTS = [{ |
892 | 'url': 'scsearch15:post-avant jazzcore', | |
893 | 'info_dict': { | |
c04cc2e2 | 894 | 'id': 'post-avant jazzcore', |
2abf7cab | 895 | 'title': 'post-avant jazzcore', |
896 | }, | |
897 | 'playlist_count': 15, | |
898 | }] | |
899 | ||
328a22e1 | 900 | _MAX_RESULTS_PER_PAGE = 200 |
901 | _DEFAULT_RESULTS_PER_PAGE = 50 | |
2abf7cab | 902 | |
903 | def _get_collection(self, endpoint, collection_id, **query): | |
a3372437 | 904 | limit = min( |
328a22e1 | 905 | query.get('limit', self._DEFAULT_RESULTS_PER_PAGE), |
906 | self._MAX_RESULTS_PER_PAGE) | |
548c3957 RA |
907 | query.update({ |
908 | 'limit': limit, | |
548c3957 RA |
909 | 'linked_partitioning': 1, |
910 | 'offset': 0, | |
911 | }) | |
912 | next_url = update_url_query(self._API_V2_BASE + endpoint, query) | |
2abf7cab | 913 | |
f6c903e7 | 914 | for i in itertools.count(1): |
7e347275 | 915 | response = self._download_json( |
cc16383f | 916 | next_url, collection_id, f'Downloading page {i}', |
fb4126a1 | 917 | 'Unable to download API page', headers=self._HEADERS) |
2abf7cab | 918 | |
cc16383f | 919 | for item in response.get('collection') or []: |
920 | if item: | |
c04cc2e2 KAW |
921 | yield self.url_result( |
922 | item['uri'], SoundcloudIE.ie_key(), **self._extract_info_dict(item, extract_flat=True)) | |
2abf7cab | 923 | |
7e347275 | 924 | next_url = response.get('next_href') |
f6c903e7 S |
925 | if not next_url: |
926 | break | |
2abf7cab | 927 | |
928 | def _get_n_results(self, query, n): | |
8863c8f0 | 929 | return self.playlist_result(itertools.islice( |
930 | self._get_collection('search/tracks', query, limit=n, q=query), | |
931 | 0, None if n == float('inf') else n), query, query) |