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