2 from __future__ import unicode_literals
7 from .common import InfoExtractor
19 class SoundcloudIE(InfoExtractor):
20 """Information extractor for soundcloud.com
21 To access the media, the uid of the song and a stream token
22 must be extracted from the page source and the script must make
23 a request to media.soundcloud.com/crossdomain.xml. Then
24 the media can be grabbed by requesting from an url composed
25 of the stream token and uid
28 _VALID_URL = r'''(?x)^(?:https?://)?
29 (?:(?:(?:www\.|m\.)?soundcloud\.com/
30 (?P<uploader>[\w\d-]+)/
31 (?!sets/|likes/?(?:$|[?#]))
33 (?P<token>[^?]+?)?(?:[?].*)?$)
34 |(?:api\.soundcloud\.com/tracks/(?P<track_id>\d+))
35 |(?P<player>(?:w|player|p.)\.soundcloud\.com/player/?.*?url=.*)
38 IE_NAME = 'soundcloud'
41 'url': 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy',
42 'file': '62986583.mp3',
43 'md5': 'ebef0a451b909710ed1d7787dddbf0d7',
45 "upload_date": "20121011",
46 "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",
47 "uploader": "E.T. ExTerrestrial Music",
48 "title": "Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1",
54 'url': 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep',
58 'title': 'Goldrushed',
59 'description': 'From Stockholm Sweden\r\nPovel / Magnus / Filip / David\r\nwww.theroyalconcept.com',
60 'uploader': 'The Royal Concept',
61 'upload_date': '20120521',
66 'skip_download': True,
71 'url': 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp',
72 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604',
76 'title': 'Youtube - Dl Test Video \'\' Ä↭',
77 'uploader': 'jaimeMF',
78 'description': 'test chars: \"\'/\\ä↭',
79 'upload_date': '20131209',
85 'url': 'https://soundcloud.com/oddsamples/bus-brakes',
86 'md5': '7624f2351f8a3b2e7cd51522496e7631',
90 'title': 'Bus Brakes',
91 'description': 'md5:0170be75dd395c96025d210d261c784e',
92 'uploader': 'oddsamples',
93 'upload_date': '20140109',
99 _CLIENT_ID = 'b45b1aa10f1ac2941910a7f0d10f8e28'
100 _IPHONE_CLIENT_ID = '376f225bf427445fc4bfb6b99b72e0bf'
102 def report_resolve(self, video_id):
103 """Report information extraction."""
104 self.to_screen('%s: Resolving id' % video_id)
107 def _resolv_url(cls, url):
108 return 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=' + cls._CLIENT_ID
110 def _extract_info_dict(self, info, full_title=None, quiet=False, secret_token=None):
111 track_id = compat_str(info['id'])
112 name = full_title or track_id
114 self.report_extraction(name)
116 thumbnail = info['artwork_url']
117 if thumbnail is not None:
118 thumbnail = thumbnail.replace('-large', '-t500x500')
122 'uploader': info['user']['username'],
123 'upload_date': unified_strdate(info['created_at']),
124 'title': info['title'],
125 'description': info['description'],
126 'thumbnail': thumbnail,
127 'duration': int_or_none(info.get('duration'), 1000),
130 if info.get('downloadable', False):
131 # We can build a direct link to the song
133 'https://api.soundcloud.com/tracks/{0}/download?client_id={1}'.format(
134 track_id, self._CLIENT_ID))
136 'format_id': 'download',
137 'ext': info.get('original_format', 'mp3'),
143 # We have to retrieve the url
144 streams_url = ('http://api.soundcloud.com/i1/tracks/{0}/streams?'
145 'client_id={1}&secret_token={2}'.format(track_id, self._IPHONE_CLIENT_ID, secret_token))
146 format_dict = self._download_json(
148 track_id, 'Downloading track url')
150 for key, stream_url in format_dict.items():
151 if key.startswith('http'):
158 elif key.startswith('rtmp'):
159 # The url doesn't have an rtmp app, we have to extract the playpath
160 url, path = stream_url.split('mp3:', 1)
164 'play_path': 'mp3:' + path,
170 # We fallback to the stream_url in the original info, this
171 # cannot be always used, sometimes it can give an HTTP 404 error
173 'format_id': 'fallback',
174 'url': info['stream_url'] + '?client_id=' + self._CLIENT_ID,
180 if f['format_id'].startswith('http'):
181 f['protocol'] = 'http'
182 if f['format_id'].startswith('rtmp'):
183 f['protocol'] = 'rtmp'
185 self._sort_formats(formats)
186 result['formats'] = formats
190 def _real_extract(self, url):
191 mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE)
193 raise ExtractorError('Invalid URL: %s' % url)
195 track_id = mobj.group('track_id')
197 if track_id is not None:
198 info_json_url = 'http://api.soundcloud.com/tracks/' + track_id + '.json?client_id=' + self._CLIENT_ID
199 full_title = track_id
200 elif mobj.group('player'):
201 query = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
202 return self.url_result(query['url'][0])
204 # extract uploader (which is in the url)
205 uploader = mobj.group('uploader')
206 # extract simple title (uploader + slug of song title)
207 slug_title = mobj.group('title')
208 token = mobj.group('token')
209 full_title = resolve_title = '%s/%s' % (uploader, slug_title)
211 resolve_title += '/%s' % token
213 self.report_resolve(full_title)
215 url = 'http://soundcloud.com/%s' % resolve_title
216 info_json_url = self._resolv_url(url)
217 info = self._download_json(info_json_url, full_title, 'Downloading info JSON')
219 return self._extract_info_dict(info, full_title, secret_token=token)
222 class SoundcloudSetIE(SoundcloudIE):
223 _VALID_URL = r'https?://(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)'
224 IE_NAME = 'soundcloud:set'
226 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep',
228 'title': 'The Royal Concept EP',
230 'playlist_mincount': 6,
233 def _real_extract(self, url):
234 mobj = re.match(self._VALID_URL, url)
236 # extract uploader (which is in the url)
237 uploader = mobj.group(1)
238 # extract simple title (uploader + slug of song title)
239 slug_title = mobj.group(2)
240 full_title = '%s/sets/%s' % (uploader, slug_title)
242 self.report_resolve(full_title)
244 url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title)
245 resolv_url = self._resolv_url(url)
246 info = self._download_json(resolv_url, full_title)
249 for err in info['errors']:
250 self._downloader.report_error('unable to download video webpage: %s' % compat_str(err['error_message']))
255 'entries': [self._extract_info_dict(track) for track in info['tracks']],
257 'title': info['title'],
261 class SoundcloudUserIE(SoundcloudIE):
262 _VALID_URL = r'https?://(www\.)?soundcloud\.com/(?P<user>[^/]+)/?((?P<rsrc>tracks|likes)/?)?(\?.*)?$'
263 IE_NAME = 'soundcloud:user'
265 'url': 'https://soundcloud.com/the-concept-band',
268 'title': 'The Royal Concept',
270 'playlist_mincount': 12
272 'url': 'https://soundcloud.com/the-concept-band/likes',
275 'title': 'The Royal Concept',
277 'playlist_mincount': 1,
280 def _real_extract(self, url):
281 mobj = re.match(self._VALID_URL, url)
282 uploader = mobj.group('user')
283 resource = mobj.group('rsrc')
286 elif resource == 'likes':
287 resource = 'favorites'
289 url = 'http://soundcloud.com/%s/' % uploader
290 resolv_url = self._resolv_url(url)
291 user = self._download_json(
292 resolv_url, uploader, 'Downloading user info')
293 base_url = 'http://api.soundcloud.com/users/%s/%s.json?' % (uploader, resource)
296 for i in itertools.count():
297 data = compat_urllib_parse.urlencode({
300 'client_id': self._CLIENT_ID,
302 new_entries = self._download_json(
303 base_url + data, uploader, 'Downloading track page %s' % (i + 1))
304 if len(new_entries) == 0:
305 self.to_screen('%s: End page received' % uploader)
307 entries.extend(self._extract_info_dict(e, quiet=True) for e in new_entries)
311 'id': compat_str(user['id']),
312 'title': user['username'],
317 class SoundcloudPlaylistIE(SoundcloudIE):
318 _VALID_URL = r'https?://api\.soundcloud\.com/playlists/(?P<id>[0-9]+)'
319 IE_NAME = 'soundcloud:playlist'
323 'url': 'http://api.soundcloud.com/playlists/4110309',
326 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]',
327 'description': 're:.*?TILT Brass - Bowery Poetry Club',
333 def _real_extract(self, url):
334 mobj = re.match(self._VALID_URL, url)
335 playlist_id = mobj.group('id')
336 base_url = '%s//api.soundcloud.com/playlists/%s.json?' % (self.http_scheme(), playlist_id)
338 data = compat_urllib_parse.urlencode({
339 'client_id': self._CLIENT_ID,
341 data = self._download_json(
342 base_url + data, playlist_id, 'Downloading playlist')
345 self._extract_info_dict(t, quiet=True) for t in data['tracks']]
350 'title': data.get('title'),
351 'description': data.get('description'),