]>
Commit | Line | Data |
---|---|---|
de2dd4c5 | 1 | # encoding: utf-8 |
fbcd7b5f PH |
2 | from __future__ import unicode_literals |
3 | ||
aad0d6d5 | 4 | import re |
92790f4e | 5 | import itertools |
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, |
eb920777 | 18 | int_or_none, |
aad0d6d5 PH |
19 | unified_strdate, |
20 | ) | |
21 | ||
22 | ||
23 | class SoundcloudIE(InfoExtractor): | |
24 | """Information extractor for soundcloud.com | |
25 | To access the media, the uid of the song and a stream token | |
26 | must be extracted from the page source and the script must make | |
27 | a request to media.soundcloud.com/crossdomain.xml. Then | |
28 | the media can be grabbed by requesting from an url composed | |
29 | of the stream token and uid | |
30 | """ | |
31 | ||
20991253 | 32 | _VALID_URL = r'''(?x)^(?:https?://)? |
71507a11 | 33 | (?:(?:(?:www\.|m\.)?soundcloud\.com/ |
4ff50ef8 | 34 | (?P<uploader>[\w\d-]+)/ |
f7043ef3 | 35 | (?!(?:tracks|sets(?:/.+?)?|reposts|likes|spotlight)/?(?:$|[?#])) |
22a6f150 | 36 | (?P<title>[\w\d-]+)/? |
de2dd4c5 | 37 | (?P<token>[^?]+?)?(?:[?].*)?$) |
9296738f | 38 | |(?:api\.soundcloud\.com/tracks/(?P<track_id>\d+) |
0403b069 | 39 | (?:/?\?secret_token=(?P<secret_token>[^&]+))?) |
31c1cf5a | 40 | |(?P<player>(?:w|player|p.)\.soundcloud\.com/player/?.*?url=.*) |
eb6a41ba JMF |
41 | ) |
42 | ''' | |
fbcd7b5f | 43 | IE_NAME = 'soundcloud' |
12c167c8 JMF |
44 | _TESTS = [ |
45 | { | |
fbcd7b5f | 46 | 'url': 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy', |
fbcd7b5f PH |
47 | 'md5': 'ebef0a451b909710ed1d7787dddbf0d7', |
48 | 'info_dict': { | |
0eb9fb9f JMF |
49 | 'id': '62986583', |
50 | 'ext': 'mp3', | |
51 | 'upload_date': '20121011', | |
52 | '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', | |
53 | 'uploader': 'E.T. ExTerrestrial Music', | |
54 | 'title': 'Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1', | |
55 | 'duration': 143, | |
4bfd294e | 56 | 'license': 'all-rights-reserved', |
12c167c8 JMF |
57 | } |
58 | }, | |
59 | # not streamable song | |
60 | { | |
fbcd7b5f PH |
61 | 'url': 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep', |
62 | 'info_dict': { | |
63 | 'id': '47127627', | |
64 | 'ext': 'mp3', | |
65 | 'title': 'Goldrushed', | |
63ad0315 | 66 | 'description': 'From Stockholm Sweden\r\nPovel / Magnus / Filip / David\r\nwww.theroyalconcept.com', |
fbcd7b5f PH |
67 | 'uploader': 'The Royal Concept', |
68 | 'upload_date': '20120521', | |
eb920777 | 69 | 'duration': 227, |
4bfd294e | 70 | 'license': 'all-rights-reserved', |
12c167c8 | 71 | }, |
fbcd7b5f | 72 | 'params': { |
12c167c8 | 73 | # rtmp |
fbcd7b5f | 74 | 'skip_download': True, |
12c167c8 JMF |
75 | }, |
76 | }, | |
de2dd4c5 JMF |
77 | # private link |
78 | { | |
fbcd7b5f PH |
79 | 'url': 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp', |
80 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
81 | 'info_dict': { | |
82 | 'id': '123998367', | |
83 | 'ext': 'mp3', | |
84 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
85 | 'uploader': 'jaimeMF', | |
86 | 'description': 'test chars: \"\'/\\ä↭', | |
87 | 'upload_date': '20131209', | |
eb920777 | 88 | 'duration': 9, |
4bfd294e | 89 | 'license': 'all-rights-reserved', |
de2dd4c5 JMF |
90 | }, |
91 | }, | |
9296738f | 92 | # private link (alt format) |
93 | { | |
94 | 'url': 'https://api.soundcloud.com/tracks/123998367?secret_token=s-8Pjrp', | |
95 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
96 | 'info_dict': { | |
97 | 'id': '123998367', | |
98 | 'ext': 'mp3', | |
99 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
100 | 'uploader': 'jaimeMF', | |
101 | 'description': 'test chars: \"\'/\\ä↭', | |
102 | 'upload_date': '20131209', | |
103 | 'duration': 9, | |
4bfd294e | 104 | 'license': 'all-rights-reserved', |
9296738f | 105 | }, |
106 | }, | |
f67ca84d JMF |
107 | # downloadable song |
108 | { | |
00a82ea8 | 109 | 'url': 'https://soundcloud.com/oddsamples/bus-brakes', |
eae12e3f | 110 | 'md5': '7624f2351f8a3b2e7cd51522496e7631', |
fbcd7b5f | 111 | 'info_dict': { |
00a82ea8 | 112 | 'id': '128590877', |
eae12e3f | 113 | 'ext': 'mp3', |
00a82ea8 | 114 | 'title': 'Bus Brakes', |
0eb9fb9f | 115 | 'description': 'md5:0053ca6396e8d2fd7b7e1595ef12ab66', |
00a82ea8 S |
116 | 'uploader': 'oddsamples', |
117 | 'upload_date': '20140109', | |
118 | 'duration': 17, | |
4bfd294e | 119 | 'license': 'cc-by-sa', |
f67ca84d JMF |
120 | }, |
121 | }, | |
12c167c8 | 122 | ] |
aad0d6d5 | 123 | |
eb11cbe8 | 124 | _CLIENT_ID = '02gUJC0hH2ct1EGOcYXQIzRFU91c72Ea' |
64bb5187 | 125 | _IPHONE_CLIENT_ID = '376f225bf427445fc4bfb6b99b72e0bf' |
7d239269 | 126 | |
fbdf8d15 S |
127 | @staticmethod |
128 | def _extract_urls(webpage): | |
129 | return [m.group('url') for m in re.finditer( | |
130 | r'<iframe[^>]+src=(["\'])(?P<url>(?:https?://)?(?:w\.)?soundcloud\.com/player.+?)\1', | |
131 | webpage)] | |
132 | ||
aad0d6d5 PH |
133 | def report_resolve(self, video_id): |
134 | """Report information extraction.""" | |
83622b6d | 135 | self.to_screen('%s: Resolving id' % video_id) |
aad0d6d5 | 136 | |
7d239269 JMF |
137 | @classmethod |
138 | def _resolv_url(cls, url): | |
139 | return 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=' + cls._CLIENT_ID | |
140 | ||
de2dd4c5 | 141 | def _extract_info_dict(self, info, full_title=None, quiet=False, secret_token=None): |
12c167c8 JMF |
142 | track_id = compat_str(info['id']) |
143 | name = full_title or track_id | |
2a15e706 | 144 | if quiet: |
92790f4e | 145 | self.report_extraction(name) |
f62a77b9 S |
146 | thumbnail = info.get('artwork_url') |
147 | if isinstance(thumbnail, compat_str): | |
7d239269 | 148 | thumbnail = thumbnail.replace('-large', '-t500x500') |
fbcd7b5f | 149 | ext = 'mp3' |
12c167c8 | 150 | result = { |
2a15e706 | 151 | 'id': track_id, |
f62a77b9 S |
152 | 'uploader': info.get('user', {}).get('username'), |
153 | 'upload_date': unified_strdate(info.get('created_at')), | |
2a15e706 | 154 | 'title': info['title'], |
f62a77b9 | 155 | 'description': info.get('description'), |
7d239269 | 156 | 'thumbnail': thumbnail, |
eb920777 | 157 | 'duration': int_or_none(info.get('duration'), 1000), |
579657ad | 158 | 'webpage_url': info.get('permalink_url'), |
f62a77b9 | 159 | 'license': info.get('license'), |
7d239269 | 160 | } |
5e114e4b | 161 | formats = [] |
12c167c8 | 162 | if info.get('downloadable', False): |
64bb5187 | 163 | # We can build a direct link to the song |
2a15e706 | 164 | format_url = ( |
fbcd7b5f | 165 | 'https://api.soundcloud.com/tracks/{0}/download?client_id={1}'.format( |
2a15e706 | 166 | track_id, self._CLIENT_ID)) |
5e114e4b | 167 | formats.append({ |
2a15e706 | 168 | 'format_id': 'download', |
fbcd7b5f | 169 | 'ext': info.get('original_format', 'mp3'), |
2a15e706 | 170 | 'url': format_url, |
fb04e403 | 171 | 'vcodec': 'none', |
5e114e4b PH |
172 | 'preference': 10, |
173 | }) | |
174 | ||
175 | # We have to retrieve the url | |
176 | streams_url = ('http://api.soundcloud.com/i1/tracks/{0}/streams?' | |
9e1a5b84 | 177 | 'client_id={1}&secret_token={2}'.format(track_id, self._IPHONE_CLIENT_ID, secret_token)) |
20991253 | 178 | format_dict = self._download_json( |
5e114e4b PH |
179 | streams_url, |
180 | track_id, 'Downloading track url') | |
181 | ||
5e114e4b PH |
182 | for key, stream_url in format_dict.items(): |
183 | if key.startswith('http'): | |
184 | formats.append({ | |
185 | 'format_id': key, | |
186 | 'ext': ext, | |
187 | 'url': stream_url, | |
188 | 'vcodec': 'none', | |
189 | }) | |
190 | elif key.startswith('rtmp'): | |
191 | # The url doesn't have an rtmp app, we have to extract the playpath | |
192 | url, path = stream_url.split('mp3:', 1) | |
193 | formats.append({ | |
194 | 'format_id': key, | |
195 | 'url': url, | |
196 | 'play_path': 'mp3:' + path, | |
295df4ed | 197 | 'ext': 'flv', |
5e114e4b PH |
198 | 'vcodec': 'none', |
199 | }) | |
2a15e706 PH |
200 | |
201 | if not formats: | |
64bb5187 JMF |
202 | # We fallback to the stream_url in the original info, this |
203 | # cannot be always used, sometimes it can give an HTTP 404 error | |
2a15e706 | 204 | formats.append({ |
fbcd7b5f | 205 | 'format_id': 'fallback', |
2a15e706 PH |
206 | 'url': info['stream_url'] + '?client_id=' + self._CLIENT_ID, |
207 | 'ext': ext, | |
fb04e403 | 208 | 'vcodec': 'none', |
2a15e706 PH |
209 | }) |
210 | ||
fbcd7b5f | 211 | for f in formats: |
2a15e706 | 212 | if f['format_id'].startswith('http'): |
fbcd7b5f | 213 | f['protocol'] = 'http' |
2a15e706 | 214 | if f['format_id'].startswith('rtmp'): |
fbcd7b5f | 215 | f['protocol'] = 'rtmp' |
2a15e706 | 216 | |
562ceab1 S |
217 | self._check_formats(formats, track_id) |
218 | self._sort_formats(formats) | |
219 | result['formats'] = formats | |
64bb5187 | 220 | |
12c167c8 | 221 | return result |
7d239269 | 222 | |
aad0d6d5 | 223 | def _real_extract(self, url): |
eb6a41ba | 224 | mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE) |
aad0d6d5 | 225 | if mobj is None: |
83622b6d | 226 | raise ExtractorError('Invalid URL: %s' % url) |
aad0d6d5 | 227 | |
eb6a41ba | 228 | track_id = mobj.group('track_id') |
4bfd294e | 229 | |
eb6a41ba JMF |
230 | if track_id is not None: |
231 | info_json_url = 'http://api.soundcloud.com/tracks/' + track_id + '.json?client_id=' + self._CLIENT_ID | |
232 | full_title = track_id | |
9296738f | 233 | token = mobj.group('secret_token') |
234 | if token: | |
611c1dd9 | 235 | info_json_url += '&secret_token=' + token |
31c1cf5a | 236 | elif mobj.group('player'): |
668de34c | 237 | query = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query) |
024ebb27 JMF |
238 | real_url = query['url'][0] |
239 | # If the token is in the query of the original url we have to | |
240 | # manually add it | |
241 | if 'secret_token' in query: | |
242 | real_url += '?secret_token=' + query['secret_token'][0] | |
243 | return self.url_result(real_url) | |
eb6a41ba JMF |
244 | else: |
245 | # extract uploader (which is in the url) | |
de2dd4c5 | 246 | uploader = mobj.group('uploader') |
eb6a41ba | 247 | # extract simple title (uploader + slug of song title) |
8bcc8756 | 248 | slug_title = mobj.group('title') |
de2dd4c5 JMF |
249 | token = mobj.group('token') |
250 | full_title = resolve_title = '%s/%s' % (uploader, slug_title) | |
251 | if token: | |
252 | resolve_title += '/%s' % token | |
5f6a1245 | 253 | |
eb6a41ba | 254 | self.report_resolve(full_title) |
5f6a1245 | 255 | |
de2dd4c5 | 256 | url = 'http://soundcloud.com/%s' % resolve_title |
eb6a41ba | 257 | info_json_url = self._resolv_url(url) |
20991253 | 258 | info = self._download_json(info_json_url, full_title, 'Downloading info JSON') |
aad0d6d5 | 259 | |
de2dd4c5 | 260 | return self._extract_info_dict(info, full_title, secret_token=token) |
aad0d6d5 | 261 | |
20991253 | 262 | |
7d239269 | 263 | class SoundcloudSetIE(SoundcloudIE): |
c808ef81 | 264 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<uploader>[\w\d-]+)/sets/(?P<slug_title>[\w\d-]+)(?:/(?P<token>[^?/]+))?' |
fbcd7b5f | 265 | IE_NAME = 'soundcloud:set' |
22a6f150 PH |
266 | _TESTS = [{ |
267 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep', | |
268 | 'info_dict': { | |
a9551e90 | 269 | 'id': '2284613', |
22a6f150 PH |
270 | 'title': 'The Royal Concept EP', |
271 | }, | |
272 | 'playlist_mincount': 6, | |
f7043ef3 S |
273 | }, { |
274 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep/token', | |
275 | 'only_matching': True, | |
22a6f150 | 276 | }] |
aad0d6d5 | 277 | |
aad0d6d5 PH |
278 | def _real_extract(self, url): |
279 | mobj = re.match(self._VALID_URL, url) | |
aad0d6d5 PH |
280 | |
281 | # extract uploader (which is in the url) | |
2f834e93 | 282 | uploader = mobj.group('uploader') |
aad0d6d5 | 283 | # extract simple title (uploader + slug of song title) |
2f834e93 | 284 | slug_title = mobj.group('slug_title') |
aad0d6d5 | 285 | full_title = '%s/sets/%s' % (uploader, slug_title) |
2f834e93 | 286 | url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title) |
287 | ||
288 | token = mobj.group('token') | |
289 | if token: | |
290 | full_title += '/' + token | |
291 | url += '/' + token | |
aad0d6d5 PH |
292 | |
293 | self.report_resolve(full_title) | |
294 | ||
7d239269 | 295 | resolv_url = self._resolv_url(url) |
20991253 | 296 | info = self._download_json(resolv_url, full_title) |
aad0d6d5 | 297 | |
aad0d6d5 | 298 | if 'errors' in info: |
214e74bf JMF |
299 | msgs = (compat_str(err['error_message']) for err in info['errors']) |
300 | raise ExtractorError('unable to download video webpage: %s' % ','.join(msgs)) | |
aad0d6d5 | 301 | |
b14fa8e6 S |
302 | entries = [self.url_result(track['permalink_url'], 'Soundcloud') for track in info['tracks']] |
303 | ||
22a6f150 PH |
304 | return { |
305 | '_type': 'playlist', | |
b14fa8e6 | 306 | 'entries': entries, |
a9551e90 | 307 | 'id': '%s' % info['id'], |
22a6f150 PH |
308 | 'title': info['title'], |
309 | } | |
92790f4e JMF |
310 | |
311 | ||
312 | class SoundcloudUserIE(SoundcloudIE): | |
16a08978 S |
313 | _VALID_URL = r'''(?x) |
314 | https?:// | |
315 | (?:(?:www|m)\.)?soundcloud\.com/ | |
316 | (?P<user>[^/]+) | |
317 | (?:/ | |
318 | (?P<rsrc>tracks|sets|reposts|likes|spotlight) | |
319 | )? | |
320 | /?(?:[?#].*)?$ | |
321 | ''' | |
fbcd7b5f | 322 | IE_NAME = 'soundcloud:user' |
22a6f150 | 323 | _TESTS = [{ |
80fb6d4a | 324 | 'url': 'https://soundcloud.com/the-akashic-chronicler', |
22a6f150 | 325 | 'info_dict': { |
80fb6d4a S |
326 | 'id': '114582580', |
327 | 'title': 'The Akashic Chronicler (All)', | |
22a6f150 | 328 | }, |
66ce9702 | 329 | 'playlist_mincount': 111, |
22a6f150 | 330 | }, { |
80fb6d4a | 331 | 'url': 'https://soundcloud.com/the-akashic-chronicler/tracks', |
22a6f150 | 332 | 'info_dict': { |
80fb6d4a S |
333 | 'id': '114582580', |
334 | 'title': 'The Akashic Chronicler (Tracks)', | |
22a6f150 | 335 | }, |
80fb6d4a | 336 | 'playlist_mincount': 50, |
03b9c944 | 337 | }, { |
80fb6d4a S |
338 | 'url': 'https://soundcloud.com/the-akashic-chronicler/sets', |
339 | 'info_dict': { | |
340 | 'id': '114582580', | |
341 | 'title': 'The Akashic Chronicler (Playlists)', | |
342 | }, | |
343 | 'playlist_mincount': 3, | |
344 | }, { | |
345 | 'url': 'https://soundcloud.com/the-akashic-chronicler/reposts', | |
346 | 'info_dict': { | |
347 | 'id': '114582580', | |
348 | 'title': 'The Akashic Chronicler (Reposts)', | |
349 | }, | |
66ce9702 | 350 | 'playlist_mincount': 7, |
80fb6d4a S |
351 | }, { |
352 | 'url': 'https://soundcloud.com/the-akashic-chronicler/likes', | |
353 | 'info_dict': { | |
354 | 'id': '114582580', | |
355 | 'title': 'The Akashic Chronicler (Likes)', | |
356 | }, | |
66ce9702 | 357 | 'playlist_mincount': 321, |
80fb6d4a S |
358 | }, { |
359 | 'url': 'https://soundcloud.com/grynpyret/spotlight', | |
360 | 'info_dict': { | |
361 | 'id': '7098329', | |
362 | 'title': 'Grynpyret (Spotlight)', | |
363 | }, | |
364 | 'playlist_mincount': 1, | |
22a6f150 | 365 | }] |
92790f4e | 366 | |
80fb6d4a S |
367 | _API_BASE = 'https://api.soundcloud.com' |
368 | _API_V2_BASE = 'https://api-v2.soundcloud.com' | |
369 | ||
370 | _BASE_URL_MAP = { | |
371 | 'all': '%s/profile/soundcloud:users:%%s' % _API_V2_BASE, | |
372 | 'tracks': '%s/users/%%s/tracks' % _API_BASE, | |
373 | 'sets': '%s/users/%%s/playlists' % _API_V2_BASE, | |
374 | 'reposts': '%s/profile/soundcloud:users:%%s/reposts' % _API_V2_BASE, | |
375 | 'likes': '%s/users/%%s/likes' % _API_V2_BASE, | |
376 | 'spotlight': '%s/users/%%s/spotlight' % _API_V2_BASE, | |
377 | } | |
378 | ||
379 | _TITLE_MAP = { | |
380 | 'all': 'All', | |
381 | 'tracks': 'Tracks', | |
382 | 'sets': 'Playlists', | |
383 | 'reposts': 'Reposts', | |
384 | 'likes': 'Likes', | |
385 | 'spotlight': 'Spotlight', | |
386 | } | |
387 | ||
92790f4e JMF |
388 | def _real_extract(self, url): |
389 | mobj = re.match(self._VALID_URL, url) | |
390 | uploader = mobj.group('user') | |
391 | ||
392 | url = 'http://soundcloud.com/%s/' % uploader | |
393 | resolv_url = self._resolv_url(url) | |
20991253 PH |
394 | user = self._download_json( |
395 | resolv_url, uploader, 'Downloading user info') | |
80fb6d4a S |
396 | |
397 | resource = mobj.group('rsrc') or 'all' | |
398 | base_url = self._BASE_URL_MAP[resource] % user['id'] | |
399 | ||
97afd99a S |
400 | COMMON_QUERY = { |
401 | 'limit': 50, | |
402 | 'client_id': self._CLIENT_ID, | |
403 | 'linked_partitioning': '1', | |
404 | } | |
405 | ||
406 | query = COMMON_QUERY.copy() | |
407 | query['offset'] = 0 | |
408 | ||
15707c7e | 409 | next_href = base_url + '?' + compat_urllib_parse_urlencode(query) |
92790f4e | 410 | |
20991253 | 411 | entries = [] |
92790f4e | 412 | for i in itertools.count(): |
80fb6d4a S |
413 | response = self._download_json( |
414 | next_href, uploader, 'Downloading track page %s' % (i + 1)) | |
415 | ||
416 | collection = response['collection'] | |
80fb6d4a | 417 | if not collection: |
92790f4e | 418 | break |
80fb6d4a S |
419 | |
420 | def resolve_permalink_url(candidates): | |
421 | for cand in candidates: | |
422 | if isinstance(cand, dict): | |
423 | permalink_url = cand.get('permalink_url') | |
424 | if permalink_url and permalink_url.startswith('http'): | |
425 | return permalink_url | |
426 | ||
427 | for e in collection: | |
428 | permalink_url = resolve_permalink_url((e, e.get('track'), e.get('playlist'))) | |
429 | if permalink_url: | |
430 | entries.append(self.url_result(permalink_url)) | |
431 | ||
97afd99a S |
432 | next_href = response.get('next_href') |
433 | if not next_href: | |
434 | break | |
435 | ||
436 | parsed_next_href = compat_urlparse.urlparse(response['next_href']) | |
437 | qs = compat_urlparse.parse_qs(parsed_next_href.query) | |
438 | qs.update(COMMON_QUERY) | |
439 | next_href = compat_urlparse.urlunparse( | |
15707c7e | 440 | parsed_next_href._replace(query=compat_urllib_parse_urlencode(qs, True))) |
92790f4e JMF |
441 | |
442 | return { | |
443 | '_type': 'playlist', | |
444 | 'id': compat_str(user['id']), | |
80fb6d4a | 445 | 'title': '%s (%s)' % (user['username'], self._TITLE_MAP[resource]), |
20991253 PH |
446 | 'entries': entries, |
447 | } | |
448 | ||
449 | ||
450 | class SoundcloudPlaylistIE(SoundcloudIE): | |
46f74bcf | 451 | _VALID_URL = r'https?://api\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$' |
20991253 | 452 | IE_NAME = 'soundcloud:playlist' |
46f74bcf PH |
453 | _TESTS = [{ |
454 | 'url': 'http://api.soundcloud.com/playlists/4110309', | |
455 | 'info_dict': { | |
456 | 'id': '4110309', | |
457 | 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]', | |
458 | 'description': 're:.*?TILT Brass - Bowery Poetry Club', | |
459 | }, | |
460 | 'playlist_count': 6, | |
461 | }] | |
20991253 PH |
462 | |
463 | def _real_extract(self, url): | |
464 | mobj = re.match(self._VALID_URL, url) | |
465 | playlist_id = mobj.group('id') | |
466 | base_url = '%s//api.soundcloud.com/playlists/%s.json?' % (self.http_scheme(), playlist_id) | |
467 | ||
2f834e93 | 468 | data_dict = { |
20991253 | 469 | 'client_id': self._CLIENT_ID, |
2f834e93 | 470 | } |
471 | token = mobj.group('token') | |
472 | ||
473 | if token: | |
474 | data_dict['secret_token'] = token | |
475 | ||
15707c7e | 476 | data = compat_urllib_parse_urlencode(data_dict) |
20991253 PH |
477 | data = self._download_json( |
478 | base_url + data, playlist_id, 'Downloading playlist') | |
479 | ||
24628cf7 S |
480 | entries = [ |
481 | self.url_result( | |
482 | track['permalink_url'], SoundcloudIE.ie_key(), | |
483 | video_id=compat_str(track['id']) if track.get('id') else None) | |
484 | for track in data['tracks'] if track.get('permalink_url')] | |
20991253 PH |
485 | |
486 | return { | |
487 | '_type': 'playlist', | |
488 | 'id': playlist_id, | |
489 | 'title': data.get('title'), | |
490 | 'description': data.get('description'), | |
491 | 'entries': entries, | |
92790f4e | 492 | } |
2abf7cab | 493 | |
494 | ||
495 | class SoundcloudSearchIE(SearchInfoExtractor, SoundcloudIE): | |
496 | IE_NAME = 'soundcloud:search' | |
497 | IE_DESC = 'Soundcloud search' | |
328a22e1 | 498 | _MAX_RESULTS = float('inf') |
2abf7cab | 499 | _TESTS = [{ |
500 | 'url': 'scsearch15:post-avant jazzcore', | |
501 | 'info_dict': { | |
502 | 'title': 'post-avant jazzcore', | |
503 | }, | |
504 | 'playlist_count': 15, | |
505 | }] | |
506 | ||
507 | _SEARCH_KEY = 'scsearch' | |
328a22e1 | 508 | _MAX_RESULTS_PER_PAGE = 200 |
509 | _DEFAULT_RESULTS_PER_PAGE = 50 | |
c30943b1 | 510 | _API_V2_BASE = 'https://api-v2.soundcloud.com' |
2abf7cab | 511 | |
512 | def _get_collection(self, endpoint, collection_id, **query): | |
a3372437 | 513 | limit = min( |
328a22e1 | 514 | query.get('limit', self._DEFAULT_RESULTS_PER_PAGE), |
515 | self._MAX_RESULTS_PER_PAGE) | |
f6c903e7 | 516 | query['limit'] = limit |
2abf7cab | 517 | query['client_id'] = self._CLIENT_ID |
518 | query['linked_partitioning'] = '1' | |
f6c903e7 | 519 | query['offset'] = 0 |
15707c7e | 520 | data = compat_urllib_parse_urlencode(query) |
f6c903e7 | 521 | next_url = '{0}{1}?{2}'.format(self._API_V2_BASE, endpoint, data) |
2abf7cab | 522 | |
2abf7cab | 523 | collected_results = 0 |
524 | ||
f6c903e7 | 525 | for i in itertools.count(1): |
7e347275 | 526 | response = self._download_json( |
f6c903e7 | 527 | next_url, collection_id, 'Downloading page {0}'.format(i), |
7e347275 | 528 | 'Unable to download API page') |
2abf7cab | 529 | |
f6c903e7 S |
530 | collection = response.get('collection', []) |
531 | if not collection: | |
532 | break | |
2abf7cab | 533 | |
f6c903e7 | 534 | collection = list(filter(bool, collection)) |
2abf7cab | 535 | collected_results += len(collection) |
536 | ||
f6c903e7 S |
537 | for item in collection: |
538 | yield self.url_result(item['uri'], SoundcloudIE.ie_key()) | |
2abf7cab | 539 | |
f6c903e7 | 540 | if not collection or collected_results >= limit: |
2abf7cab | 541 | break |
542 | ||
7e347275 | 543 | next_url = response.get('next_href') |
f6c903e7 S |
544 | if not next_url: |
545 | break | |
2abf7cab | 546 | |
547 | def _get_n_results(self, query, n): | |
f6c903e7 S |
548 | tracks = self._get_collection('/search/tracks', query, limit=n, q=query) |
549 | return self.playlist_result(tracks, playlist_title=query) |