]>
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 |
aad0d6d5 | 6 | |
2abf7cab | 7 | from .common import ( |
8 | InfoExtractor, | |
9 | SearchInfoExtractor | |
10 | ) | |
1cc79574 | 11 | from ..compat import ( |
aad0d6d5 | 12 | compat_str, |
668de34c | 13 | compat_urlparse, |
15707c7e | 14 | compat_urllib_parse_urlencode, |
1cc79574 PH |
15 | ) |
16 | from ..utils import ( | |
aad0d6d5 | 17 | ExtractorError, |
e09965d5 | 18 | float_or_none, |
eb920777 | 19 | int_or_none, |
e09965d5 S |
20 | KNOWN_EXTENSIONS, |
21 | merge_dicts, | |
22 | mimetype2ext, | |
23 | str_or_none, | |
f516f440 S |
24 | try_get, |
25 | unified_timestamp, | |
d7c7100e | 26 | update_url_query, |
7c5307f4 | 27 | url_or_none, |
d7c7100e | 28 | ) |
aad0d6d5 PH |
29 | |
30 | ||
31 | class SoundcloudIE(InfoExtractor): | |
32 | """Information extractor for soundcloud.com | |
33 | To access the media, the uid of the song and a stream token | |
34 | must be extracted from the page source and the script must make | |
35 | a request to media.soundcloud.com/crossdomain.xml. Then | |
36 | the media can be grabbed by requesting from an url composed | |
37 | of the stream token and uid | |
38 | """ | |
39 | ||
20991253 | 40 | _VALID_URL = r'''(?x)^(?:https?://)? |
71507a11 | 41 | (?:(?:(?:www\.|m\.)?soundcloud\.com/ |
836ef264 | 42 | (?!stations/track) |
4ff50ef8 | 43 | (?P<uploader>[\w\d-]+)/ |
3ef2da2d | 44 | (?!(?:tracks|albums|sets(?:/.+?)?|reposts|likes|spotlight)/?(?:$|[?#])) |
22a6f150 | 45 | (?P<title>[\w\d-]+)/? |
de2dd4c5 | 46 | (?P<token>[^?]+?)?(?:[?].*)?$) |
9296738f | 47 | |(?:api\.soundcloud\.com/tracks/(?P<track_id>\d+) |
0403b069 | 48 | (?:/?\?secret_token=(?P<secret_token>[^&]+))?) |
31c1cf5a | 49 | |(?P<player>(?:w|player|p.)\.soundcloud\.com/player/?.*?url=.*) |
eb6a41ba JMF |
50 | ) |
51 | ''' | |
fbcd7b5f | 52 | IE_NAME = 'soundcloud' |
12c167c8 JMF |
53 | _TESTS = [ |
54 | { | |
fbcd7b5f | 55 | 'url': 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy', |
fbcd7b5f PH |
56 | 'md5': 'ebef0a451b909710ed1d7787dddbf0d7', |
57 | 'info_dict': { | |
0eb9fb9f JMF |
58 | 'id': '62986583', |
59 | 'ext': 'mp3', | |
f516f440 | 60 | 'title': 'Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1', |
0eb9fb9f JMF |
61 | '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', |
62 | 'uploader': 'E.T. ExTerrestrial Music', | |
f516f440 S |
63 | 'timestamp': 1349920598, |
64 | 'upload_date': '20121011', | |
e09965d5 | 65 | 'duration': 143.216, |
4bfd294e | 66 | 'license': 'all-rights-reserved', |
f516f440 S |
67 | 'view_count': int, |
68 | 'like_count': int, | |
69 | 'comment_count': int, | |
70 | 'repost_count': int, | |
12c167c8 JMF |
71 | } |
72 | }, | |
73 | # not streamable song | |
74 | { | |
fbcd7b5f PH |
75 | 'url': 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep', |
76 | 'info_dict': { | |
77 | 'id': '47127627', | |
78 | 'ext': 'mp3', | |
79 | 'title': 'Goldrushed', | |
63ad0315 | 80 | 'description': 'From Stockholm Sweden\r\nPovel / Magnus / Filip / David\r\nwww.theroyalconcept.com', |
fbcd7b5f | 81 | 'uploader': 'The Royal Concept', |
f516f440 | 82 | 'timestamp': 1337635207, |
fbcd7b5f | 83 | 'upload_date': '20120521', |
f516f440 | 84 | 'duration': 30, |
4bfd294e | 85 | 'license': 'all-rights-reserved', |
f516f440 S |
86 | 'view_count': int, |
87 | 'like_count': int, | |
88 | 'comment_count': int, | |
89 | 'repost_count': int, | |
12c167c8 | 90 | }, |
fbcd7b5f | 91 | 'params': { |
12c167c8 | 92 | # rtmp |
fbcd7b5f | 93 | 'skip_download': True, |
12c167c8 JMF |
94 | }, |
95 | }, | |
de2dd4c5 JMF |
96 | # private link |
97 | { | |
fbcd7b5f PH |
98 | 'url': 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp', |
99 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
100 | 'info_dict': { | |
101 | 'id': '123998367', | |
102 | 'ext': 'mp3', | |
103 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
fbcd7b5f | 104 | 'description': 'test chars: \"\'/\\ä↭', |
f516f440 S |
105 | 'uploader': 'jaimeMF', |
106 | 'timestamp': 1386604920, | |
fbcd7b5f | 107 | 'upload_date': '20131209', |
e09965d5 | 108 | 'duration': 9.927, |
4bfd294e | 109 | 'license': 'all-rights-reserved', |
f516f440 S |
110 | 'view_count': int, |
111 | 'like_count': int, | |
112 | 'comment_count': int, | |
113 | 'repost_count': int, | |
de2dd4c5 JMF |
114 | }, |
115 | }, | |
9296738f | 116 | # private link (alt format) |
117 | { | |
118 | 'url': 'https://api.soundcloud.com/tracks/123998367?secret_token=s-8Pjrp', | |
119 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
120 | 'info_dict': { | |
121 | 'id': '123998367', | |
122 | 'ext': 'mp3', | |
123 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
9296738f | 124 | 'description': 'test chars: \"\'/\\ä↭', |
f516f440 S |
125 | 'uploader': 'jaimeMF', |
126 | 'timestamp': 1386604920, | |
9296738f | 127 | 'upload_date': '20131209', |
e09965d5 | 128 | 'duration': 9.927, |
4bfd294e | 129 | 'license': 'all-rights-reserved', |
f516f440 S |
130 | 'view_count': int, |
131 | 'like_count': int, | |
132 | 'comment_count': int, | |
133 | 'repost_count': int, | |
9296738f | 134 | }, |
135 | }, | |
f67ca84d JMF |
136 | # downloadable song |
137 | { | |
00a82ea8 | 138 | 'url': 'https://soundcloud.com/oddsamples/bus-brakes', |
eae12e3f | 139 | 'md5': '7624f2351f8a3b2e7cd51522496e7631', |
fbcd7b5f | 140 | 'info_dict': { |
00a82ea8 | 141 | 'id': '128590877', |
eae12e3f | 142 | 'ext': 'mp3', |
00a82ea8 | 143 | 'title': 'Bus Brakes', |
0eb9fb9f | 144 | 'description': 'md5:0053ca6396e8d2fd7b7e1595ef12ab66', |
00a82ea8 | 145 | 'uploader': 'oddsamples', |
f516f440 | 146 | 'timestamp': 1389232924, |
00a82ea8 | 147 | 'upload_date': '20140109', |
e09965d5 | 148 | 'duration': 17.346, |
4bfd294e | 149 | 'license': 'cc-by-sa', |
f516f440 S |
150 | 'view_count': int, |
151 | 'like_count': int, | |
152 | 'comment_count': int, | |
153 | 'repost_count': int, | |
f67ca84d JMF |
154 | }, |
155 | }, | |
d7c7100e S |
156 | # private link, downloadable format |
157 | { | |
158 | 'url': 'https://soundcloud.com/oriuplift/uponly-238-no-talking-wav/s-AyZUd', | |
159 | 'md5': '64a60b16e617d41d0bef032b7f55441e', | |
160 | 'info_dict': { | |
161 | 'id': '340344461', | |
162 | 'ext': 'wav', | |
163 | 'title': 'Uplifting Only 238 [No Talking] (incl. Alex Feed Guestmix) (Aug 31, 2017) [wav]', | |
164 | 'description': 'md5:fa20ee0fca76a3d6df8c7e57f3715366', | |
165 | 'uploader': 'Ori Uplift Music', | |
f516f440 | 166 | 'timestamp': 1504206263, |
d7c7100e | 167 | 'upload_date': '20170831', |
e09965d5 | 168 | 'duration': 7449.096, |
d7c7100e | 169 | 'license': 'all-rights-reserved', |
f516f440 S |
170 | 'view_count': int, |
171 | 'like_count': int, | |
172 | 'comment_count': int, | |
173 | 'repost_count': int, | |
d7c7100e S |
174 | }, |
175 | }, | |
0b0870f9 PV |
176 | # no album art, use avatar pic for thumbnail |
177 | { | |
178 | 'url': 'https://soundcloud.com/garyvee/sideways-prod-mad-real', | |
179 | 'md5': '59c7872bc44e5d99b7211891664760c2', | |
180 | 'info_dict': { | |
181 | 'id': '309699954', | |
182 | 'ext': 'mp3', | |
183 | 'title': 'Sideways (Prod. Mad Real)', | |
184 | 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e', | |
185 | 'uploader': 'garyvee', | |
f516f440 | 186 | 'timestamp': 1488152409, |
0b0870f9 | 187 | 'upload_date': '20170226', |
e09965d5 | 188 | 'duration': 207.012, |
0b0870f9 PV |
189 | 'thumbnail': r're:https?://.*\.jpg', |
190 | 'license': 'all-rights-reserved', | |
f516f440 S |
191 | 'view_count': int, |
192 | 'like_count': int, | |
193 | 'comment_count': int, | |
194 | 'repost_count': int, | |
0b0870f9 PV |
195 | }, |
196 | 'params': { | |
197 | 'skip_download': True, | |
198 | }, | |
199 | }, | |
843ad179 | 200 | # not available via api.soundcloud.com/i1/tracks/id/streams |
e09965d5 S |
201 | { |
202 | 'url': 'https://soundcloud.com/giovannisarani/mezzo-valzer', | |
203 | 'md5': 'e22aecd2bc88e0e4e432d7dcc0a1abf7', | |
204 | 'info_dict': { | |
205 | 'id': '583011102', | |
206 | 'ext': 'mp3', | |
207 | 'title': 'Mezzo Valzer', | |
208 | 'description': 'md5:4138d582f81866a530317bae316e8b61', | |
209 | 'uploader': 'Giovanni Sarani', | |
210 | 'timestamp': 1551394171, | |
211 | 'upload_date': '20190228', | |
212 | 'duration': 180.157, | |
213 | 'thumbnail': r're:https?://.*\.jpg', | |
214 | 'license': 'all-rights-reserved', | |
215 | 'view_count': int, | |
216 | 'like_count': int, | |
217 | 'comment_count': int, | |
218 | 'repost_count': int, | |
219 | }, | |
220 | 'expected_warnings': ['Unable to download JSON metadata'], | |
221 | } | |
12c167c8 | 222 | ] |
aad0d6d5 | 223 | |
c5606802 | 224 | _CLIENT_ID = 'BeGVhOrGmfboy1LtiHTQF6Ejpt9ULJCI' |
7d239269 | 225 | |
fbdf8d15 S |
226 | @staticmethod |
227 | def _extract_urls(webpage): | |
228 | return [m.group('url') for m in re.finditer( | |
229 | r'<iframe[^>]+src=(["\'])(?P<url>(?:https?://)?(?:w\.)?soundcloud\.com/player.+?)\1', | |
230 | webpage)] | |
231 | ||
7d239269 JMF |
232 | @classmethod |
233 | def _resolv_url(cls, url): | |
f1c05100 | 234 | return 'https://api.soundcloud.com/resolve.json?url=' + url + '&client_id=' + cls._CLIENT_ID |
7d239269 | 235 | |
de2dd4c5 | 236 | def _extract_info_dict(self, info, full_title=None, quiet=False, secret_token=None): |
12c167c8 | 237 | track_id = compat_str(info['id']) |
f516f440 | 238 | title = info['title'] |
12c167c8 | 239 | name = full_title or track_id |
2a15e706 | 240 | if quiet: |
92790f4e | 241 | self.report_extraction(name) |
0b0870f9 | 242 | thumbnail = info.get('artwork_url') or info.get('user', {}).get('avatar_url') |
f62a77b9 | 243 | if isinstance(thumbnail, compat_str): |
7d239269 | 244 | thumbnail = thumbnail.replace('-large', '-t500x500') |
f516f440 S |
245 | username = try_get(info, lambda x: x['user']['username'], compat_str) |
246 | ||
247 | def extract_count(key): | |
248 | return int_or_none(info.get('%s_count' % key)) | |
249 | ||
e09965d5 S |
250 | like_count = extract_count('favoritings') |
251 | if like_count is None: | |
252 | like_count = extract_count('likes') | |
253 | ||
12c167c8 | 254 | result = { |
2a15e706 | 255 | 'id': track_id, |
f516f440 S |
256 | 'uploader': username, |
257 | 'timestamp': unified_timestamp(info.get('created_at')), | |
258 | 'title': title, | |
f62a77b9 | 259 | 'description': info.get('description'), |
7d239269 | 260 | 'thumbnail': thumbnail, |
e09965d5 | 261 | 'duration': float_or_none(info.get('duration'), 1000), |
579657ad | 262 | 'webpage_url': info.get('permalink_url'), |
f62a77b9 | 263 | 'license': info.get('license'), |
f516f440 | 264 | 'view_count': extract_count('playback'), |
e09965d5 | 265 | 'like_count': like_count, |
f516f440 S |
266 | 'comment_count': extract_count('comment'), |
267 | 'repost_count': extract_count('reposts'), | |
268 | 'genre': info.get('genre'), | |
7d239269 | 269 | } |
e09965d5 S |
270 | |
271 | format_urls = set() | |
5e114e4b | 272 | formats = [] |
73602bcd TI |
273 | query = {'client_id': self._CLIENT_ID} |
274 | if secret_token is not None: | |
275 | query['secret_token'] = secret_token | |
12c167c8 | 276 | if info.get('downloadable', False): |
64bb5187 | 277 | # We can build a direct link to the song |
73602bcd | 278 | format_url = update_url_query( |
d7c7100e | 279 | 'https://api.soundcloud.com/tracks/%s/download' % track_id, query) |
e09965d5 | 280 | format_urls.add(format_url) |
5e114e4b | 281 | formats.append({ |
2a15e706 | 282 | 'format_id': 'download', |
fbcd7b5f | 283 | 'ext': info.get('original_format', 'mp3'), |
2a15e706 | 284 | 'url': format_url, |
fb04e403 | 285 | 'vcodec': 'none', |
5e114e4b PH |
286 | 'preference': 10, |
287 | }) | |
288 | ||
e09965d5 S |
289 | # Old API, does not work for some tracks (e.g. |
290 | # https://soundcloud.com/giovannisarani/mezzo-valzer) | |
20991253 | 291 | format_dict = self._download_json( |
f1c05100 | 292 | 'https://api.soundcloud.com/i1/tracks/%s/streams' % track_id, |
e09965d5 S |
293 | track_id, 'Downloading track url', query=query, fatal=False) |
294 | ||
295 | if format_dict: | |
296 | for key, stream_url in format_dict.items(): | |
297 | if stream_url in format_urls: | |
298 | continue | |
299 | format_urls.add(stream_url) | |
300 | ext, abr = 'mp3', None | |
301 | mobj = re.search(r'_([^_]+)_(\d+)_url', key) | |
302 | if mobj: | |
303 | ext, abr = mobj.groups() | |
304 | abr = int(abr) | |
305 | if key.startswith('http'): | |
306 | stream_formats = [{ | |
307 | 'format_id': key, | |
308 | 'ext': ext, | |
309 | 'url': stream_url, | |
310 | }] | |
311 | elif key.startswith('rtmp'): | |
312 | # The url doesn't have an rtmp app, we have to extract the playpath | |
313 | url, path = stream_url.split('mp3:', 1) | |
314 | stream_formats = [{ | |
315 | 'format_id': key, | |
316 | 'url': url, | |
317 | 'play_path': 'mp3:' + path, | |
318 | 'ext': 'flv', | |
319 | }] | |
320 | elif key.startswith('hls'): | |
321 | stream_formats = self._extract_m3u8_formats( | |
322 | stream_url, track_id, ext, entry_protocol='m3u8_native', | |
323 | m3u8_id=key, fatal=False) | |
324 | else: | |
325 | continue | |
326 | ||
327 | if abr: | |
328 | for f in stream_formats: | |
329 | f['abr'] = abr | |
330 | ||
331 | formats.extend(stream_formats) | |
332 | ||
333 | # New API | |
334 | transcodings = try_get( | |
335 | info, lambda x: x['media']['transcodings'], list) or [] | |
336 | for t in transcodings: | |
337 | if not isinstance(t, dict): | |
3a194cb4 | 338 | continue |
e09965d5 S |
339 | format_url = url_or_none(t.get('url')) |
340 | if not format_url: | |
341 | continue | |
342 | stream = self._download_json( | |
343 | update_url_query(format_url, query), track_id, fatal=False) | |
344 | if not isinstance(stream, dict): | |
345 | continue | |
346 | stream_url = url_or_none(stream.get('url')) | |
347 | if not stream_url: | |
348 | continue | |
349 | if stream_url in format_urls: | |
350 | continue | |
351 | format_urls.add(stream_url) | |
352 | protocol = try_get(t, lambda x: x['format']['protocol'], compat_str) | |
353 | if protocol != 'hls' and '/hls' in format_url: | |
354 | protocol = 'hls' | |
355 | ext = None | |
356 | preset = str_or_none(t.get('preset')) | |
357 | if preset: | |
358 | ext = preset.split('_')[0] | |
359 | if ext not in KNOWN_EXTENSIONS: | |
360 | mimetype = try_get( | |
361 | t, lambda x: x['format']['mime_type'], compat_str) | |
362 | ext = mimetype2ext(mimetype) or 'mp3' | |
363 | format_id_list = [] | |
364 | if protocol: | |
365 | format_id_list.append(protocol) | |
366 | format_id_list.append(ext) | |
367 | format_id = '_'.join(format_id_list) | |
368 | formats.append({ | |
369 | 'url': stream_url, | |
370 | 'format_id': format_id, | |
371 | 'ext': ext, | |
372 | 'protocol': 'm3u8_native' if protocol == 'hls' else 'http', | |
373 | }) | |
3a194cb4 S |
374 | |
375 | if not formats: | |
376 | # We fallback to the stream_url in the original info, this | |
377 | # cannot be always used, sometimes it can give an HTTP 404 error | |
378 | formats.append({ | |
379 | 'format_id': 'fallback', | |
73602bcd | 380 | 'url': update_url_query(info['stream_url'], query), |
c0fd20ab | 381 | 'ext': 'mp3', |
3a194cb4 | 382 | }) |
e09965d5 | 383 | self._check_formats(formats, track_id) |
3a194cb4 S |
384 | |
385 | for f in formats: | |
386 | f['vcodec'] = 'none' | |
2a15e706 | 387 | |
562ceab1 S |
388 | self._sort_formats(formats) |
389 | result['formats'] = formats | |
64bb5187 | 390 | |
12c167c8 | 391 | return result |
7d239269 | 392 | |
aad0d6d5 | 393 | def _real_extract(self, url): |
eb6a41ba | 394 | mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE) |
aad0d6d5 | 395 | if mobj is None: |
83622b6d | 396 | raise ExtractorError('Invalid URL: %s' % url) |
aad0d6d5 | 397 | |
eb6a41ba | 398 | track_id = mobj.group('track_id') |
e09965d5 | 399 | new_info = {} |
4bfd294e | 400 | |
eb6a41ba | 401 | if track_id is not None: |
f1c05100 | 402 | info_json_url = 'https://api.soundcloud.com/tracks/' + track_id + '.json?client_id=' + self._CLIENT_ID |
eb6a41ba | 403 | full_title = track_id |
9296738f | 404 | token = mobj.group('secret_token') |
405 | if token: | |
611c1dd9 | 406 | info_json_url += '&secret_token=' + token |
31c1cf5a | 407 | elif mobj.group('player'): |
668de34c | 408 | query = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query) |
024ebb27 JMF |
409 | real_url = query['url'][0] |
410 | # If the token is in the query of the original url we have to | |
411 | # manually add it | |
412 | if 'secret_token' in query: | |
413 | real_url += '?secret_token=' + query['secret_token'][0] | |
414 | return self.url_result(real_url) | |
eb6a41ba JMF |
415 | else: |
416 | # extract uploader (which is in the url) | |
de2dd4c5 | 417 | uploader = mobj.group('uploader') |
eb6a41ba | 418 | # extract simple title (uploader + slug of song title) |
8bcc8756 | 419 | slug_title = mobj.group('title') |
de2dd4c5 JMF |
420 | token = mobj.group('token') |
421 | full_title = resolve_title = '%s/%s' % (uploader, slug_title) | |
422 | if token: | |
423 | resolve_title += '/%s' % token | |
5f6a1245 | 424 | |
e09965d5 S |
425 | webpage = self._download_webpage(url, full_title, fatal=False) |
426 | if webpage: | |
427 | entries = self._parse_json( | |
428 | self._search_regex( | |
429 | r'var\s+c\s*=\s*(\[.+?\])\s*,\s*o\s*=Date\b', webpage, | |
430 | 'data', default='[]'), full_title, fatal=False) | |
431 | if entries: | |
432 | for e in entries: | |
433 | if not isinstance(e, dict): | |
434 | continue | |
435 | if e.get('id') != 67: | |
436 | continue | |
437 | data = try_get(e, lambda x: x['data'][0], dict) | |
438 | if data: | |
439 | new_info = data | |
440 | break | |
441 | info_json_url = self._resolv_url( | |
442 | 'https://soundcloud.com/%s' % resolve_title) | |
443 | ||
444 | # Contains some additional info missing from new_info | |
445 | info = self._download_json( | |
446 | info_json_url, full_title, 'Downloading info JSON') | |
447 | ||
448 | return self._extract_info_dict( | |
449 | merge_dicts(info, new_info), full_title, secret_token=token) | |
aad0d6d5 | 450 | |
20991253 | 451 | |
7518a61d | 452 | class SoundcloudPlaylistBaseIE(SoundcloudIE): |
8e45e1cc S |
453 | @staticmethod |
454 | def _extract_id(e): | |
455 | return compat_str(e['id']) if e.get('id') else None | |
456 | ||
457 | def _extract_track_entries(self, tracks): | |
458 | return [ | |
459 | self.url_result( | |
460 | track['permalink_url'], SoundcloudIE.ie_key(), | |
461 | video_id=self._extract_id(track)) | |
462 | for track in tracks if track.get('permalink_url')] | |
463 | ||
464 | ||
7518a61d | 465 | class SoundcloudSetIE(SoundcloudPlaylistBaseIE): |
c808ef81 | 466 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<uploader>[\w\d-]+)/sets/(?P<slug_title>[\w\d-]+)(?:/(?P<token>[^?/]+))?' |
fbcd7b5f | 467 | IE_NAME = 'soundcloud:set' |
22a6f150 PH |
468 | _TESTS = [{ |
469 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep', | |
470 | 'info_dict': { | |
a9551e90 | 471 | 'id': '2284613', |
22a6f150 PH |
472 | 'title': 'The Royal Concept EP', |
473 | }, | |
bf2dc9cc | 474 | 'playlist_mincount': 5, |
f7043ef3 S |
475 | }, { |
476 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep/token', | |
477 | 'only_matching': True, | |
22a6f150 | 478 | }] |
aad0d6d5 | 479 | |
aad0d6d5 PH |
480 | def _real_extract(self, url): |
481 | mobj = re.match(self._VALID_URL, url) | |
aad0d6d5 PH |
482 | |
483 | # extract uploader (which is in the url) | |
2f834e93 | 484 | uploader = mobj.group('uploader') |
aad0d6d5 | 485 | # extract simple title (uploader + slug of song title) |
2f834e93 | 486 | slug_title = mobj.group('slug_title') |
aad0d6d5 | 487 | full_title = '%s/sets/%s' % (uploader, slug_title) |
f1c05100 | 488 | url = 'https://soundcloud.com/%s/sets/%s' % (uploader, slug_title) |
2f834e93 | 489 | |
490 | token = mobj.group('token') | |
491 | if token: | |
492 | full_title += '/' + token | |
493 | url += '/' + token | |
aad0d6d5 | 494 | |
7d239269 | 495 | resolv_url = self._resolv_url(url) |
20991253 | 496 | info = self._download_json(resolv_url, full_title) |
aad0d6d5 | 497 | |
aad0d6d5 | 498 | if 'errors' in info: |
214e74bf JMF |
499 | msgs = (compat_str(err['error_message']) for err in info['errors']) |
500 | raise ExtractorError('unable to download video webpage: %s' % ','.join(msgs)) | |
aad0d6d5 | 501 | |
8e45e1cc | 502 | entries = self._extract_track_entries(info['tracks']) |
b14fa8e6 | 503 | |
22a6f150 PH |
504 | return { |
505 | '_type': 'playlist', | |
b14fa8e6 | 506 | 'entries': entries, |
a9551e90 | 507 | 'id': '%s' % info['id'], |
22a6f150 PH |
508 | 'title': info['title'], |
509 | } | |
92790f4e JMF |
510 | |
511 | ||
836ef264 | 512 | class SoundcloudPagedPlaylistBaseIE(SoundcloudPlaylistBaseIE): |
836ef264 S |
513 | _API_V2_BASE = 'https://api-v2.soundcloud.com' |
514 | ||
515 | def _extract_playlist(self, base_url, playlist_id, playlist_title): | |
516 | COMMON_QUERY = { | |
517 | 'limit': 50, | |
518 | 'client_id': self._CLIENT_ID, | |
519 | 'linked_partitioning': '1', | |
520 | } | |
521 | ||
522 | query = COMMON_QUERY.copy() | |
523 | query['offset'] = 0 | |
524 | ||
525 | next_href = base_url + '?' + compat_urllib_parse_urlencode(query) | |
526 | ||
527 | entries = [] | |
528 | for i in itertools.count(): | |
529 | response = self._download_json( | |
530 | next_href, playlist_id, 'Downloading track page %s' % (i + 1)) | |
531 | ||
532 | collection = response['collection'] | |
3ef2da2d S |
533 | |
534 | if not isinstance(collection, list): | |
535 | collection = [] | |
536 | ||
537 | # Empty collection may be returned, in this case we proceed | |
538 | # straight to next_href | |
836ef264 | 539 | |
7c5307f4 S |
540 | def resolve_entry(candidates): |
541 | for cand in candidates: | |
542 | if not isinstance(cand, dict): | |
543 | continue | |
544 | permalink_url = url_or_none(cand.get('permalink_url')) | |
545 | if not permalink_url: | |
546 | continue | |
547 | return self.url_result( | |
548 | permalink_url, | |
549 | ie=SoundcloudIE.ie_key() if SoundcloudIE.suitable(permalink_url) else None, | |
550 | video_id=self._extract_id(cand), | |
551 | video_title=cand.get('title')) | |
836ef264 S |
552 | |
553 | for e in collection: | |
7c5307f4 S |
554 | entry = resolve_entry((e, e.get('track'), e.get('playlist'))) |
555 | if entry: | |
556 | entries.append(entry) | |
836ef264 S |
557 | |
558 | next_href = response.get('next_href') | |
559 | if not next_href: | |
560 | break | |
561 | ||
562 | parsed_next_href = compat_urlparse.urlparse(response['next_href']) | |
563 | qs = compat_urlparse.parse_qs(parsed_next_href.query) | |
564 | qs.update(COMMON_QUERY) | |
565 | next_href = compat_urlparse.urlunparse( | |
566 | parsed_next_href._replace(query=compat_urllib_parse_urlencode(qs, True))) | |
567 | ||
568 | return { | |
569 | '_type': 'playlist', | |
570 | 'id': playlist_id, | |
571 | 'title': playlist_title, | |
572 | 'entries': entries, | |
573 | } | |
574 | ||
575 | ||
576 | class SoundcloudUserIE(SoundcloudPagedPlaylistBaseIE): | |
16a08978 S |
577 | _VALID_URL = r'''(?x) |
578 | https?:// | |
579 | (?:(?:www|m)\.)?soundcloud\.com/ | |
580 | (?P<user>[^/]+) | |
581 | (?:/ | |
3ef2da2d | 582 | (?P<rsrc>tracks|albums|sets|reposts|likes|spotlight) |
16a08978 S |
583 | )? |
584 | /?(?:[?#].*)?$ | |
585 | ''' | |
fbcd7b5f | 586 | IE_NAME = 'soundcloud:user' |
22a6f150 | 587 | _TESTS = [{ |
b6423e6c | 588 | 'url': 'https://soundcloud.com/soft-cell-official', |
22a6f150 | 589 | 'info_dict': { |
b6423e6c S |
590 | 'id': '207965082', |
591 | 'title': 'Soft Cell (All)', | |
22a6f150 | 592 | }, |
b6423e6c | 593 | 'playlist_mincount': 28, |
22a6f150 | 594 | }, { |
b6423e6c | 595 | 'url': 'https://soundcloud.com/soft-cell-official/tracks', |
22a6f150 | 596 | 'info_dict': { |
b6423e6c S |
597 | 'id': '207965082', |
598 | 'title': 'Soft Cell (Tracks)', | |
22a6f150 | 599 | }, |
b6423e6c | 600 | 'playlist_mincount': 27, |
03b9c944 | 601 | }, { |
b6423e6c S |
602 | 'url': 'https://soundcloud.com/soft-cell-official/albums', |
603 | 'info_dict': { | |
604 | 'id': '207965082', | |
605 | 'title': 'Soft Cell (Albums)', | |
606 | }, | |
607 | 'playlist_mincount': 1, | |
608 | }, { | |
609 | 'url': 'https://soundcloud.com/jcv246/sets', | |
80fb6d4a | 610 | 'info_dict': { |
b6423e6c S |
611 | 'id': '12982173', |
612 | 'title': 'Jordi / cv (Playlists)', | |
80fb6d4a | 613 | }, |
8e45e1cc | 614 | 'playlist_mincount': 2, |
80fb6d4a | 615 | }, { |
b6423e6c | 616 | 'url': 'https://soundcloud.com/jcv246/reposts', |
80fb6d4a | 617 | 'info_dict': { |
b6423e6c S |
618 | 'id': '12982173', |
619 | 'title': 'Jordi / cv (Reposts)', | |
80fb6d4a | 620 | }, |
b6423e6c | 621 | 'playlist_mincount': 6, |
80fb6d4a | 622 | }, { |
b6423e6c | 623 | 'url': 'https://soundcloud.com/clalberg/likes', |
80fb6d4a | 624 | 'info_dict': { |
b6423e6c S |
625 | 'id': '11817582', |
626 | 'title': 'clalberg (Likes)', | |
80fb6d4a | 627 | }, |
b6423e6c | 628 | 'playlist_mincount': 5, |
80fb6d4a S |
629 | }, { |
630 | 'url': 'https://soundcloud.com/grynpyret/spotlight', | |
631 | 'info_dict': { | |
632 | 'id': '7098329', | |
bf2dc9cc | 633 | 'title': 'Grynpyret (Spotlight)', |
80fb6d4a S |
634 | }, |
635 | 'playlist_mincount': 1, | |
22a6f150 | 636 | }] |
92790f4e | 637 | |
80fb6d4a | 638 | _BASE_URL_MAP = { |
3ef2da2d S |
639 | 'all': '%s/stream/users/%%s' % SoundcloudPagedPlaylistBaseIE._API_V2_BASE, |
640 | 'tracks': '%s/users/%%s/tracks' % SoundcloudPagedPlaylistBaseIE._API_V2_BASE, | |
641 | 'albums': '%s/users/%%s/albums' % SoundcloudPagedPlaylistBaseIE._API_V2_BASE, | |
836ef264 | 642 | 'sets': '%s/users/%%s/playlists' % SoundcloudPagedPlaylistBaseIE._API_V2_BASE, |
3ef2da2d | 643 | 'reposts': '%s/stream/users/%%s/reposts' % SoundcloudPagedPlaylistBaseIE._API_V2_BASE, |
836ef264 S |
644 | 'likes': '%s/users/%%s/likes' % SoundcloudPagedPlaylistBaseIE._API_V2_BASE, |
645 | 'spotlight': '%s/users/%%s/spotlight' % SoundcloudPagedPlaylistBaseIE._API_V2_BASE, | |
80fb6d4a S |
646 | } |
647 | ||
648 | _TITLE_MAP = { | |
649 | 'all': 'All', | |
650 | 'tracks': 'Tracks', | |
3ef2da2d | 651 | 'albums': 'Albums', |
80fb6d4a S |
652 | 'sets': 'Playlists', |
653 | 'reposts': 'Reposts', | |
654 | 'likes': 'Likes', | |
655 | 'spotlight': 'Spotlight', | |
656 | } | |
657 | ||
92790f4e JMF |
658 | def _real_extract(self, url): |
659 | mobj = re.match(self._VALID_URL, url) | |
660 | uploader = mobj.group('user') | |
661 | ||
f1c05100 | 662 | url = 'https://soundcloud.com/%s/' % uploader |
92790f4e | 663 | resolv_url = self._resolv_url(url) |
20991253 PH |
664 | user = self._download_json( |
665 | resolv_url, uploader, 'Downloading user info') | |
80fb6d4a S |
666 | |
667 | resource = mobj.group('rsrc') or 'all' | |
80fb6d4a | 668 | |
836ef264 S |
669 | return self._extract_playlist( |
670 | self._BASE_URL_MAP[resource] % user['id'], compat_str(user['id']), | |
671 | '%s (%s)' % (user['username'], self._TITLE_MAP[resource])) | |
97afd99a | 672 | |
92790f4e | 673 | |
836ef264 S |
674 | class SoundcloudTrackStationIE(SoundcloudPagedPlaylistBaseIE): |
675 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/stations/track/[^/]+/(?P<id>[^/?#&]+)' | |
676 | IE_NAME = 'soundcloud:trackstation' | |
677 | _TESTS = [{ | |
678 | 'url': 'https://soundcloud.com/stations/track/officialsundial/your-text', | |
679 | 'info_dict': { | |
680 | 'id': '286017854', | |
681 | 'title': 'Track station: your-text', | |
682 | }, | |
683 | 'playlist_mincount': 47, | |
684 | }] | |
80fb6d4a | 685 | |
836ef264 S |
686 | def _real_extract(self, url): |
687 | track_name = self._match_id(url) | |
80fb6d4a | 688 | |
836ef264 | 689 | webpage = self._download_webpage(url, track_name) |
97afd99a | 690 | |
836ef264 S |
691 | track_id = self._search_regex( |
692 | r'soundcloud:track-stations:(\d+)', webpage, 'track id') | |
92790f4e | 693 | |
836ef264 S |
694 | return self._extract_playlist( |
695 | '%s/stations/soundcloud:track-stations:%s/tracks' | |
696 | % (self._API_V2_BASE, track_id), | |
697 | track_id, 'Track station: %s' % track_name) | |
20991253 PH |
698 | |
699 | ||
7518a61d | 700 | class SoundcloudPlaylistIE(SoundcloudPlaylistBaseIE): |
46f74bcf | 701 | _VALID_URL = r'https?://api\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$' |
20991253 | 702 | IE_NAME = 'soundcloud:playlist' |
46f74bcf | 703 | _TESTS = [{ |
f1c05100 | 704 | 'url': 'https://api.soundcloud.com/playlists/4110309', |
46f74bcf PH |
705 | 'info_dict': { |
706 | 'id': '4110309', | |
707 | 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]', | |
708 | 'description': 're:.*?TILT Brass - Bowery Poetry Club', | |
709 | }, | |
710 | 'playlist_count': 6, | |
711 | }] | |
20991253 PH |
712 | |
713 | def _real_extract(self, url): | |
714 | mobj = re.match(self._VALID_URL, url) | |
715 | playlist_id = mobj.group('id') | |
716 | base_url = '%s//api.soundcloud.com/playlists/%s.json?' % (self.http_scheme(), playlist_id) | |
717 | ||
2f834e93 | 718 | data_dict = { |
20991253 | 719 | 'client_id': self._CLIENT_ID, |
2f834e93 | 720 | } |
721 | token = mobj.group('token') | |
722 | ||
723 | if token: | |
724 | data_dict['secret_token'] = token | |
725 | ||
15707c7e | 726 | data = compat_urllib_parse_urlencode(data_dict) |
20991253 PH |
727 | data = self._download_json( |
728 | base_url + data, playlist_id, 'Downloading playlist') | |
729 | ||
8e45e1cc | 730 | entries = self._extract_track_entries(data['tracks']) |
20991253 PH |
731 | |
732 | return { | |
733 | '_type': 'playlist', | |
734 | 'id': playlist_id, | |
735 | 'title': data.get('title'), | |
736 | 'description': data.get('description'), | |
737 | 'entries': entries, | |
92790f4e | 738 | } |
2abf7cab | 739 | |
740 | ||
741 | class SoundcloudSearchIE(SearchInfoExtractor, SoundcloudIE): | |
742 | IE_NAME = 'soundcloud:search' | |
743 | IE_DESC = 'Soundcloud search' | |
328a22e1 | 744 | _MAX_RESULTS = float('inf') |
2abf7cab | 745 | _TESTS = [{ |
746 | 'url': 'scsearch15:post-avant jazzcore', | |
747 | 'info_dict': { | |
748 | 'title': 'post-avant jazzcore', | |
749 | }, | |
750 | 'playlist_count': 15, | |
751 | }] | |
752 | ||
753 | _SEARCH_KEY = 'scsearch' | |
328a22e1 | 754 | _MAX_RESULTS_PER_PAGE = 200 |
755 | _DEFAULT_RESULTS_PER_PAGE = 50 | |
c30943b1 | 756 | _API_V2_BASE = 'https://api-v2.soundcloud.com' |
2abf7cab | 757 | |
758 | def _get_collection(self, endpoint, collection_id, **query): | |
a3372437 | 759 | limit = min( |
328a22e1 | 760 | query.get('limit', self._DEFAULT_RESULTS_PER_PAGE), |
761 | self._MAX_RESULTS_PER_PAGE) | |
f6c903e7 | 762 | query['limit'] = limit |
2abf7cab | 763 | query['client_id'] = self._CLIENT_ID |
764 | query['linked_partitioning'] = '1' | |
f6c903e7 | 765 | query['offset'] = 0 |
15707c7e | 766 | data = compat_urllib_parse_urlencode(query) |
f6c903e7 | 767 | next_url = '{0}{1}?{2}'.format(self._API_V2_BASE, endpoint, data) |
2abf7cab | 768 | |
2abf7cab | 769 | collected_results = 0 |
770 | ||
f6c903e7 | 771 | for i in itertools.count(1): |
7e347275 | 772 | response = self._download_json( |
f6c903e7 | 773 | next_url, collection_id, 'Downloading page {0}'.format(i), |
7e347275 | 774 | 'Unable to download API page') |
2abf7cab | 775 | |
f6c903e7 S |
776 | collection = response.get('collection', []) |
777 | if not collection: | |
778 | break | |
2abf7cab | 779 | |
f6c903e7 | 780 | collection = list(filter(bool, collection)) |
2abf7cab | 781 | collected_results += len(collection) |
782 | ||
f6c903e7 S |
783 | for item in collection: |
784 | yield self.url_result(item['uri'], SoundcloudIE.ie_key()) | |
2abf7cab | 785 | |
f6c903e7 | 786 | if not collection or collected_results >= limit: |
2abf7cab | 787 | break |
788 | ||
7e347275 | 789 | next_url = response.get('next_href') |
f6c903e7 S |
790 | if not next_url: |
791 | break | |
2abf7cab | 792 | |
793 | def _get_n_results(self, query, n): | |
f6c903e7 S |
794 | tracks = self._get_collection('/search/tracks', query, limit=n, q=query) |
795 | return self.playlist_result(tracks, playlist_title=query) |