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