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