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