]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/soundcloud.py
29cd5617c7d1919fa95e0b48e7ff35585106b800
[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 _TEST = {
33 u'url': u'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy',
34 u'file': u'62986583.mp3',
35 u'md5': u'ebef0a451b909710ed1d7787dddbf0d7',
36 u'info_dict': {
37 u"upload_date": u"20121011",
38 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",
39 u"uploader": u"E.T. ExTerrestrial Music",
40 u"title": u"Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1"
41 }
42 }
43
44 _CLIENT_ID = 'b45b1aa10f1ac2941910a7f0d10f8e28'
45
46 @classmethod
47 def suitable(cls, url):
48 return re.match(cls._VALID_URL, url, flags=re.VERBOSE) is not None
49
50 def report_resolve(self, video_id):
51 """Report information extraction."""
52 self.to_screen(u'%s: Resolving id' % video_id)
53
54 @classmethod
55 def _resolv_url(cls, url):
56 return 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=' + cls._CLIENT_ID
57
58 def _extract_info_dict(self, info, full_title=None, quiet=False):
59 video_id = info['id']
60 name = full_title or video_id
61 if quiet == False:
62 self.report_extraction(name)
63
64 thumbnail = info['artwork_url']
65 if thumbnail is not None:
66 thumbnail = thumbnail.replace('-large', '-t500x500')
67 return {
68 'id': info['id'],
69 'url': info['stream_url'] + '?client_id=' + self._CLIENT_ID,
70 'uploader': info['user']['username'],
71 'upload_date': unified_strdate(info['created_at']),
72 'title': info['title'],
73 'ext': u'mp3',
74 'description': info['description'],
75 'thumbnail': thumbnail,
76 }
77
78 def _real_extract(self, url):
79 mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE)
80 if mobj is None:
81 raise ExtractorError(u'Invalid URL: %s' % url)
82
83 track_id = mobj.group('track_id')
84 if track_id is not None:
85 info_json_url = 'http://api.soundcloud.com/tracks/' + track_id + '.json?client_id=' + self._CLIENT_ID
86 full_title = track_id
87 elif mobj.group('widget'):
88 query = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
89 return self.url_result(query['url'][0], ie='Soundcloud')
90 else:
91 # extract uploader (which is in the url)
92 uploader = mobj.group(1)
93 # extract simple title (uploader + slug of song title)
94 slug_title = mobj.group(2)
95 full_title = '%s/%s' % (uploader, slug_title)
96
97 self.report_resolve(full_title)
98
99 url = 'http://soundcloud.com/%s/%s' % (uploader, slug_title)
100 info_json_url = self._resolv_url(url)
101 info_json = self._download_webpage(info_json_url, full_title, u'Downloading info JSON')
102
103 info = json.loads(info_json)
104 return self._extract_info_dict(info, full_title)
105
106 class SoundcloudSetIE(SoundcloudIE):
107 _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)(?:[?].*)?$'
108 IE_NAME = u'soundcloud:set'
109 _TEST = {
110 u"url":"https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep",
111 u"playlist": [
112 {
113 u"file":"30510138.mp3",
114 u"md5":"f9136bf103901728f29e419d2c70f55d",
115 u"info_dict": {
116 u"upload_date": u"20111213",
117 u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
118 u"uploader": u"The Royal Concept",
119 u"title": u"D-D-Dance"
120 }
121 },
122 {
123 u"file":"47127625.mp3",
124 u"md5":"09b6758a018470570f8fd423c9453dd8",
125 u"info_dict": {
126 u"upload_date": u"20120521",
127 u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
128 u"uploader": u"The Royal Concept",
129 u"title": u"The Royal Concept - Gimme Twice"
130 }
131 },
132 {
133 u"file":"47127627.mp3",
134 u"md5":"154abd4e418cea19c3b901f1e1306d9c",
135 u"info_dict": {
136 u"upload_date": u"20120521",
137 u"uploader": u"The Royal Concept",
138 u"title": u"Goldrushed"
139 }
140 },
141 {
142 u"file":"47127629.mp3",
143 u"md5":"2f5471edc79ad3f33a683153e96a79c1",
144 u"info_dict": {
145 u"upload_date": u"20120521",
146 u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
147 u"uploader": u"The Royal Concept",
148 u"title": u"In the End"
149 }
150 },
151 {
152 u"file":"47127631.mp3",
153 u"md5":"f9ba87aa940af7213f98949254f1c6e2",
154 u"info_dict": {
155 u"upload_date": u"20120521",
156 u"description": u"The Royal Concept from Stockholm\r\nFilip / David / Povel / Magnus\r\nwww.theroyalconceptband.com",
157 u"uploader": u"The Royal Concept",
158 u"title": u"Knocked Up"
159 }
160 },
161 {
162 u"file":"75206121.mp3",
163 u"md5":"f9d1fe9406717e302980c30de4af9353",
164 u"info_dict": {
165 u"upload_date": u"20130116",
166 u"description": u"The unreleased track World on Fire premiered on the CW's hit show Arrow (8pm/7pm central). \r\nAs a gift to our fans we would like to offer you a free download of the track! ",
167 u"uploader": u"The Royal Concept",
168 u"title": u"World On Fire"
169 }
170 }
171 ]
172 }
173
174 def _real_extract(self, url):
175 mobj = re.match(self._VALID_URL, url)
176 if mobj is None:
177 raise ExtractorError(u'Invalid URL: %s' % url)
178
179 # extract uploader (which is in the url)
180 uploader = mobj.group(1)
181 # extract simple title (uploader + slug of song title)
182 slug_title = mobj.group(2)
183 full_title = '%s/sets/%s' % (uploader, slug_title)
184
185 self.report_resolve(full_title)
186
187 url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title)
188 resolv_url = self._resolv_url(url)
189 info_json = self._download_webpage(resolv_url, full_title)
190
191 videos = []
192 info = json.loads(info_json)
193 if 'errors' in info:
194 for err in info['errors']:
195 self._downloader.report_error(u'unable to download video webpage: %s' % compat_str(err['error_message']))
196 return
197
198 self.report_extraction(full_title)
199 return {'_type': 'playlist',
200 'entries': [self._extract_info_dict(track) for track in info['tracks']],
201 'id': info['id'],
202 'title': info['title'],
203 }
204
205
206 class SoundcloudUserIE(SoundcloudIE):
207 _VALID_URL = r'https?://(www\.)?soundcloud.com/(?P<user>[^/]+)(/?(tracks/)?)?(\?.*)?$'
208 IE_NAME = u'soundcloud:user'
209
210 # it's in tests/test_playlists.py
211 _TEST = None
212
213 def _real_extract(self, url):
214 mobj = re.match(self._VALID_URL, url)
215 uploader = mobj.group('user')
216
217 url = 'http://soundcloud.com/%s/' % uploader
218 resolv_url = self._resolv_url(url)
219 user_json = self._download_webpage(resolv_url, uploader,
220 u'Downloading user info')
221 user = json.loads(user_json)
222
223 tracks = []
224 for i in itertools.count():
225 data = compat_urllib_parse.urlencode({'offset': i*50,
226 'client_id': self._CLIENT_ID,
227 })
228 tracks_url = 'http://api.soundcloud.com/users/%s/tracks.json?' % user['id'] + data
229 response = self._download_webpage(tracks_url, uploader,
230 u'Downloading tracks page %s' % (i+1))
231 new_tracks = json.loads(response)
232 tracks.extend(self._extract_info_dict(track, quiet=True) for track in new_tracks)
233 if len(new_tracks) < 50:
234 break
235
236 return {
237 '_type': 'playlist',
238 'id': compat_str(user['id']),
239 'title': user['username'],
240 'entries': tracks,
241 }