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