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