]>
Commit | Line | Data |
---|---|---|
de2dd4c5 | 1 | # encoding: utf-8 |
fbcd7b5f PH |
2 | from __future__ import unicode_literals |
3 | ||
aad0d6d5 | 4 | import re |
92790f4e | 5 | import itertools |
aad0d6d5 PH |
6 | |
7 | from .common import InfoExtractor | |
1cc79574 | 8 | from ..compat import ( |
aad0d6d5 | 9 | compat_str, |
668de34c | 10 | compat_urlparse, |
92790f4e | 11 | compat_urllib_parse, |
1cc79574 PH |
12 | ) |
13 | from ..utils import ( | |
aad0d6d5 | 14 | ExtractorError, |
eb920777 | 15 | int_or_none, |
aad0d6d5 PH |
16 | unified_strdate, |
17 | ) | |
18 | ||
19 | ||
20 | class SoundcloudIE(InfoExtractor): | |
21 | """Information extractor for soundcloud.com | |
22 | To access the media, the uid of the song and a stream token | |
23 | must be extracted from the page source and the script must make | |
24 | a request to media.soundcloud.com/crossdomain.xml. Then | |
25 | the media can be grabbed by requesting from an url composed | |
26 | of the stream token and uid | |
27 | """ | |
28 | ||
20991253 | 29 | _VALID_URL = r'''(?x)^(?:https?://)? |
71507a11 | 30 | (?:(?:(?:www\.|m\.)?soundcloud\.com/ |
4ff50ef8 | 31 | (?P<uploader>[\w\d-]+)/ |
22a6f150 PH |
32 | (?!sets/|likes/?(?:$|[?#])) |
33 | (?P<title>[\w\d-]+)/? | |
de2dd4c5 | 34 | (?P<token>[^?]+?)?(?:[?].*)?$) |
9296738f | 35 | |(?:api\.soundcloud\.com/tracks/(?P<track_id>\d+) |
0403b069 | 36 | (?:/?\?secret_token=(?P<secret_token>[^&]+))?) |
31c1cf5a | 37 | |(?P<player>(?:w|player|p.)\.soundcloud\.com/player/?.*?url=.*) |
eb6a41ba JMF |
38 | ) |
39 | ''' | |
fbcd7b5f | 40 | IE_NAME = 'soundcloud' |
12c167c8 JMF |
41 | _TESTS = [ |
42 | { | |
fbcd7b5f | 43 | 'url': 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy', |
fbcd7b5f PH |
44 | 'md5': 'ebef0a451b909710ed1d7787dddbf0d7', |
45 | 'info_dict': { | |
0eb9fb9f JMF |
46 | 'id': '62986583', |
47 | 'ext': 'mp3', | |
48 | 'upload_date': '20121011', | |
49 | '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', | |
50 | 'uploader': 'E.T. ExTerrestrial Music', | |
51 | 'title': 'Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1', | |
52 | 'duration': 143, | |
12c167c8 JMF |
53 | } |
54 | }, | |
55 | # not streamable song | |
56 | { | |
fbcd7b5f PH |
57 | 'url': 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep', |
58 | 'info_dict': { | |
59 | 'id': '47127627', | |
60 | 'ext': 'mp3', | |
61 | 'title': 'Goldrushed', | |
63ad0315 | 62 | 'description': 'From Stockholm Sweden\r\nPovel / Magnus / Filip / David\r\nwww.theroyalconcept.com', |
fbcd7b5f PH |
63 | 'uploader': 'The Royal Concept', |
64 | 'upload_date': '20120521', | |
eb920777 | 65 | 'duration': 227, |
12c167c8 | 66 | }, |
fbcd7b5f | 67 | 'params': { |
12c167c8 | 68 | # rtmp |
fbcd7b5f | 69 | 'skip_download': True, |
12c167c8 JMF |
70 | }, |
71 | }, | |
de2dd4c5 JMF |
72 | # private link |
73 | { | |
fbcd7b5f PH |
74 | 'url': 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp', |
75 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
76 | 'info_dict': { | |
77 | 'id': '123998367', | |
78 | 'ext': 'mp3', | |
79 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
80 | 'uploader': 'jaimeMF', | |
81 | 'description': 'test chars: \"\'/\\ä↭', | |
82 | 'upload_date': '20131209', | |
eb920777 | 83 | 'duration': 9, |
de2dd4c5 JMF |
84 | }, |
85 | }, | |
9296738f | 86 | # private link (alt format) |
87 | { | |
88 | 'url': 'https://api.soundcloud.com/tracks/123998367?secret_token=s-8Pjrp', | |
89 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
90 | 'info_dict': { | |
91 | 'id': '123998367', | |
92 | 'ext': 'mp3', | |
93 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
94 | 'uploader': 'jaimeMF', | |
95 | 'description': 'test chars: \"\'/\\ä↭', | |
96 | 'upload_date': '20131209', | |
97 | 'duration': 9, | |
98 | }, | |
99 | }, | |
f67ca84d JMF |
100 | # downloadable song |
101 | { | |
00a82ea8 | 102 | 'url': 'https://soundcloud.com/oddsamples/bus-brakes', |
eae12e3f | 103 | 'md5': '7624f2351f8a3b2e7cd51522496e7631', |
fbcd7b5f | 104 | 'info_dict': { |
00a82ea8 | 105 | 'id': '128590877', |
eae12e3f | 106 | 'ext': 'mp3', |
00a82ea8 | 107 | 'title': 'Bus Brakes', |
0eb9fb9f | 108 | 'description': 'md5:0053ca6396e8d2fd7b7e1595ef12ab66', |
00a82ea8 S |
109 | 'uploader': 'oddsamples', |
110 | 'upload_date': '20140109', | |
111 | 'duration': 17, | |
f67ca84d JMF |
112 | }, |
113 | }, | |
12c167c8 | 114 | ] |
aad0d6d5 | 115 | |
7d239269 | 116 | _CLIENT_ID = 'b45b1aa10f1ac2941910a7f0d10f8e28' |
64bb5187 | 117 | _IPHONE_CLIENT_ID = '376f225bf427445fc4bfb6b99b72e0bf' |
7d239269 | 118 | |
aad0d6d5 PH |
119 | def report_resolve(self, video_id): |
120 | """Report information extraction.""" | |
83622b6d | 121 | self.to_screen('%s: Resolving id' % video_id) |
aad0d6d5 | 122 | |
7d239269 JMF |
123 | @classmethod |
124 | def _resolv_url(cls, url): | |
125 | return 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=' + cls._CLIENT_ID | |
126 | ||
de2dd4c5 | 127 | def _extract_info_dict(self, info, full_title=None, quiet=False, secret_token=None): |
12c167c8 JMF |
128 | track_id = compat_str(info['id']) |
129 | name = full_title or track_id | |
2a15e706 | 130 | if quiet: |
92790f4e | 131 | self.report_extraction(name) |
7d239269 JMF |
132 | |
133 | thumbnail = info['artwork_url'] | |
134 | if thumbnail is not None: | |
135 | thumbnail = thumbnail.replace('-large', '-t500x500') | |
fbcd7b5f | 136 | ext = 'mp3' |
12c167c8 | 137 | result = { |
2a15e706 | 138 | 'id': track_id, |
7d239269 JMF |
139 | 'uploader': info['user']['username'], |
140 | 'upload_date': unified_strdate(info['created_at']), | |
2a15e706 | 141 | 'title': info['title'], |
7d239269 JMF |
142 | 'description': info['description'], |
143 | 'thumbnail': thumbnail, | |
eb920777 | 144 | 'duration': int_or_none(info.get('duration'), 1000), |
579657ad | 145 | 'webpage_url': info.get('permalink_url'), |
7d239269 | 146 | } |
5e114e4b | 147 | formats = [] |
12c167c8 | 148 | if info.get('downloadable', False): |
64bb5187 | 149 | # We can build a direct link to the song |
2a15e706 | 150 | format_url = ( |
fbcd7b5f | 151 | 'https://api.soundcloud.com/tracks/{0}/download?client_id={1}'.format( |
2a15e706 | 152 | track_id, self._CLIENT_ID)) |
5e114e4b | 153 | formats.append({ |
2a15e706 | 154 | 'format_id': 'download', |
fbcd7b5f | 155 | 'ext': info.get('original_format', 'mp3'), |
2a15e706 | 156 | 'url': format_url, |
fb04e403 | 157 | 'vcodec': 'none', |
5e114e4b PH |
158 | 'preference': 10, |
159 | }) | |
160 | ||
161 | # We have to retrieve the url | |
162 | streams_url = ('http://api.soundcloud.com/i1/tracks/{0}/streams?' | |
9e1a5b84 | 163 | 'client_id={1}&secret_token={2}'.format(track_id, self._IPHONE_CLIENT_ID, secret_token)) |
20991253 | 164 | format_dict = self._download_json( |
5e114e4b PH |
165 | streams_url, |
166 | track_id, 'Downloading track url') | |
167 | ||
5e114e4b PH |
168 | for key, stream_url in format_dict.items(): |
169 | if key.startswith('http'): | |
170 | formats.append({ | |
171 | 'format_id': key, | |
172 | 'ext': ext, | |
173 | 'url': stream_url, | |
174 | 'vcodec': 'none', | |
175 | }) | |
176 | elif key.startswith('rtmp'): | |
177 | # The url doesn't have an rtmp app, we have to extract the playpath | |
178 | url, path = stream_url.split('mp3:', 1) | |
179 | formats.append({ | |
180 | 'format_id': key, | |
181 | 'url': url, | |
182 | 'play_path': 'mp3:' + path, | |
183 | 'ext': ext, | |
184 | 'vcodec': 'none', | |
185 | }) | |
2a15e706 PH |
186 | |
187 | if not formats: | |
64bb5187 JMF |
188 | # We fallback to the stream_url in the original info, this |
189 | # cannot be always used, sometimes it can give an HTTP 404 error | |
2a15e706 | 190 | formats.append({ |
fbcd7b5f | 191 | 'format_id': 'fallback', |
2a15e706 PH |
192 | 'url': info['stream_url'] + '?client_id=' + self._CLIENT_ID, |
193 | 'ext': ext, | |
fb04e403 | 194 | 'vcodec': 'none', |
2a15e706 PH |
195 | }) |
196 | ||
fbcd7b5f | 197 | for f in formats: |
2a15e706 | 198 | if f['format_id'].startswith('http'): |
fbcd7b5f | 199 | f['protocol'] = 'http' |
2a15e706 | 200 | if f['format_id'].startswith('rtmp'): |
fbcd7b5f | 201 | f['protocol'] = 'rtmp' |
2a15e706 | 202 | |
fbcd7b5f | 203 | self._sort_formats(formats) |
2a15e706 | 204 | result['formats'] = formats |
64bb5187 | 205 | |
12c167c8 | 206 | return result |
7d239269 | 207 | |
aad0d6d5 | 208 | def _real_extract(self, url): |
eb6a41ba | 209 | mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE) |
aad0d6d5 | 210 | if mobj is None: |
83622b6d | 211 | raise ExtractorError('Invalid URL: %s' % url) |
aad0d6d5 | 212 | |
eb6a41ba | 213 | track_id = mobj.group('track_id') |
de2dd4c5 | 214 | token = None |
eb6a41ba JMF |
215 | if track_id is not None: |
216 | info_json_url = 'http://api.soundcloud.com/tracks/' + track_id + '.json?client_id=' + self._CLIENT_ID | |
217 | full_title = track_id | |
9296738f | 218 | token = mobj.group('secret_token') |
219 | if token: | |
220 | info_json_url += "&secret_token=" + token | |
31c1cf5a | 221 | elif mobj.group('player'): |
668de34c | 222 | query = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query) |
20991253 | 223 | return self.url_result(query['url'][0]) |
eb6a41ba JMF |
224 | else: |
225 | # extract uploader (which is in the url) | |
de2dd4c5 | 226 | uploader = mobj.group('uploader') |
eb6a41ba | 227 | # extract simple title (uploader + slug of song title) |
8bcc8756 | 228 | slug_title = mobj.group('title') |
de2dd4c5 JMF |
229 | token = mobj.group('token') |
230 | full_title = resolve_title = '%s/%s' % (uploader, slug_title) | |
231 | if token: | |
232 | resolve_title += '/%s' % token | |
5f6a1245 | 233 | |
eb6a41ba | 234 | self.report_resolve(full_title) |
5f6a1245 | 235 | |
de2dd4c5 | 236 | url = 'http://soundcloud.com/%s' % resolve_title |
eb6a41ba | 237 | info_json_url = self._resolv_url(url) |
20991253 | 238 | info = self._download_json(info_json_url, full_title, 'Downloading info JSON') |
aad0d6d5 | 239 | |
de2dd4c5 | 240 | return self._extract_info_dict(info, full_title, secret_token=token) |
aad0d6d5 | 241 | |
20991253 | 242 | |
7d239269 | 243 | class SoundcloudSetIE(SoundcloudIE): |
2f834e93 | 244 | _VALID_URL = r'https?://(?:www\.)?soundcloud\.com/(?P<uploader>[\w\d-]+)/sets/(?P<slug_title>[\w\d-]+)(?:/(?P<token>[^?/]+))?' |
fbcd7b5f | 245 | IE_NAME = 'soundcloud:set' |
22a6f150 PH |
246 | _TESTS = [{ |
247 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep', | |
248 | 'info_dict': { | |
a9551e90 | 249 | 'id': '2284613', |
22a6f150 PH |
250 | 'title': 'The Royal Concept EP', |
251 | }, | |
252 | 'playlist_mincount': 6, | |
253 | }] | |
aad0d6d5 | 254 | |
aad0d6d5 PH |
255 | def _real_extract(self, url): |
256 | mobj = re.match(self._VALID_URL, url) | |
aad0d6d5 PH |
257 | |
258 | # extract uploader (which is in the url) | |
2f834e93 | 259 | uploader = mobj.group('uploader') |
aad0d6d5 | 260 | # extract simple title (uploader + slug of song title) |
2f834e93 | 261 | slug_title = mobj.group('slug_title') |
aad0d6d5 | 262 | full_title = '%s/sets/%s' % (uploader, slug_title) |
2f834e93 | 263 | url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title) |
264 | ||
265 | token = mobj.group('token') | |
266 | if token: | |
267 | full_title += '/' + token | |
268 | url += '/' + token | |
aad0d6d5 PH |
269 | |
270 | self.report_resolve(full_title) | |
271 | ||
7d239269 | 272 | resolv_url = self._resolv_url(url) |
20991253 | 273 | info = self._download_json(resolv_url, full_title) |
aad0d6d5 | 274 | |
aad0d6d5 PH |
275 | if 'errors' in info: |
276 | for err in info['errors']: | |
83622b6d | 277 | self._downloader.report_error('unable to download video webpage: %s' % compat_str(err['error_message'])) |
aad0d6d5 PH |
278 | return |
279 | ||
22a6f150 PH |
280 | return { |
281 | '_type': 'playlist', | |
2f834e93 | 282 | 'entries': [self._extract_info_dict(track, secret_token=token) for track in info['tracks']], |
a9551e90 | 283 | 'id': '%s' % info['id'], |
22a6f150 PH |
284 | 'title': info['title'], |
285 | } | |
92790f4e JMF |
286 | |
287 | ||
288 | class SoundcloudUserIE(SoundcloudIE): | |
3941669d | 289 | _VALID_URL = r'https?://(www\.)?soundcloud\.com/(?P<user>[^/]+)/?((?P<rsrc>tracks|likes)/?)?(\?.*)?$' |
fbcd7b5f | 290 | IE_NAME = 'soundcloud:user' |
22a6f150 PH |
291 | _TESTS = [{ |
292 | 'url': 'https://soundcloud.com/the-concept-band', | |
293 | 'info_dict': { | |
294 | 'id': '9615865', | |
295 | 'title': 'The Royal Concept', | |
296 | }, | |
297 | 'playlist_mincount': 12 | |
298 | }, { | |
299 | 'url': 'https://soundcloud.com/the-concept-band/likes', | |
300 | 'info_dict': { | |
301 | 'id': '9615865', | |
302 | 'title': 'The Royal Concept', | |
303 | }, | |
304 | 'playlist_mincount': 1, | |
305 | }] | |
92790f4e JMF |
306 | |
307 | def _real_extract(self, url): | |
308 | mobj = re.match(self._VALID_URL, url) | |
309 | uploader = mobj.group('user') | |
3941669d | 310 | resource = mobj.group('rsrc') |
311 | if resource is None: | |
312 | resource = 'tracks' | |
313 | elif resource == 'likes': | |
314 | resource = 'favorites' | |
92790f4e JMF |
315 | |
316 | url = 'http://soundcloud.com/%s/' % uploader | |
317 | resolv_url = self._resolv_url(url) | |
20991253 PH |
318 | user = self._download_json( |
319 | resolv_url, uploader, 'Downloading user info') | |
3941669d | 320 | base_url = 'http://api.soundcloud.com/users/%s/%s.json?' % (uploader, resource) |
92790f4e | 321 | |
20991253 | 322 | entries = [] |
92790f4e | 323 | for i in itertools.count(): |
20991253 PH |
324 | data = compat_urllib_parse.urlencode({ |
325 | 'offset': i * 50, | |
3941669d | 326 | 'limit': 50, |
20991253 PH |
327 | 'client_id': self._CLIENT_ID, |
328 | }) | |
329 | new_entries = self._download_json( | |
330 | base_url + data, uploader, 'Downloading track page %s' % (i + 1)) | |
3941669d | 331 | if len(new_entries) == 0: |
332 | self.to_screen('%s: End page received' % uploader) | |
92790f4e | 333 | break |
3941669d | 334 | entries.extend(self._extract_info_dict(e, quiet=True) for e in new_entries) |
92790f4e JMF |
335 | |
336 | return { | |
337 | '_type': 'playlist', | |
338 | 'id': compat_str(user['id']), | |
339 | 'title': user['username'], | |
20991253 PH |
340 | 'entries': entries, |
341 | } | |
342 | ||
343 | ||
344 | class SoundcloudPlaylistIE(SoundcloudIE): | |
46f74bcf | 345 | _VALID_URL = r'https?://api\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$' |
20991253 | 346 | IE_NAME = 'soundcloud:playlist' |
46f74bcf PH |
347 | _TESTS = [{ |
348 | 'url': 'http://api.soundcloud.com/playlists/4110309', | |
349 | 'info_dict': { | |
350 | 'id': '4110309', | |
351 | 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]', | |
352 | 'description': 're:.*?TILT Brass - Bowery Poetry Club', | |
353 | }, | |
354 | 'playlist_count': 6, | |
355 | }] | |
20991253 PH |
356 | |
357 | def _real_extract(self, url): | |
358 | mobj = re.match(self._VALID_URL, url) | |
359 | playlist_id = mobj.group('id') | |
360 | base_url = '%s//api.soundcloud.com/playlists/%s.json?' % (self.http_scheme(), playlist_id) | |
361 | ||
2f834e93 | 362 | data_dict = { |
20991253 | 363 | 'client_id': self._CLIENT_ID, |
2f834e93 | 364 | } |
365 | token = mobj.group('token') | |
366 | ||
367 | if token: | |
368 | data_dict['secret_token'] = token | |
369 | ||
370 | data = compat_urllib_parse.urlencode(data_dict) | |
20991253 PH |
371 | data = self._download_json( |
372 | base_url + data, playlist_id, 'Downloading playlist') | |
373 | ||
374 | entries = [ | |
2f834e93 | 375 | self._extract_info_dict(t, quiet=True, secret_token=token) |
b74e86f4 | 376 | for t in data['tracks']] |
20991253 PH |
377 | |
378 | return { | |
379 | '_type': 'playlist', | |
380 | 'id': playlist_id, | |
381 | 'title': data.get('title'), | |
382 | 'description': data.get('description'), | |
383 | 'entries': entries, | |
92790f4e | 384 | } |