]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/soundcloud.py
3a19ab17222831d87ffde4992e5712b01359e6eb
[youtubedl] / youtube_dl / extractor / soundcloud.py
1 import json
2 import re
3 import itertools
4
5 from .common import InfoExtractor
6 from ..utils import (
7 compat_str,
8 compat_urlparse,
9 compat_urllib_parse,
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
25 _VALID_URL = r'''^(?:https?://)?
26 (?:(?:(?:www\.)?soundcloud\.com/([\w\d-]+)/([\w\d-]+)/?(?:[?].*)?$)
27 |(?:api\.soundcloud\.com/tracks/(?P<track_id>\d+))
28 |(?P<widget>w.soundcloud.com/player/?.*?url=.*)
29 )
30 '''
31 IE_NAME = u'soundcloud'
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 ]
60
61 _CLIENT_ID = 'b45b1aa10f1ac2941910a7f0d10f8e28'
62 _IPHONE_CLIENT_ID = '376f225bf427445fc4bfb6b99b72e0bf'
63
64 @classmethod
65 def suitable(cls, url):
66 return re.match(cls._VALID_URL, url, flags=re.VERBOSE) is not None
67
68 def report_resolve(self, video_id):
69 """Report information extraction."""
70 self.to_screen(u'%s: Resolving id' % video_id)
71
72 @classmethod
73 def _resolv_url(cls, url):
74 return 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=' + cls._CLIENT_ID
75
76 def _extract_info_dict(self, info, full_title=None, quiet=False):
77 track_id = compat_str(info['id'])
78 name = full_title or track_id
79 if quiet:
80 self.report_extraction(name)
81
82 thumbnail = info['artwork_url']
83 if thumbnail is not None:
84 thumbnail = thumbnail.replace('-large', '-t500x500')
85 ext = info.get('original_format', u'mp3')
86 result = {
87 'id': track_id,
88 'uploader': info['user']['username'],
89 'upload_date': unified_strdate(info['created_at']),
90 'title': info['title'],
91 'description': info['description'],
92 'thumbnail': thumbnail,
93 }
94 if info.get('downloadable', False):
95 # We can build a direct link to the song
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,
103 'vcodec': 'none',
104 }]
105 else:
106 # We have to retrieve the url
107 stream_json = self._download_webpage(
108 'http://api.soundcloud.com/i1/tracks/{0}/streams?client_id={1}'.format(track_id, self._IPHONE_CLIENT_ID),
109 track_id, u'Downloading track url')
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,
119 'vcodec': 'none',
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,
129 'vcodec': 'none',
130 })
131
132 if not formats:
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
135 formats.append({
136 'format_id': u'fallback',
137 'url': info['stream_url'] + '?client_id=' + self._CLIENT_ID,
138 'ext': ext,
139 'vcodec': 'none',
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
151
152 return result
153
154 def _real_extract(self, url):
155 mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE)
156 if mobj is None:
157 raise ExtractorError(u'Invalid URL: %s' % url)
158
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
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')
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')
178
179 info = json.loads(info_json)
180 return self._extract_info_dict(info, full_title)
181
182 class SoundcloudSetIE(SoundcloudIE):
183 _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)(?:[?].*)?$'
184 IE_NAME = u'soundcloud:set'
185 # it's in tests/test_playlists.py
186 _TESTS = []
187
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)
202 resolv_url = self._resolv_url(url)
203 info_json = self._download_webpage(resolv_url, full_title)
204
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)
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 }
217
218
219 class SoundcloudUserIE(SoundcloudIE):
220 _VALID_URL = r'https?://(www\.)?soundcloud.com/(?P<user>[^/]+)(/?(tracks/)?)?(\?.*)?$'
221 IE_NAME = u'soundcloud:user'
222
223 # it's in tests/test_playlists.py
224 _TESTS = []
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 }