]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/soundcloud.py
5 from . common
import InfoExtractor
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
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=.*)
31 IE_NAME
= u
'soundcloud'
34 u
'url' : u
'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy' ,
35 u
'file' : u
'62986583.mp3' ,
36 u
'md5' : u
'ebef0a451b909710ed1d7787dddbf0d7' ,
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"
46 u
'url' : u
'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep' ,
50 u
'title' : u
'Goldrushed' ,
51 u
'uploader' : u
'The Royal Concept' ,
52 u
'upload_date' : u
'20120521' ,
56 u
'skip_download' : True ,
61 _CLIENT_ID
= 'b45b1aa10f1ac2941910a7f0d10f8e28'
64 def suitable ( cls
, url
):
65 return re
. match ( cls
._ VALID
_U RL
, url
, flags
= re
. VERBOSE
) is not None
67 def report_resolve ( self
, video_id
):
68 """Report information extraction."""
69 self
. to_screen ( u
' %s : Resolving id' % video_id
)
72 def _resolv_url ( cls
, url
):
73 return 'http://api.soundcloud.com/resolve.json?url=' + url
+ '&client_id=' + cls
._ CLIENT
_ ID
75 def _extract_info_dict ( self
, info
, full_title
= None , quiet
= False ):
76 track_id
= compat_str ( info
[ 'id' ])
77 name
= full_title
or track_id
79 self
. report_extraction ( name
)
81 thumbnail
= info
[ 'artwork_url' ]
82 if thumbnail
is not None :
83 thumbnail
= thumbnail
. replace ( '-large' , '-t500x500' )
86 'url' : info
[ 'stream_url' ] + '?client_id=' + self
._ CLIENT
_ ID
,
87 'uploader' : info
[ 'user' ][ 'username' ],
88 'upload_date' : unified_strdate ( info
[ 'created_at' ]),
89 'title' : info
[ 'title' ],
91 'description' : info
[ 'description' ],
92 'thumbnail' : thumbnail
,
94 if info
. get ( 'downloadable' , False ):
95 result
[ 'url' ] = 'https://api.soundcloud.com/tracks/ {0} /download?client_id= {1} ' . format ( track_id
, self
._ CLIENT
_ ID
)
96 if not info
. get ( 'streamable' , False ):
97 # We have to get the rtmp url
98 stream_json
= self
._ download
_ webpage
(
99 'http://api.soundcloud.com/i1/tracks/ {0} /streams?client_id= {1} ' . format ( track_id
, self
._ CLIENT
_ ID
),
100 track_id
, u
'Downloading track url' )
101 rtmp_url
= json
. loads ( stream_json
)[ 'rtmp_mp3_128_url' ]
102 # The url doesn't have an rtmp app, we have to extract the playpath
103 url
, path
= rtmp_url
. split ( 'mp3:' , 1 )
106 'play_path' : 'mp3:' + path
,
110 def _real_extract ( self
, url
):
111 mobj
= re
. match ( self
._ VALID
_U RL
, url
, flags
= re
. VERBOSE
)
113 raise ExtractorError ( u
'Invalid URL: %s ' % url
)
115 track_id
= mobj
. group ( 'track_id' )
116 if track_id
is not None :
117 info_json_url
= 'http://api.soundcloud.com/tracks/' + track_id
+ '.json?client_id=' + self
._ CLIENT
_ ID
118 full_title
= track_id
119 elif mobj
. group ( 'widget' ):
120 query
= compat_urlparse
. parse_qs ( compat_urlparse
. urlparse ( url
). query
)
121 return self
. url_result ( query
[ 'url' ][ 0 ], ie
= 'Soundcloud' )
123 # extract uploader (which is in the url)
124 uploader
= mobj
. group ( 1 )
125 # extract simple title (uploader + slug of song title)
126 slug_title
= mobj
. group ( 2 )
127 full_title
= ' %s / %s ' % ( uploader
, slug_title
)
129 self
. report_resolve ( full_title
)
131 url
= 'http://soundcloud.com/ %s / %s ' % ( uploader
, slug_title
)
132 info_json_url
= self
._ resolv
_u rl
( url
)
133 info_json
= self
._ download
_ webpage
( info_json_url
, full_title
, u
'Downloading info JSON' )
135 info
= json
. loads ( info_json
)
136 return self
._ extract
_ info
_ dict
( info
, full_title
)
138 class SoundcloudSetIE ( SoundcloudIE
):
139 _VALID_URL
= r
'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)(?:[?].*)?$'
140 IE_NAME
= u
'soundcloud:set'
141 # it's in tests/test_playlists.py
144 def _real_extract ( self
, url
):
145 mobj
= re
. match ( self
._ VALID
_U RL
, url
)
147 raise ExtractorError ( u
'Invalid URL: %s ' % url
)
149 # extract uploader (which is in the url)
150 uploader
= mobj
. group ( 1 )
151 # extract simple title (uploader + slug of song title)
152 slug_title
= mobj
. group ( 2 )
153 full_title
= ' %s /sets/ %s ' % ( uploader
, slug_title
)
155 self
. report_resolve ( full_title
)
157 url
= 'http://soundcloud.com/ %s /sets/ %s ' % ( uploader
, slug_title
)
158 resolv_url
= self
._ resolv
_u rl
( url
)
159 info_json
= self
._ download
_ webpage
( resolv_url
, full_title
)
162 info
= json
. loads ( info_json
)
164 for err
in info
[ 'errors' ]:
165 self
._ downloader
. report_error ( u
'unable to download video webpage: %s ' % compat_str ( err
[ 'error_message' ]))
168 self
. report_extraction ( full_title
)
169 return { '_type' : 'playlist' ,
170 'entries' : [ self
._ extract
_ info
_ dict
( track
) for track
in info
[ 'tracks' ]],
172 'title' : info
[ 'title' ],
176 class SoundcloudUserIE ( SoundcloudIE
):
177 _VALID_URL
= r
'https?://(www\.)?soundcloud.com/(?P<user>[^/]+)(/?(tracks/)?)?(\?.*)?$'
178 IE_NAME
= u
'soundcloud:user'
180 # it's in tests/test_playlists.py
183 def _real_extract ( self
, url
):
184 mobj
= re
. match ( self
._ VALID
_U RL
, url
)
185 uploader
= mobj
. group ( 'user' )
187 url
= 'http://soundcloud.com/ %s /' % uploader
188 resolv_url
= self
._ resolv
_u rl
( url
)
189 user_json
= self
._ download
_ webpage
( resolv_url
, uploader
,
190 u
'Downloading user info' )
191 user
= json
. loads ( user_json
)
194 for i
in itertools
. count ():
195 data
= compat_urllib_parse
. urlencode ({ 'offset' : i
* 50 ,
196 'client_id' : self
._ CLIENT
_ ID
,
198 tracks_url
= 'http://api.soundcloud.com/users/ %s /tracks.json?' % user
[ 'id' ] + data
199 response
= self
._ download
_ webpage
( tracks_url
, uploader
,
200 u
'Downloading tracks page %s ' % ( i
+ 1 ))
201 new_tracks
= json
. loads ( response
)
202 tracks
. extend ( self
._ extract
_ info
_ dict
( track
, quiet
= True ) for track
in new_tracks
)
203 if len ( new_tracks
) < 50 :
208 'id' : compat_str ( user
[ 'id' ]),
209 'title' : user
[ 'username' ],