]>
Commit | Line | Data |
---|---|---|
de2dd4c5 | 1 | # encoding: utf-8 |
aad0d6d5 PH |
2 | import json |
3 | import re | |
92790f4e | 4 | import itertools |
aad0d6d5 PH |
5 | |
6 | from .common import InfoExtractor | |
7 | from ..utils import ( | |
8 | compat_str, | |
668de34c | 9 | compat_urlparse, |
92790f4e | 10 | compat_urllib_parse, |
aad0d6d5 PH |
11 | |
12 | ExtractorError, | |
13 | unified_strdate, | |
14 | ) | |
15 | ||
16 | ||
17 | class SoundcloudIE(InfoExtractor): | |
18 | """Information extractor for soundcloud.com | |
19 | To access the media, the uid of the song and a stream token | |
20 | must be extracted from the page source and the script must make | |
21 | a request to media.soundcloud.com/crossdomain.xml. Then | |
22 | the media can be grabbed by requesting from an url composed | |
23 | of the stream token and uid | |
24 | """ | |
25 | ||
eb6a41ba | 26 | _VALID_URL = r'''^(?:https?://)? |
de2dd4c5 | 27 | (?:(?:(?:www\.)?soundcloud\.com/ |
4ff50ef8 PH |
28 | (?P<uploader>[\w\d-]+)/ |
29 | (?!sets/)(?P<title>[\w\d-]+)/? | |
de2dd4c5 | 30 | (?P<token>[^?]+?)?(?:[?].*)?$) |
eb6a41ba | 31 | |(?:api\.soundcloud\.com/tracks/(?P<track_id>\d+)) |
c0ade33e | 32 | |(?P<widget>w\.soundcloud\.com/player/?.*?url=.*) |
eb6a41ba JMF |
33 | ) |
34 | ''' | |
aad0d6d5 | 35 | IE_NAME = u'soundcloud' |
12c167c8 JMF |
36 | _TESTS = [ |
37 | { | |
38 | u'url': u'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy', | |
39 | u'file': u'62986583.mp3', | |
40 | u'md5': u'ebef0a451b909710ed1d7787dddbf0d7', | |
41 | u'info_dict': { | |
42 | u"upload_date": u"20121011", | |
43 | u"description": u"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", | |
44 | u"uploader": u"E.T. ExTerrestrial Music", | |
45 | u"title": u"Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1" | |
46 | } | |
47 | }, | |
48 | # not streamable song | |
49 | { | |
50 | u'url': u'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep', | |
51 | u'info_dict': { | |
52 | u'id': u'47127627', | |
53 | u'ext': u'mp3', | |
54 | u'title': u'Goldrushed', | |
55 | u'uploader': u'The Royal Concept', | |
56 | u'upload_date': u'20120521', | |
57 | }, | |
58 | u'params': { | |
59 | # rtmp | |
60 | u'skip_download': True, | |
61 | }, | |
62 | }, | |
de2dd4c5 JMF |
63 | # private link |
64 | { | |
65 | u'url': u'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp', | |
66 | u'md5': u'aa0dd32bfea9b0c5ef4f02aacd080604', | |
67 | u'info_dict': { | |
68 | u'id': u'123998367', | |
69 | u'ext': u'mp3', | |
70 | u'title': u'Youtube - Dl Test Video \'\' Ä↭', | |
71 | u'uploader': u'jaimeMF', | |
72 | u'description': u'test chars: \"\'/\\ä↭', | |
73 | u'upload_date': u'20131209', | |
74 | }, | |
75 | }, | |
f67ca84d JMF |
76 | # downloadable song |
77 | { | |
78 | u'url': u'https://soundcloud.com/simgretina/just-your-problem-baby-1', | |
79 | u'md5': u'56a8b69568acaa967b4c49f9d1d52d19', | |
80 | u'info_dict': { | |
81 | u'id': u'105614606', | |
82 | u'ext': u'wav', | |
83 | u'title': u'Just Your Problem Baby (Acapella)', | |
84 | u'description': u'Vocals', | |
85 | u'uploader': u'Sim Gretina', | |
86 | u'upload_date': u'20130815', | |
87 | }, | |
88 | }, | |
12c167c8 | 89 | ] |
aad0d6d5 | 90 | |
7d239269 | 91 | _CLIENT_ID = 'b45b1aa10f1ac2941910a7f0d10f8e28' |
64bb5187 | 92 | _IPHONE_CLIENT_ID = '376f225bf427445fc4bfb6b99b72e0bf' |
7d239269 | 93 | |
eb6a41ba JMF |
94 | @classmethod |
95 | def suitable(cls, url): | |
96 | return re.match(cls._VALID_URL, url, flags=re.VERBOSE) is not None | |
97 | ||
aad0d6d5 PH |
98 | def report_resolve(self, video_id): |
99 | """Report information extraction.""" | |
100 | self.to_screen(u'%s: Resolving id' % video_id) | |
101 | ||
7d239269 JMF |
102 | @classmethod |
103 | def _resolv_url(cls, url): | |
104 | return 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=' + cls._CLIENT_ID | |
105 | ||
de2dd4c5 | 106 | def _extract_info_dict(self, info, full_title=None, quiet=False, secret_token=None): |
12c167c8 JMF |
107 | track_id = compat_str(info['id']) |
108 | name = full_title or track_id | |
2a15e706 | 109 | if quiet: |
92790f4e | 110 | self.report_extraction(name) |
7d239269 JMF |
111 | |
112 | thumbnail = info['artwork_url'] | |
113 | if thumbnail is not None: | |
114 | thumbnail = thumbnail.replace('-large', '-t500x500') | |
475700ac | 115 | ext = u'mp3' |
12c167c8 | 116 | result = { |
2a15e706 | 117 | 'id': track_id, |
7d239269 JMF |
118 | 'uploader': info['user']['username'], |
119 | 'upload_date': unified_strdate(info['created_at']), | |
2a15e706 | 120 | 'title': info['title'], |
7d239269 JMF |
121 | 'description': info['description'], |
122 | 'thumbnail': thumbnail, | |
123 | } | |
12c167c8 | 124 | if info.get('downloadable', False): |
64bb5187 | 125 | # We can build a direct link to the song |
2a15e706 PH |
126 | format_url = ( |
127 | u'https://api.soundcloud.com/tracks/{0}/download?client_id={1}'.format( | |
128 | track_id, self._CLIENT_ID)) | |
129 | result['formats'] = [{ | |
130 | 'format_id': 'download', | |
f67ca84d | 131 | 'ext': info.get('original_format', u'mp3'), |
2a15e706 | 132 | 'url': format_url, |
fb04e403 | 133 | 'vcodec': 'none', |
2a15e706 | 134 | }] |
64bb5187 JMF |
135 | else: |
136 | # We have to retrieve the url | |
de2dd4c5 JMF |
137 | streams_url = ('http://api.soundcloud.com/i1/tracks/{0}/streams?' |
138 | 'client_id={1}&secret_token={2}'.format(track_id, self._IPHONE_CLIENT_ID, secret_token)) | |
12c167c8 | 139 | stream_json = self._download_webpage( |
de2dd4c5 | 140 | streams_url, |
12c167c8 | 141 | track_id, u'Downloading track url') |
2a15e706 PH |
142 | |
143 | formats = [] | |
144 | format_dict = json.loads(stream_json) | |
145 | for key, stream_url in format_dict.items(): | |
146 | if key.startswith(u'http'): | |
147 | formats.append({ | |
148 | 'format_id': key, | |
149 | 'ext': ext, | |
150 | 'url': stream_url, | |
fb04e403 | 151 | 'vcodec': 'none', |
2a15e706 PH |
152 | }) |
153 | elif key.startswith(u'rtmp'): | |
154 | # The url doesn't have an rtmp app, we have to extract the playpath | |
155 | url, path = stream_url.split('mp3:', 1) | |
156 | formats.append({ | |
157 | 'format_id': key, | |
158 | 'url': url, | |
159 | 'play_path': 'mp3:' + path, | |
160 | 'ext': ext, | |
fb04e403 | 161 | 'vcodec': 'none', |
2a15e706 PH |
162 | }) |
163 | ||
164 | if not formats: | |
64bb5187 JMF |
165 | # We fallback to the stream_url in the original info, this |
166 | # cannot be always used, sometimes it can give an HTTP 404 error | |
2a15e706 PH |
167 | formats.append({ |
168 | 'format_id': u'fallback', | |
169 | 'url': info['stream_url'] + '?client_id=' + self._CLIENT_ID, | |
170 | 'ext': ext, | |
fb04e403 | 171 | 'vcodec': 'none', |
2a15e706 PH |
172 | }) |
173 | ||
174 | def format_pref(f): | |
175 | if f['format_id'].startswith('http'): | |
176 | return 2 | |
177 | if f['format_id'].startswith('rtmp'): | |
178 | return 1 | |
179 | return 0 | |
180 | ||
181 | formats.sort(key=format_pref) | |
182 | result['formats'] = formats | |
64bb5187 | 183 | |
12c167c8 | 184 | return result |
7d239269 | 185 | |
aad0d6d5 | 186 | def _real_extract(self, url): |
eb6a41ba | 187 | mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE) |
aad0d6d5 PH |
188 | if mobj is None: |
189 | raise ExtractorError(u'Invalid URL: %s' % url) | |
190 | ||
eb6a41ba | 191 | track_id = mobj.group('track_id') |
de2dd4c5 | 192 | token = None |
eb6a41ba JMF |
193 | if track_id is not None: |
194 | info_json_url = 'http://api.soundcloud.com/tracks/' + track_id + '.json?client_id=' + self._CLIENT_ID | |
195 | full_title = track_id | |
668de34c JMF |
196 | elif mobj.group('widget'): |
197 | query = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query) | |
198 | return self.url_result(query['url'][0], ie='Soundcloud') | |
eb6a41ba JMF |
199 | else: |
200 | # extract uploader (which is in the url) | |
de2dd4c5 | 201 | uploader = mobj.group('uploader') |
eb6a41ba | 202 | # extract simple title (uploader + slug of song title) |
de2dd4c5 JMF |
203 | slug_title = mobj.group('title') |
204 | token = mobj.group('token') | |
205 | full_title = resolve_title = '%s/%s' % (uploader, slug_title) | |
206 | if token: | |
207 | resolve_title += '/%s' % token | |
eb6a41ba JMF |
208 | |
209 | self.report_resolve(full_title) | |
210 | ||
de2dd4c5 | 211 | url = 'http://soundcloud.com/%s' % resolve_title |
eb6a41ba JMF |
212 | info_json_url = self._resolv_url(url) |
213 | info_json = self._download_webpage(info_json_url, full_title, u'Downloading info JSON') | |
aad0d6d5 PH |
214 | |
215 | info = json.loads(info_json) | |
de2dd4c5 | 216 | return self._extract_info_dict(info, full_title, secret_token=token) |
aad0d6d5 | 217 | |
7d239269 | 218 | class SoundcloudSetIE(SoundcloudIE): |
20db33e2 | 219 | _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)(?:[?].*)?$' |
aad0d6d5 | 220 | IE_NAME = u'soundcloud:set' |
12c167c8 JMF |
221 | # it's in tests/test_playlists.py |
222 | _TESTS = [] | |
aad0d6d5 | 223 | |
aad0d6d5 PH |
224 | def _real_extract(self, url): |
225 | mobj = re.match(self._VALID_URL, url) | |
226 | if mobj is None: | |
227 | raise ExtractorError(u'Invalid URL: %s' % url) | |
228 | ||
229 | # extract uploader (which is in the url) | |
230 | uploader = mobj.group(1) | |
231 | # extract simple title (uploader + slug of song title) | |
232 | slug_title = mobj.group(2) | |
233 | full_title = '%s/sets/%s' % (uploader, slug_title) | |
234 | ||
235 | self.report_resolve(full_title) | |
236 | ||
237 | url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title) | |
7d239269 | 238 | resolv_url = self._resolv_url(url) |
aad0d6d5 PH |
239 | info_json = self._download_webpage(resolv_url, full_title) |
240 | ||
aad0d6d5 PH |
241 | info = json.loads(info_json) |
242 | if 'errors' in info: | |
243 | for err in info['errors']: | |
244 | self._downloader.report_error(u'unable to download video webpage: %s' % compat_str(err['error_message'])) | |
245 | return | |
246 | ||
247 | self.report_extraction(full_title) | |
7d239269 JMF |
248 | return {'_type': 'playlist', |
249 | 'entries': [self._extract_info_dict(track) for track in info['tracks']], | |
250 | 'id': info['id'], | |
251 | 'title': info['title'], | |
252 | } | |
92790f4e JMF |
253 | |
254 | ||
255 | class SoundcloudUserIE(SoundcloudIE): | |
c0ade33e | 256 | _VALID_URL = r'https?://(www\.)?soundcloud\.com/(?P<user>[^/]+)(/?(tracks/)?)?(\?.*)?$' |
92790f4e JMF |
257 | IE_NAME = u'soundcloud:user' |
258 | ||
259 | # it's in tests/test_playlists.py | |
12c167c8 | 260 | _TESTS = [] |
92790f4e JMF |
261 | |
262 | def _real_extract(self, url): | |
263 | mobj = re.match(self._VALID_URL, url) | |
264 | uploader = mobj.group('user') | |
265 | ||
266 | url = 'http://soundcloud.com/%s/' % uploader | |
267 | resolv_url = self._resolv_url(url) | |
268 | user_json = self._download_webpage(resolv_url, uploader, | |
269 | u'Downloading user info') | |
270 | user = json.loads(user_json) | |
271 | ||
272 | tracks = [] | |
273 | for i in itertools.count(): | |
274 | data = compat_urllib_parse.urlencode({'offset': i*50, | |
275 | 'client_id': self._CLIENT_ID, | |
276 | }) | |
277 | tracks_url = 'http://api.soundcloud.com/users/%s/tracks.json?' % user['id'] + data | |
278 | response = self._download_webpage(tracks_url, uploader, | |
279 | u'Downloading tracks page %s' % (i+1)) | |
280 | new_tracks = json.loads(response) | |
281 | tracks.extend(self._extract_info_dict(track, quiet=True) for track in new_tracks) | |
282 | if len(new_tracks) < 50: | |
283 | break | |
284 | ||
285 | return { | |
286 | '_type': 'playlist', | |
287 | 'id': compat_str(user['id']), | |
288 | 'title': user['username'], | |
289 | 'entries': tracks, | |
290 | } |