2 from __future__ import unicode_literals
11 from ..compat import (
32 class SoundcloudEmbedIE(InfoExtractor):
33 _VALID_URL = r'https?://(?:w|player|p)\.soundcloud\.com/player/?.*?\burl=(?P<id>.+)'
35 # from https://www.soundi.fi/uutiset/ennakkokuuntelussa-timo-kaukolammen-station-to-station-to-station-julkaisua-juhlitaan-tanaan-g-livelabissa/
36 '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',
37 'only_matching': True,
41 def _extract_urls(webpage):
42 return [m.group('url') for m in re.finditer(
43 r'<iframe[^>]+src=(["\'])(?P<url>(?:https?://)?(?:w\.)?soundcloud\.com/player.+?)\1',
46 def _real_extract(self, url):
47 query = compat_urlparse.parse_qs(
48 compat_urlparse.urlparse(url).query)
49 api_url = query['url'][0]
50 secret_token = query.get('secret_token')
52 api_url = update_url_query(api_url, {'secret_token': secret_token[0]})
53 return self.url_result(api_url)
56 class SoundcloudIE(InfoExtractor):
57 """Information extractor for soundcloud.com
58 To access the media, the uid of the song and a stream token
59 must be extracted from the page source and the script must make
60 a request to media.soundcloud.com/crossdomain.xml. Then
61 the media can be grabbed by requesting from an url composed
62 of the stream token and uid
65 _VALID_URL = r'''(?x)^(?:https?://)?
66 (?:(?:(?:www\.|m\.)?soundcloud\.com/
68 (?P<uploader>[\w\d-]+)/
69 (?!(?:tracks|albums|sets(?:/.+?)?|reposts|likes|spotlight)/?(?:$|[?#]))
71 (?P<token>[^?]+?)?(?:[?].*)?$)
72 |(?:api(?:-v2)?\.soundcloud\.com/tracks/(?P<track_id>\d+)
73 (?:/?\?secret_token=(?P<secret_token>[^&]+))?)
76 IE_NAME = 'soundcloud'
79 'url': 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy',
80 'md5': 'ebef0a451b909710ed1d7787dddbf0d7',
84 'title': 'Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1',
85 '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',
86 'uploader': 'E.T. ExTerrestrial Music',
87 'uploader_id': '1571244',
88 'timestamp': 1349920598,
89 'upload_date': '20121011',
91 'license': 'all-rights-reserved',
100 'url': 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep',
104 'title': 'Goldrushed',
105 'description': 'From Stockholm Sweden\r\nPovel / Magnus / Filip / David\r\nwww.theroyalconcept.com',
106 'uploader': 'The Royal Concept',
107 'uploader_id': '9615865',
108 'timestamp': 1337635207,
109 'upload_date': '20120521',
111 'license': 'all-rights-reserved',
114 'comment_count': int,
119 'skip_download': True,
125 'url': 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp',
126 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604',
130 'title': 'Youtube - Dl Test Video \'\' Ä↭',
131 'description': 'test chars: \"\'/\\ä↭',
132 'uploader': 'jaimeMF',
133 'uploader_id': '69767071',
134 'timestamp': 1386604920,
135 'upload_date': '20131209',
137 'license': 'all-rights-reserved',
140 'comment_count': int,
144 # private link (alt format)
146 'url': 'https://api.soundcloud.com/tracks/123998367?secret_token=s-8Pjrp',
147 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604',
151 'title': 'Youtube - Dl Test Video \'\' Ä↭',
152 'description': 'test chars: \"\'/\\ä↭',
153 'uploader': 'jaimeMF',
154 'uploader_id': '69767071',
155 'timestamp': 1386604920,
156 'upload_date': '20131209',
158 'license': 'all-rights-reserved',
161 'comment_count': int,
167 'url': 'https://soundcloud.com/oddsamples/bus-brakes',
168 'md5': '7624f2351f8a3b2e7cd51522496e7631',
172 'title': 'Bus Brakes',
173 'description': 'md5:0053ca6396e8d2fd7b7e1595ef12ab66',
174 'uploader': 'oddsamples',
175 'uploader_id': '73680509',
176 'timestamp': 1389232924,
177 'upload_date': '20140109',
179 'license': 'cc-by-sa',
182 'comment_count': int,
186 # private link, downloadable format
188 'url': 'https://soundcloud.com/oriuplift/uponly-238-no-talking-wav/s-AyZUd',
189 'md5': '64a60b16e617d41d0bef032b7f55441e',
193 'title': 'Uplifting Only 238 [No Talking] (incl. Alex Feed Guestmix) (Aug 31, 2017) [wav]',
194 'description': 'md5:fa20ee0fca76a3d6df8c7e57f3715366',
195 'uploader': 'Ori Uplift Music',
196 'uploader_id': '12563093',
197 'timestamp': 1504206263,
198 'upload_date': '20170831',
199 'duration': 7449.096,
200 'license': 'all-rights-reserved',
203 'comment_count': int,
207 # no album art, use avatar pic for thumbnail
209 'url': 'https://soundcloud.com/garyvee/sideways-prod-mad-real',
210 'md5': '59c7872bc44e5d99b7211891664760c2',
214 'title': 'Sideways (Prod. Mad Real)',
215 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
216 'uploader': 'garyvee',
217 'uploader_id': '2366352',
218 'timestamp': 1488152409,
219 'upload_date': '20170226',
221 'thumbnail': r're:https?://.*\.jpg',
222 'license': 'all-rights-reserved',
225 'comment_count': int,
229 'skip_download': True,
232 # not available via api.soundcloud.com/i1/tracks/id/streams
234 'url': 'https://soundcloud.com/giovannisarani/mezzo-valzer',
235 'md5': 'e22aecd2bc88e0e4e432d7dcc0a1abf7',
239 'title': 'Mezzo Valzer',
240 'description': 'md5:4138d582f81866a530317bae316e8b61',
241 'uploader': 'Giovanni Sarani',
242 'uploader_id': '3352531',
243 'timestamp': 1551394171,
244 'upload_date': '20190228',
246 'thumbnail': r're:https?://.*\.jpg',
247 'license': 'all-rights-reserved',
250 'comment_count': int,
253 'expected_warnings': ['Unable to download JSON metadata'],
257 _API_BASE = 'https://api.soundcloud.com/'
258 _API_V2_BASE = 'https://api-v2.soundcloud.com/'
259 _BASE_URL = 'https://soundcloud.com/'
260 _IMAGE_REPL_RE = r'-([0-9a-z]+)\.jpg'
275 def _update_client_id(self):
276 webpage = self._download_webpage('https://soundcloud.com/', None)
277 for src in reversed(re.findall(r'<script[^>]+src="([^"]+)"', webpage)):
278 script = self._download_webpage(src, None, fatal=False)
280 client_id = self._search_regex(
281 r'client_id\s*:\s*"([0-9a-zA-Z]{32})"',
282 script, 'client id', default=None)
284 self._CLIENT_ID = client_id
285 self._downloader.cache.store('soundcloud', 'client_id', client_id)
287 raise ExtractorError('Unable to extract client id')
289 def _download_json(self, *args, **kwargs):
290 query = kwargs.get('query', {}).copy()
292 query['client_id'] = self._CLIENT_ID
293 kwargs['query'] = query
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._update_client_id()
302 def _real_initialize(self):
303 self._CLIENT_ID = self._downloader.cache.load('soundcloud', 'client_id') or 'YUKXoArFcqrlQn9tfNHvvyfnDISj04zk'
306 def _resolv_url(cls, url):
307 return SoundcloudIE._API_V2_BASE + 'resolve?url=' + url
309 def _extract_info_dict(self, info, full_title=None, secret_token=None, version=2):
310 track_id = compat_str(info['id'])
311 title = info['title']
312 track_base_url = self._API_BASE + 'tracks/%s' % track_id
316 query = {'client_id': self._CLIENT_ID}
318 query['secret_token'] = secret_token
320 if info.get('downloadable') and info.get('has_downloads_left'):
321 format_url = update_url_query(
322 info.get('download_url') or track_base_url + '/download', query)
323 format_urls.add(format_url)
325 v1_info = self._download_json(
326 track_base_url, track_id, query=query, fatal=False) or {}
330 'format_id': 'download',
331 'ext': v1_info.get('original_format') or 'mp3',
332 'filesize': int_or_none(v1_info.get('original_content_size')),
337 def invalid_url(url):
338 return not url or url in format_urls or re.search(r'/(?:preview|playlist)/0/30/', url)
340 def add_format(f, protocol):
341 mobj = re.search(r'\.(?P<abr>\d+)\.(?P<ext>[0-9a-z]{3,4})(?=[/?])', stream_url)
343 for k, v in mobj.groupdict().items():
348 format_id_list.append(protocol)
349 for k in ('ext', 'abr'):
352 format_id_list.append(v)
357 'format_id': '_'.join(format_id_list),
358 'protocol': 'm3u8_native' if protocol == 'hls' else 'http',
363 transcodings = try_get(
364 info, lambda x: x['media']['transcodings'], list) or []
365 for t in transcodings:
366 if not isinstance(t, dict):
368 format_url = url_or_none(t.get('url'))
369 if not format_url or t.get('snipped') or '/preview/' in format_url:
371 stream = self._download_json(
372 format_url, track_id, query=query, fatal=False)
373 if not isinstance(stream, dict):
375 stream_url = url_or_none(stream.get('url'))
376 if invalid_url(stream_url):
378 format_urls.add(stream_url)
379 stream_format = t.get('format') or {}
380 protocol = stream_format.get('protocol')
381 if protocol != 'hls' and '/hls' in format_url:
384 preset = str_or_none(t.get('preset'))
386 ext = preset.split('_')[0]
387 if ext not in KNOWN_EXTENSIONS:
388 ext = mimetype2ext(stream_format.get('mime_type'))
392 }, 'http' if protocol == 'progressive' else protocol)
395 # Old API, does not work for some tracks (e.g.
396 # https://soundcloud.com/giovannisarani/mezzo-valzer)
397 # and might serve preview URLs (e.g.
398 # http://www.soundcloud.com/snbrn/ele)
399 format_dict = self._download_json(
400 track_base_url + '/streams', track_id,
401 'Downloading track url', query=query, fatal=False) or {}
403 for key, stream_url in format_dict.items():
404 if invalid_url(stream_url):
406 format_urls.add(stream_url)
407 mobj = re.search(r'(http|hls)_([^_]+)_(\d+)_url', key)
409 protocol, ext, abr = mobj.groups()
417 # We fallback to the stream_url in the original info, this
418 # cannot be always used, sometimes it can give an HTTP 404 error
419 urlh = self._request_webpage(
420 HEADRequest(info.get('stream_url') or track_base_url + '/stream'),
421 track_id, query=query, fatal=False)
423 stream_url = urlh.geturl()
424 if not invalid_url(stream_url):
425 add_format({'url': stream_url}, 'http')
430 self._sort_formats(formats)
432 user = info.get('user') or {}
435 artwork_url = info.get('artwork_url')
436 thumbnail = artwork_url or user.get('avatar_url')
437 if isinstance(thumbnail, compat_str):
438 if re.search(self._IMAGE_REPL_RE, thumbnail):
439 for image_id, size in self._ARTWORK_MAP.items():
442 'url': re.sub(self._IMAGE_REPL_RE, '-%s.jpg' % image_id, thumbnail),
444 if image_id == 'tiny' and not artwork_url:
446 elif image_id == 'original':
455 thumbnails = [{'url': thumbnail}]
457 def extract_count(key):
458 return int_or_none(info.get('%s_count' % key))
462 'uploader': user.get('username'),
463 'uploader_id': str_or_none(user.get('id')) or user.get('permalink'),
464 'uploader_url': user.get('permalink_url'),
465 'timestamp': unified_timestamp(info.get('created_at')),
467 'description': info.get('description'),
468 'thumbnails': thumbnails,
469 'duration': float_or_none(info.get('duration'), 1000),
470 'webpage_url': info.get('permalink_url'),
471 'license': info.get('license'),
472 'view_count': extract_count('playback'),
473 'like_count': extract_count('favoritings') or extract_count('likes'),
474 'comment_count': extract_count('comment'),
475 'repost_count': extract_count('reposts'),
476 'genre': info.get('genre'),
480 def _real_extract(self, url):
481 mobj = re.match(self._VALID_URL, url)
483 track_id = mobj.group('track_id')
487 info_json_url = self._API_V2_BASE + 'tracks/' + track_id
488 full_title = track_id
489 token = mobj.group('secret_token')
491 query['secret_token'] = token
493 full_title = resolve_title = '%s/%s' % mobj.group('uploader', 'title')
494 token = mobj.group('token')
496 resolve_title += '/%s' % token
497 info_json_url = self._resolv_url(self._BASE_URL + resolve_title)
500 info = self._download_json(
501 info_json_url, full_title, 'Downloading info JSON', query=query, fatal=False)
503 info = self._download_json(
504 info_json_url.replace(self._API_V2_BASE, self._API_BASE),
505 full_title, 'Downloading info JSON', query=query)
508 return self._extract_info_dict(info, full_title, token, version)
511 class SoundcloudPlaylistBaseIE(SoundcloudIE):
512 def _extract_track_entries(self, tracks, token=None):
515 track_id = str_or_none(track.get('id'))
516 url = track.get('permalink_url')
520 url = self._API_V2_BASE + 'tracks/' + track_id
522 url += '?secret_token=' + token
523 entries.append(self.url_result(
524 url, SoundcloudIE.ie_key(), track_id))
528 class SoundcloudSetIE(SoundcloudPlaylistBaseIE):
529 _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<uploader>[\w\d-]+)/sets/(?P<slug_title>[\w\d-]+)(?:/(?P<token>[^?/]+))?'
530 IE_NAME = 'soundcloud:set'
532 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep',
535 'title': 'The Royal Concept EP',
537 'playlist_mincount': 5,
539 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep/token',
540 'only_matching': True,
543 def _real_extract(self, url):
544 mobj = re.match(self._VALID_URL, url)
546 full_title = '%s/sets/%s' % mobj.group('uploader', 'slug_title')
547 token = mobj.group('token')
549 full_title += '/' + token
551 info = self._download_json(self._resolv_url(
552 self._BASE_URL + full_title), full_title)
555 msgs = (compat_str(err['error_message']) for err in info['errors'])
556 raise ExtractorError('unable to download video webpage: %s' % ','.join(msgs))
558 entries = self._extract_track_entries(info['tracks'], token)
560 return self.playlist_result(
561 entries, str_or_none(info.get('id')), info.get('title'))
564 class SoundcloudPagedPlaylistBaseIE(SoundcloudPlaylistBaseIE):
565 def _extract_playlist(self, base_url, playlist_id, playlist_title):
568 'linked_partitioning': '1',
571 query = COMMON_QUERY.copy()
577 for i in itertools.count():
578 response = self._download_json(
579 next_href, playlist_id,
580 'Downloading track page %s' % (i + 1), query=query)
582 collection = response['collection']
584 if not isinstance(collection, list):
587 # Empty collection may be returned, in this case we proceed
588 # straight to next_href
590 def resolve_entry(candidates):
591 for cand in candidates:
592 if not isinstance(cand, dict):
594 permalink_url = url_or_none(cand.get('permalink_url'))
595 if not permalink_url:
597 return self.url_result(
599 SoundcloudIE.ie_key() if SoundcloudIE.suitable(permalink_url) else None,
600 str_or_none(cand.get('id')), cand.get('title'))
603 entry = resolve_entry((e, e.get('track'), e.get('playlist')))
605 entries.append(entry)
607 next_href = response.get('next_href')
611 next_href = response['next_href']
612 parsed_next_href = compat_urlparse.urlparse(next_href)
613 query = compat_urlparse.parse_qs(parsed_next_href.query)
614 query.update(COMMON_QUERY)
619 'title': playlist_title,
624 class SoundcloudUserIE(SoundcloudPagedPlaylistBaseIE):
625 _VALID_URL = r'''(?x)
627 (?:(?:www|m)\.)?soundcloud\.com/
630 (?P<rsrc>tracks|albums|sets|reposts|likes|spotlight)
634 IE_NAME = 'soundcloud:user'
636 'url': 'https://soundcloud.com/soft-cell-official',
639 'title': 'Soft Cell (All)',
641 'playlist_mincount': 28,
643 'url': 'https://soundcloud.com/soft-cell-official/tracks',
646 'title': 'Soft Cell (Tracks)',
648 'playlist_mincount': 27,
650 'url': 'https://soundcloud.com/soft-cell-official/albums',
653 'title': 'Soft Cell (Albums)',
655 'playlist_mincount': 1,
657 'url': 'https://soundcloud.com/jcv246/sets',
660 'title': 'Jordi / cv (Sets)',
662 'playlist_mincount': 2,
664 'url': 'https://soundcloud.com/jcv246/reposts',
667 'title': 'Jordi / cv (Reposts)',
669 'playlist_mincount': 6,
671 'url': 'https://soundcloud.com/clalberg/likes',
674 'title': 'clalberg (Likes)',
676 'playlist_mincount': 5,
678 'url': 'https://soundcloud.com/grynpyret/spotlight',
681 'title': 'Grynpyret (Spotlight)',
683 'playlist_mincount': 1,
687 'all': 'stream/users/%s',
688 'tracks': 'users/%s/tracks',
689 'albums': 'users/%s/albums',
690 'sets': 'users/%s/playlists',
691 'reposts': 'stream/users/%s/reposts',
692 'likes': 'users/%s/likes',
693 'spotlight': 'users/%s/spotlight',
696 def _real_extract(self, url):
697 mobj = re.match(self._VALID_URL, url)
698 uploader = mobj.group('user')
700 user = self._download_json(
701 self._resolv_url(self._BASE_URL + uploader),
702 uploader, 'Downloading user info')
704 resource = mobj.group('rsrc') or 'all'
706 return self._extract_playlist(
707 self._API_V2_BASE + self._BASE_URL_MAP[resource] % user['id'],
708 str_or_none(user.get('id')),
709 '%s (%s)' % (user['username'], resource.capitalize()))
712 class SoundcloudTrackStationIE(SoundcloudPagedPlaylistBaseIE):
713 _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/stations/track/[^/]+/(?P<id>[^/?#&]+)'
714 IE_NAME = 'soundcloud:trackstation'
716 'url': 'https://soundcloud.com/stations/track/officialsundial/your-text',
719 'title': 'Track station: your text',
721 'playlist_mincount': 47,
724 def _real_extract(self, url):
725 track_name = self._match_id(url)
727 track = self._download_json(self._resolv_url(url), track_name)
728 track_id = self._search_regex(
729 r'soundcloud:track-stations:(\d+)', track['id'], 'track id')
731 return self._extract_playlist(
732 self._API_V2_BASE + 'stations/%s/tracks' % track['id'],
733 track_id, 'Track station: %s' % track['title'])
736 class SoundcloudPlaylistIE(SoundcloudPlaylistBaseIE):
737 _VALID_URL = r'https?://api(?:-v2)?\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$'
738 IE_NAME = 'soundcloud:playlist'
740 'url': 'https://api.soundcloud.com/playlists/4110309',
743 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]',
744 'description': 're:.*?TILT Brass - Bowery Poetry Club',
749 def _real_extract(self, url):
750 mobj = re.match(self._VALID_URL, url)
751 playlist_id = mobj.group('id')
754 token = mobj.group('token')
756 query['secret_token'] = token
758 data = self._download_json(
759 self._API_V2_BASE + 'playlists/' + playlist_id,
760 playlist_id, 'Downloading playlist', query=query)
762 entries = self._extract_track_entries(data['tracks'], token)
764 return self.playlist_result(
765 entries, playlist_id, data.get('title'), data.get('description'))
768 class SoundcloudSearchIE(SearchInfoExtractor, SoundcloudIE):
769 IE_NAME = 'soundcloud:search'
770 IE_DESC = 'Soundcloud search'
771 _MAX_RESULTS = float('inf')
773 'url': 'scsearch15:post-avant jazzcore',
775 'title': 'post-avant jazzcore',
777 'playlist_count': 15,
780 _SEARCH_KEY = 'scsearch'
781 _MAX_RESULTS_PER_PAGE = 200
782 _DEFAULT_RESULTS_PER_PAGE = 50
784 def _get_collection(self, endpoint, collection_id, **query):
786 query.get('limit', self._DEFAULT_RESULTS_PER_PAGE),
787 self._MAX_RESULTS_PER_PAGE)
790 'linked_partitioning': 1,
793 next_url = update_url_query(self._API_V2_BASE + endpoint, query)
795 collected_results = 0
797 for i in itertools.count(1):
798 response = self._download_json(
799 next_url, collection_id, 'Downloading page {0}'.format(i),
800 'Unable to download API page')
802 collection = response.get('collection', [])
806 collection = list(filter(bool, collection))
807 collected_results += len(collection)
809 for item in collection:
810 yield self.url_result(item['uri'], SoundcloudIE.ie_key())
812 if not collection or collected_results >= limit:
815 next_url = response.get('next_href')
819 def _get_n_results(self, query, n):
820 tracks = self._get_collection('search/tracks', query, limit=n, q=query)
821 return self.playlist_result(tracks, playlist_title=query)