]>
jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/soundcloud.py
2 from __future__
import unicode_literals
7 from . common
import InfoExtractor
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
28 _VALID_URL
= r
'''(?x)^(?:https?://)?
29 (?:(?:(?:www\.|m\.)?soundcloud\.com/
30 (?P<uploader>[\w\d-]+)/
31 (?!sets/|likes/?(?:$|[?#]))
33 (?P<token>[^?]+?)?(?:[?].*)?$)
34 |(?:api\.soundcloud\.com/tracks/(?P<track_id>\d+)
35 (?:/?\?secret_token=(?P<secret_token>[^&]+?))?$)
36 |(?P<player>(?:w|player|p.)\.soundcloud\.com/player/?.*?url=.*)
39 IE_NAME
= 'soundcloud'
42 'url' : 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy' ,
43 'file' : '62986583.mp3' ,
44 'md5' : 'ebef0a451b909710ed1d7787dddbf0d7' ,
46 "upload_date" : "20121011" ,
47 "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" ,
48 "uploader" : "E.T. ExTerrestrial Music" ,
49 "title" : "Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1" ,
55 'url' : 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep' ,
59 'title' : 'Goldrushed' ,
60 'description' : 'From Stockholm Sweden \r\n Povel / Magnus / Filip / David \r\n www.theroyalconcept.com' ,
61 'uploader' : 'The Royal Concept' ,
62 'upload_date' : '20120521' ,
67 'skip_download' : True ,
72 'url' : 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp' ,
73 'md5' : 'aa0dd32bfea9b0c5ef4f02aacd080604' ,
77 'title' : 'Youtube - Dl Test Video \'\' Ä↭' ,
78 'uploader' : 'jaimeMF' ,
79 'description' : 'test chars: \"\' / \\ ä↭' ,
80 'upload_date' : '20131209' ,
84 # private link (alt format)
86 'url' : 'https://api.soundcloud.com/tracks/123998367?secret_token=s-8Pjrp' ,
87 'md5' : 'aa0dd32bfea9b0c5ef4f02aacd080604' ,
91 'title' : 'Youtube - Dl Test Video \'\' Ä↭' ,
92 'uploader' : 'jaimeMF' ,
93 'description' : 'test chars: \"\' / \\ ä↭' ,
94 'upload_date' : '20131209' ,
100 'url' : 'https://soundcloud.com/oddsamples/bus-brakes' ,
101 'md5' : '7624f2351f8a3b2e7cd51522496e7631' ,
105 'title' : 'Bus Brakes' ,
106 'description' : 'md5:0170be75dd395c96025d210d261c784e' ,
107 'uploader' : 'oddsamples' ,
108 'upload_date' : '20140109' ,
114 _CLIENT_ID
= 'b45b1aa10f1ac2941910a7f0d10f8e28'
115 _IPHONE_CLIENT_ID
= '376f225bf427445fc4bfb6b99b72e0bf'
117 def report_resolve ( self
, video_id
):
118 """Report information extraction."""
119 self
. to_screen ( ' %s : Resolving id' % video_id
)
122 def _resolv_url ( cls
, url
):
123 return 'http://api.soundcloud.com/resolve.json?url=' + url
+ '&client_id=' + cls
._ CLIENT
_ ID
125 def _extract_info_dict ( self
, info
, full_title
= None , quiet
= False , secret_token
= None ):
126 track_id
= compat_str ( info
[ 'id' ])
127 name
= full_title
or track_id
129 self
. report_extraction ( name
)
131 thumbnail
= info
[ 'artwork_url' ]
132 if thumbnail
is not None :
133 thumbnail
= thumbnail
. replace ( '-large' , '-t500x500' )
137 'uploader' : info
[ 'user' ][ 'username' ],
138 'upload_date' : unified_strdate ( info
[ 'created_at' ]),
139 'title' : info
[ 'title' ],
140 'description' : info
[ 'description' ],
141 'thumbnail' : thumbnail
,
142 'duration' : int_or_none ( info
. get ( 'duration' ), 1000 ),
145 if info
. get ( 'downloadable' , False ):
146 # We can build a direct link to the song
148 'https://api.soundcloud.com/tracks/ {0} /download?client_id= {1} ' . format (
149 track_id
, self
._ CLIENT
_ ID
))
151 'format_id' : 'download' ,
152 'ext' : info
. get ( 'original_format' , 'mp3' ),
158 # We have to retrieve the url
159 streams_url
= ( 'http://api.soundcloud.com/i1/tracks/ {0} /streams?'
160 'client_id= {1} &secret_token= {2} ' . format ( track_id
, self
._ IPHONE
_ CLIENT
_ ID
, secret_token
))
161 format_dict
= self
._ download
_ json
(
163 track_id
, 'Downloading track url' )
165 for key
, stream_url
in format_dict
. items ():
166 if key
. startswith ( 'http' ):
173 elif key
. startswith ( 'rtmp' ):
174 # The url doesn't have an rtmp app, we have to extract the playpath
175 url
, path
= stream_url
. split ( 'mp3:' , 1 )
179 'play_path' : 'mp3:' + path
,
185 # We fallback to the stream_url in the original info, this
186 # cannot be always used, sometimes it can give an HTTP 404 error
188 'format_id' : 'fallback' ,
189 'url' : info
[ 'stream_url' ] + '?client_id=' + self
._ CLIENT
_ ID
,
195 if f
[ 'format_id' ]. startswith ( 'http' ):
196 f
[ 'protocol' ] = 'http'
197 if f
[ 'format_id' ]. startswith ( 'rtmp' ):
198 f
[ 'protocol' ] = 'rtmp'
200 self
._ sort
_ formats
( formats
)
201 result
[ 'formats' ] = formats
205 def _real_extract ( self
, url
):
206 mobj
= re
. match ( self
._ VALID
_U RL
, url
, flags
= re
. VERBOSE
)
208 raise ExtractorError ( 'Invalid URL: %s ' % url
)
210 track_id
= mobj
. group ( 'track_id' )
212 if track_id
is not None :
213 info_json_url
= 'http://api.soundcloud.com/tracks/' + track_id
+ '.json?client_id=' + self
._ CLIENT
_ ID
214 full_title
= track_id
215 token
= mobj
. group ( 'secret_token' )
217 info_json_url
+= "&secret_token=" + token
218 elif mobj
. group ( 'player' ):
219 query
= compat_urlparse
. parse_qs ( compat_urlparse
. urlparse ( url
). query
)
220 return self
. url_result ( query
[ 'url' ][ 0 ])
222 # extract uploader (which is in the url)
223 uploader
= mobj
. group ( 'uploader' )
224 # extract simple title (uploader + slug of song title)
225 slug_title
= mobj
. group ( 'title' )
226 token
= mobj
. group ( 'token' )
227 full_title
= resolve_title
= ' %s / %s ' % ( uploader
, slug_title
)
229 resolve_title
+= '/ %s ' % token
231 self
. report_resolve ( full_title
)
233 url
= 'http://soundcloud.com/ %s ' % resolve_title
234 info_json_url
= self
._ resolv
_u rl
( url
)
235 info
= self
._ download
_ json
( info_json_url
, full_title
, 'Downloading info JSON' )
237 return self
._ extract
_ info
_ dict
( info
, full_title
, secret_token
= token
)
240 class SoundcloudSetIE ( SoundcloudIE
):
241 _VALID_URL
= r
'https?://(?:www\.)?soundcloud\.com/(?P<uploader>[\w\d-]+)/sets/(?P<slug_title>[\w\d-]+)(?:/(?P<token>[^?/]+))?'
242 IE_NAME
= 'soundcloud:set'
244 'url' : 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep' ,
246 'title' : 'The Royal Concept EP' ,
248 'playlist_mincount' : 6 ,
251 def _real_extract ( self
, url
):
252 mobj
= re
. match ( self
._ VALID
_U RL
, url
)
254 # extract uploader (which is in the url)
255 uploader
= mobj
. group ( 'uploader' )
256 # extract simple title (uploader + slug of song title)
257 slug_title
= mobj
. group ( 'slug_title' )
258 full_title
= ' %s /sets/ %s ' % ( uploader
, slug_title
)
259 url
= 'http://soundcloud.com/ %s /sets/ %s ' % ( uploader
, slug_title
)
261 token
= mobj
. group ( 'token' )
263 full_title
+= '/' + token
266 self
. report_resolve ( full_title
)
268 resolv_url
= self
._ resolv
_u rl
( url
)
269 info
= self
._ download
_ json
( resolv_url
, full_title
)
272 for err
in info
[ 'errors' ]:
273 self
._ downloader
. report_error ( 'unable to download video webpage: %s ' % compat_str ( err
[ 'error_message' ]))
278 'entries' : [ self
._ extract
_ info
_ dict
( track
, secret_token
= token
) for track
in info
[ 'tracks' ]],
280 'title' : info
[ 'title' ],
284 class SoundcloudUserIE ( SoundcloudIE
):
285 _VALID_URL
= r
'https?://(www\.)?soundcloud\.com/(?P<user>[^/]+)/?((?P<rsrc>tracks|likes)/?)?(\?.*)?$'
286 IE_NAME
= 'soundcloud:user'
288 'url' : 'https://soundcloud.com/the-concept-band' ,
291 'title' : 'The Royal Concept' ,
293 'playlist_mincount' : 12
295 'url' : 'https://soundcloud.com/the-concept-band/likes' ,
298 'title' : 'The Royal Concept' ,
300 'playlist_mincount' : 1 ,
303 def _real_extract ( self
, url
):
304 mobj
= re
. match ( self
._ VALID
_U RL
, url
)
305 uploader
= mobj
. group ( 'user' )
306 resource
= mobj
. group ( 'rsrc' )
309 elif resource
== 'likes' :
310 resource
= 'favorites'
312 url
= 'http://soundcloud.com/ %s /' % uploader
313 resolv_url
= self
._ resolv
_u rl
( url
)
314 user
= self
._ download
_ json
(
315 resolv_url
, uploader
, 'Downloading user info' )
316 base_url
= 'http://api.soundcloud.com/users/ %s / %s .json?' % ( uploader
, resource
)
319 for i
in itertools
. count ():
320 data
= compat_urllib_parse
. urlencode ({
323 'client_id' : self
._ CLIENT
_ ID
,
325 new_entries
= self
._ download
_ json
(
326 base_url
+ data
, uploader
, 'Downloading track page %s ' % ( i
+ 1 ))
327 if len ( new_entries
) == 0 :
328 self
. to_screen ( ' %s : End page received' % uploader
)
330 entries
. extend ( self
._ extract
_ info
_ dict
( e
, quiet
= True ) for e
in new_entries
)
334 'id' : compat_str ( user
[ 'id' ]),
335 'title' : user
[ 'username' ],
340 class SoundcloudPlaylistIE ( SoundcloudIE
):
341 _VALID_URL
= r
'https?://api\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$'
342 IE_NAME
= 'soundcloud:playlist'
344 'url' : 'http://api.soundcloud.com/playlists/4110309' ,
347 'title' : 'TILT Brass - Bowery Poetry Club, August \' 03 [Non-Site SCR 02]' ,
348 'description' : 're:.*?TILT Brass - Bowery Poetry Club' ,
353 def _real_extract ( self
, url
):
354 mobj
= re
. match ( self
._ VALID
_U RL
, url
)
355 playlist_id
= mobj
. group ( 'id' )
356 base_url
= ' %s //api.soundcloud.com/playlists/ %s .json?' % ( self
. http_scheme (), playlist_id
)
359 'client_id' : self
._ CLIENT
_ ID
,
361 token
= mobj
. group ( 'token' )
364 data_dict
[ 'secret_token' ] = token
366 data
= compat_urllib_parse
. urlencode ( data_dict
)
367 data
= self
._ download
_ json
(
368 base_url
+ data
, playlist_id
, 'Downloading playlist' )
371 self
._ extract
_ info
_ dict
( t
, quiet
= True , secret_token
= token
)
372 for t
in data
[ 'tracks' ]]
377 'title' : data
. get ( 'title' ),
378 'description' : data
. get ( 'description' ),