2 from __future__ import unicode_literals
11 from ..compat import (
30 class SoundcloudEmbedIE(InfoExtractor):
31 _VALID_URL = r'https?://(?:w|player|p)\.soundcloud\.com/player/?.*?url=(?P<id>.*)'
34 def _extract_urls(webpage):
35 return [m.group('url') for m in re.finditer(
36 r'<iframe[^>]+src=(["\'])(?P<url>(?:https?://)?(?:w\.)?soundcloud\.com/player.+?)\1',
39 def _real_extract(self, url):
40 return self.url_result(compat_urlparse.parse_qs(
41 compat_urlparse.urlparse(url).query)['url'][0])
44 class SoundcloudIE(InfoExtractor):
45 """Information extractor for soundcloud.com
46 To access the media, the uid of the song and a stream token
47 must be extracted from the page source and the script must make
48 a request to media.soundcloud.com/crossdomain.xml. Then
49 the media can be grabbed by requesting from an url composed
50 of the stream token and uid
53 _VALID_URL = r'''(?x)^(?:https?://)?
54 (?:(?:(?:www\.|m\.)?soundcloud\.com/
56 (?P<uploader>[\w\d-]+)/
57 (?!(?:tracks|albums|sets(?:/.+?)?|reposts|likes|spotlight)/?(?:$|[?#]))
59 (?P<token>[^?]+?)?(?:[?].*)?$)
60 |(?:api(?:-v2)?\.soundcloud\.com/tracks/(?P<track_id>\d+)
61 (?:/?\?secret_token=(?P<secret_token>[^&]+))?)
64 IE_NAME = 'soundcloud'
67 'url': 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy',
68 'md5': 'ebef0a451b909710ed1d7787dddbf0d7',
72 'title': 'Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1',
73 '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',
74 'uploader': 'E.T. ExTerrestrial Music',
75 'uploader_id': '1571244',
76 'timestamp': 1349920598,
77 'upload_date': '20121011',
79 'license': 'all-rights-reserved',
88 'url': 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep',
92 'title': 'Goldrushed',
93 'description': 'From Stockholm Sweden\r\nPovel / Magnus / Filip / David\r\nwww.theroyalconcept.com',
94 'uploader': 'The Royal Concept',
95 'uploader_id': '9615865',
96 'timestamp': 1337635207,
97 'upload_date': '20120521',
99 'license': 'all-rights-reserved',
102 'comment_count': int,
107 'skip_download': True,
113 'url': 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp',
114 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604',
118 'title': 'Youtube - Dl Test Video \'\' Ä↭',
119 'description': 'test chars: \"\'/\\ä↭',
120 'uploader': 'jaimeMF',
121 'uploader_id': '69767071',
122 'timestamp': 1386604920,
123 'upload_date': '20131209',
125 'license': 'all-rights-reserved',
128 'comment_count': int,
132 # private link (alt format)
134 'url': 'https://api.soundcloud.com/tracks/123998367?secret_token=s-8Pjrp',
135 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604',
139 'title': 'Youtube - Dl Test Video \'\' Ä↭',
140 'description': 'test chars: \"\'/\\ä↭',
141 'uploader': 'jaimeMF',
142 'uploader_id': '69767071',
143 'timestamp': 1386604920,
144 'upload_date': '20131209',
146 'license': 'all-rights-reserved',
149 'comment_count': int,
155 'url': 'https://soundcloud.com/oddsamples/bus-brakes',
156 'md5': '7624f2351f8a3b2e7cd51522496e7631',
160 'title': 'Bus Brakes',
161 'description': 'md5:0053ca6396e8d2fd7b7e1595ef12ab66',
162 'uploader': 'oddsamples',
163 'uploader_id': '73680509',
164 'timestamp': 1389232924,
165 'upload_date': '20140109',
167 'license': 'cc-by-sa',
170 'comment_count': int,
174 # private link, downloadable format
176 'url': 'https://soundcloud.com/oriuplift/uponly-238-no-talking-wav/s-AyZUd',
177 'md5': '64a60b16e617d41d0bef032b7f55441e',
181 'title': 'Uplifting Only 238 [No Talking] (incl. Alex Feed Guestmix) (Aug 31, 2017) [wav]',
182 'description': 'md5:fa20ee0fca76a3d6df8c7e57f3715366',
183 'uploader': 'Ori Uplift Music',
184 'uploader_id': '12563093',
185 'timestamp': 1504206263,
186 'upload_date': '20170831',
187 'duration': 7449.096,
188 'license': 'all-rights-reserved',
191 'comment_count': int,
195 # no album art, use avatar pic for thumbnail
197 'url': 'https://soundcloud.com/garyvee/sideways-prod-mad-real',
198 'md5': '59c7872bc44e5d99b7211891664760c2',
202 'title': 'Sideways (Prod. Mad Real)',
203 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
204 'uploader': 'garyvee',
205 'uploader_id': '2366352',
206 'timestamp': 1488152409,
207 'upload_date': '20170226',
209 'thumbnail': r're:https?://.*\.jpg',
210 'license': 'all-rights-reserved',
213 'comment_count': int,
217 'skip_download': True,
220 # not available via api.soundcloud.com/i1/tracks/id/streams
222 'url': 'https://soundcloud.com/giovannisarani/mezzo-valzer',
223 'md5': 'e22aecd2bc88e0e4e432d7dcc0a1abf7',
227 'title': 'Mezzo Valzer',
228 'description': 'md5:4138d582f81866a530317bae316e8b61',
229 'uploader': 'Giovanni Sarani',
230 'uploader_id': '3352531',
231 'timestamp': 1551394171,
232 'upload_date': '20190228',
234 'thumbnail': r're:https?://.*\.jpg',
235 'license': 'all-rights-reserved',
238 'comment_count': int,
241 'expected_warnings': ['Unable to download JSON metadata'],
245 _API_BASE = 'https://api.soundcloud.com/'
246 _API_V2_BASE = 'https://api-v2.soundcloud.com/'
247 _BASE_URL = 'https://soundcloud.com/'
248 _CLIENT_ID = 'UW9ajvMgVdMMW3cdeBi8lPfN6dvOVGji'
249 _IMAGE_REPL_RE = r'-([0-9a-z]+)\.jpg'
265 def _resolv_url(cls, url):
266 return SoundcloudIE._API_V2_BASE + 'resolve?url=' + url + '&client_id=' + cls._CLIENT_ID
268 def _extract_info_dict(self, info, full_title=None, secret_token=None, version=2):
269 track_id = compat_str(info['id'])
270 title = info['title']
271 track_base_url = self._API_BASE + 'tracks/%s' % track_id
275 query = {'client_id': self._CLIENT_ID}
277 query['secret_token'] = secret_token
279 if info.get('downloadable') and info.get('has_downloads_left'):
280 format_url = update_url_query(
281 info.get('download_url') or track_base_url + '/download', query)
282 format_urls.add(format_url)
284 v1_info = self._download_json(
285 track_base_url, track_id, query=query, fatal=False) or {}
289 'format_id': 'download',
290 'ext': v1_info.get('original_format') or 'mp3',
291 'filesize': int_or_none(v1_info.get('original_content_size')),
296 def invalid_url(url):
297 return not url or url in format_urls or re.search(r'/(?:preview|playlist)/0/30/', url)
299 def add_format(f, protocol):
300 mobj = re.search(r'\.(?P<abr>\d+)\.(?P<ext>[0-9a-z]{3,4})(?=[/?])', stream_url)
302 for k, v in mobj.groupdict().items():
307 format_id_list.append(protocol)
308 for k in ('ext', 'abr'):
311 format_id_list.append(v)
316 'format_id': '_'.join(format_id_list),
317 'protocol': 'm3u8_native' if protocol == 'hls' else 'http',
322 transcodings = try_get(
323 info, lambda x: x['media']['transcodings'], list) or []
324 for t in transcodings:
325 if not isinstance(t, dict):
327 format_url = url_or_none(t.get('url'))
328 if not format_url or t.get('snipped') or '/preview/' in format_url:
330 stream = self._download_json(
331 format_url, track_id, query=query, fatal=False)
332 if not isinstance(stream, dict):
334 stream_url = url_or_none(stream.get('url'))
335 if invalid_url(stream_url):
337 format_urls.add(stream_url)
338 stream_format = t.get('format') or {}
339 protocol = stream_format.get('protocol')
340 if protocol != 'hls' and '/hls' in format_url:
343 preset = str_or_none(t.get('preset'))
345 ext = preset.split('_')[0]
346 if ext not in KNOWN_EXTENSIONS:
347 ext = mimetype2ext(stream_format.get('mime_type'))
351 }, 'http' if protocol == 'progressive' else protocol)
354 # Old API, does not work for some tracks (e.g.
355 # https://soundcloud.com/giovannisarani/mezzo-valzer)
356 # and might serve preview URLs (e.g.
357 # http://www.soundcloud.com/snbrn/ele)
358 format_dict = self._download_json(
359 track_base_url + '/streams', track_id,
360 'Downloading track url', query=query, fatal=False) or {}
362 for key, stream_url in format_dict.items():
363 if invalid_url(stream_url):
365 format_urls.add(stream_url)
366 mobj = re.search(r'(http|hls)_([^_]+)_(\d+)_url', key)
368 protocol, ext, abr = mobj.groups()
376 # We fallback to the stream_url in the original info, this
377 # cannot be always used, sometimes it can give an HTTP 404 error
378 urlh = self._request_webpage(
379 HEADRequest(info.get('stream_url') or track_base_url + '/stream'),
380 track_id, query=query, fatal=False)
382 stream_url = urlh.geturl()
383 if not invalid_url(stream_url):
384 add_format({'url': stream_url}, 'http')
389 self._sort_formats(formats)
391 user = info.get('user') or {}
394 artwork_url = info.get('artwork_url')
395 thumbnail = artwork_url or user.get('avatar_url')
396 if isinstance(thumbnail, compat_str):
397 if re.search(self._IMAGE_REPL_RE, thumbnail):
398 for image_id, size in self._ARTWORK_MAP.items():
401 'url': re.sub(self._IMAGE_REPL_RE, '-%s.jpg' % image_id, thumbnail),
403 if image_id == 'tiny' and not artwork_url:
405 elif image_id == 'original':
414 thumbnails = [{'url': thumbnail}]
416 def extract_count(key):
417 return int_or_none(info.get('%s_count' % key))
421 'uploader': user.get('username'),
422 'uploader_id': str_or_none(user.get('id')) or user.get('permalink'),
423 'uploader_url': user.get('permalink_url'),
424 'timestamp': unified_timestamp(info.get('created_at')),
426 'description': info.get('description'),
427 'thumbnails': thumbnails,
428 'duration': float_or_none(info.get('duration'), 1000),
429 'webpage_url': info.get('permalink_url'),
430 'license': info.get('license'),
431 'view_count': extract_count('playback'),
432 'like_count': extract_count('favoritings') or extract_count('likes'),
433 'comment_count': extract_count('comment'),
434 'repost_count': extract_count('reposts'),
435 'genre': info.get('genre'),
439 def _real_extract(self, url):
440 mobj = re.match(self._VALID_URL, url)
442 track_id = mobj.group('track_id')
445 'client_id': self._CLIENT_ID,
448 info_json_url = self._API_V2_BASE + 'tracks/' + track_id
449 full_title = track_id
450 token = mobj.group('secret_token')
452 query['secret_token'] = token
454 full_title = resolve_title = '%s/%s' % mobj.group('uploader', 'title')
455 token = mobj.group('token')
457 resolve_title += '/%s' % token
458 info_json_url = self._resolv_url(self._BASE_URL + resolve_title)
461 info = self._download_json(
462 info_json_url, full_title, 'Downloading info JSON', query=query, fatal=False)
464 info = self._download_json(
465 info_json_url.replace(self._API_V2_BASE, self._API_BASE),
466 full_title, 'Downloading info JSON', query=query)
469 return self._extract_info_dict(info, full_title, token, version)
472 class SoundcloudPlaylistBaseIE(SoundcloudIE):
473 def _extract_track_entries(self, tracks, token=None):
476 track_id = str_or_none(track.get('id'))
477 url = track.get('permalink_url')
481 url = self._API_V2_BASE + 'tracks/' + track_id
483 url += '?secret_token=' + token
484 entries.append(self.url_result(
485 url, SoundcloudIE.ie_key(), track_id))
489 class SoundcloudSetIE(SoundcloudPlaylistBaseIE):
490 _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<uploader>[\w\d-]+)/sets/(?P<slug_title>[\w\d-]+)(?:/(?P<token>[^?/]+))?'
491 IE_NAME = 'soundcloud:set'
493 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep',
496 'title': 'The Royal Concept EP',
498 'playlist_mincount': 5,
500 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep/token',
501 'only_matching': True,
504 def _real_extract(self, url):
505 mobj = re.match(self._VALID_URL, url)
507 full_title = '%s/sets/%s' % mobj.group('uploader', 'slug_title')
508 token = mobj.group('token')
510 full_title += '/' + token
512 info = self._download_json(self._resolv_url(
513 self._BASE_URL + full_title), full_title)
516 msgs = (compat_str(err['error_message']) for err in info['errors'])
517 raise ExtractorError('unable to download video webpage: %s' % ','.join(msgs))
519 entries = self._extract_track_entries(info['tracks'], token)
521 return self.playlist_result(
522 entries, str_or_none(info.get('id')), info.get('title'))
525 class SoundcloudPagedPlaylistBaseIE(SoundcloudPlaylistBaseIE):
526 def _extract_playlist(self, base_url, playlist_id, playlist_title):
529 'client_id': self._CLIENT_ID,
530 'linked_partitioning': '1',
533 query = COMMON_QUERY.copy()
539 for i in itertools.count():
540 response = self._download_json(
541 next_href, playlist_id,
542 'Downloading track page %s' % (i + 1), query=query)
544 collection = response['collection']
546 if not isinstance(collection, list):
549 # Empty collection may be returned, in this case we proceed
550 # straight to next_href
552 def resolve_entry(candidates):
553 for cand in candidates:
554 if not isinstance(cand, dict):
556 permalink_url = url_or_none(cand.get('permalink_url'))
557 if not permalink_url:
559 return self.url_result(
561 SoundcloudIE.ie_key() if SoundcloudIE.suitable(permalink_url) else None,
562 str_or_none(cand.get('id')), cand.get('title'))
565 entry = resolve_entry((e, e.get('track'), e.get('playlist')))
567 entries.append(entry)
569 next_href = response.get('next_href')
573 next_href = response['next_href']
574 parsed_next_href = compat_urlparse.urlparse(next_href)
575 query = compat_urlparse.parse_qs(parsed_next_href.query)
576 query.update(COMMON_QUERY)
581 'title': playlist_title,
586 class SoundcloudUserIE(SoundcloudPagedPlaylistBaseIE):
587 _VALID_URL = r'''(?x)
589 (?:(?:www|m)\.)?soundcloud\.com/
592 (?P<rsrc>tracks|albums|sets|reposts|likes|spotlight)
596 IE_NAME = 'soundcloud:user'
598 'url': 'https://soundcloud.com/soft-cell-official',
601 'title': 'Soft Cell (All)',
603 'playlist_mincount': 28,
605 'url': 'https://soundcloud.com/soft-cell-official/tracks',
608 'title': 'Soft Cell (Tracks)',
610 'playlist_mincount': 27,
612 'url': 'https://soundcloud.com/soft-cell-official/albums',
615 'title': 'Soft Cell (Albums)',
617 'playlist_mincount': 1,
619 'url': 'https://soundcloud.com/jcv246/sets',
622 'title': 'Jordi / cv (Sets)',
624 'playlist_mincount': 2,
626 'url': 'https://soundcloud.com/jcv246/reposts',
629 'title': 'Jordi / cv (Reposts)',
631 'playlist_mincount': 6,
633 'url': 'https://soundcloud.com/clalberg/likes',
636 'title': 'clalberg (Likes)',
638 'playlist_mincount': 5,
640 'url': 'https://soundcloud.com/grynpyret/spotlight',
643 'title': 'Grynpyret (Spotlight)',
645 'playlist_mincount': 1,
649 'all': 'stream/users/%s',
650 'tracks': 'users/%s/tracks',
651 'albums': 'users/%s/albums',
652 'sets': 'users/%s/playlists',
653 'reposts': 'stream/users/%s/reposts',
654 'likes': 'users/%s/likes',
655 'spotlight': 'users/%s/spotlight',
658 def _real_extract(self, url):
659 mobj = re.match(self._VALID_URL, url)
660 uploader = mobj.group('user')
662 user = self._download_json(
663 self._resolv_url(self._BASE_URL + uploader),
664 uploader, 'Downloading user info')
666 resource = mobj.group('rsrc') or 'all'
668 return self._extract_playlist(
669 self._API_V2_BASE + self._BASE_URL_MAP[resource] % user['id'],
670 str_or_none(user.get('id')),
671 '%s (%s)' % (user['username'], resource.capitalize()))
674 class SoundcloudTrackStationIE(SoundcloudPagedPlaylistBaseIE):
675 _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/stations/track/[^/]+/(?P<id>[^/?#&]+)'
676 IE_NAME = 'soundcloud:trackstation'
678 'url': 'https://soundcloud.com/stations/track/officialsundial/your-text',
681 'title': 'Track station: your text',
683 'playlist_mincount': 47,
686 def _real_extract(self, url):
687 track_name = self._match_id(url)
689 track = self._download_json(self._resolv_url(url), track_name)
690 track_id = self._search_regex(
691 r'soundcloud:track-stations:(\d+)', track['id'], 'track id')
693 return self._extract_playlist(
694 self._API_V2_BASE + 'stations/%s/tracks' % track['id'],
695 track_id, 'Track station: %s' % track['title'])
698 class SoundcloudPlaylistIE(SoundcloudPlaylistBaseIE):
699 _VALID_URL = r'https?://api(?:-v2)?\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$'
700 IE_NAME = 'soundcloud:playlist'
702 'url': 'https://api.soundcloud.com/playlists/4110309',
705 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]',
706 'description': 're:.*?TILT Brass - Bowery Poetry Club',
711 def _real_extract(self, url):
712 mobj = re.match(self._VALID_URL, url)
713 playlist_id = mobj.group('id')
716 'client_id': self._CLIENT_ID,
718 token = mobj.group('token')
720 query['secret_token'] = token
722 data = self._download_json(
723 self._API_V2_BASE + 'playlists/' + playlist_id,
724 playlist_id, 'Downloading playlist', query=query)
726 entries = self._extract_track_entries(data['tracks'], token)
728 return self.playlist_result(
729 entries, playlist_id, data.get('title'), data.get('description'))
732 class SoundcloudSearchIE(SearchInfoExtractor, SoundcloudIE):
733 IE_NAME = 'soundcloud:search'
734 IE_DESC = 'Soundcloud search'
735 _MAX_RESULTS = float('inf')
737 'url': 'scsearch15:post-avant jazzcore',
739 'title': 'post-avant jazzcore',
741 'playlist_count': 15,
744 _SEARCH_KEY = 'scsearch'
745 _MAX_RESULTS_PER_PAGE = 200
746 _DEFAULT_RESULTS_PER_PAGE = 50
748 def _get_collection(self, endpoint, collection_id, **query):
750 query.get('limit', self._DEFAULT_RESULTS_PER_PAGE),
751 self._MAX_RESULTS_PER_PAGE)
754 'client_id': self._CLIENT_ID,
755 'linked_partitioning': 1,
758 next_url = update_url_query(self._API_V2_BASE + endpoint, query)
760 collected_results = 0
762 for i in itertools.count(1):
763 response = self._download_json(
764 next_url, collection_id, 'Downloading page {0}'.format(i),
765 'Unable to download API page')
767 collection = response.get('collection', [])
771 collection = list(filter(bool, collection))
772 collected_results += len(collection)
774 for item in collection:
775 yield self.url_result(item['uri'], SoundcloudIE.ie_key())
777 if not collection or collected_results >= limit:
780 next_url = response.get('next_href')
784 def _get_n_results(self, query, n):
785 tracks = self._get_collection('search/tracks', query, limit=n, q=query)
786 return self.playlist_result(tracks, playlist_title=query)