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