]>
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'
33 u
'url' : u
'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy' ,
34 u
'file' : u
'62986583.mp3' ,
35 u
'md5' : u
'ebef0a451b909710ed1d7787dddbf0d7' ,
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"
44 _CLIENT_ID
= 'b45b1aa10f1ac2941910a7f0d10f8e28'
47 def suitable ( cls
, url
):
48 return re
. match ( cls
._ VALID
_U RL
, url
, flags
= re
. VERBOSE
) is not None
50 def report_resolve ( self
, video_id
):
51 """Report information extraction."""
52 self
. to_screen ( u
' %s : Resolving id' % video_id
)
55 def _resolv_url ( cls
, url
):
56 return 'http://api.soundcloud.com/resolve.json?url=' + url
+ '&client_id=' + cls
._ CLIENT
_ ID
58 def _extract_info_dict ( self
, info
, full_title
= None , quiet
= False ):
60 name
= full_title
or video_id
62 self
. report_extraction ( name
)
64 thumbnail
= info
[ 'artwork_url' ]
65 if thumbnail
is not None :
66 thumbnail
= thumbnail
. replace ( '-large' , '-t500x500' )
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' ],
74 'description' : info
[ 'description' ],
75 'thumbnail' : thumbnail
,
78 def _real_extract ( self
, url
):
79 mobj
= re
. match ( self
._ VALID
_U RL
, url
, flags
= re
. VERBOSE
)
81 raise ExtractorError ( u
'Invalid URL: %s ' % url
)
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
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' )
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
)
97 self
. report_resolve ( full_title
)
99 url
= 'http://soundcloud.com/ %s / %s ' % ( uploader
, slug_title
)
100 info_json_url
= self
._ resolv
_u rl
( url
)
101 info_json
= self
._ download
_ webpage
( info_json_url
, full_title
, u
'Downloading info JSON' )
103 info
= json
. loads ( info_json
)
104 return self
._ extract
_ info
_ dict
( info
, full_title
)
106 class SoundcloudSetIE ( SoundcloudIE
):
107 _VALID_URL
= r
'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)(?:[?].*)?$'
108 IE_NAME
= u
'soundcloud:set'
110 u
"url" : "https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep" ,
113 u
"file" : "30510138.mp3" ,
114 u
"md5" : "f9136bf103901728f29e419d2c70f55d" ,
116 u
"upload_date" : u
"20111213" ,
117 u
"description" : u
"The Royal Concept from Stockholm \r\n Filip / Povel / David / Magnus \r\n www.royalconceptband.com" ,
118 u
"uploader" : u
"The Royal Concept" ,
119 u
"title" : u
"D-D-Dance"
123 u
"file" : "47127625.mp3" ,
124 u
"md5" : "09b6758a018470570f8fd423c9453dd8" ,
126 u
"upload_date" : u
"20120521" ,
127 u
"description" : u
"The Royal Concept from Stockholm \r\n Filip / Povel / David / Magnus \r\n www.royalconceptband.com" ,
128 u
"uploader" : u
"The Royal Concept" ,
129 u
"title" : u
"The Royal Concept - Gimme Twice"
133 u
"file" : "47127627.mp3" ,
134 u
"md5" : "154abd4e418cea19c3b901f1e1306d9c" ,
136 u
"upload_date" : u
"20120521" ,
137 u
"uploader" : u
"The Royal Concept" ,
138 u
"title" : u
"Goldrushed"
142 u
"file" : "47127629.mp3" ,
143 u
"md5" : "2f5471edc79ad3f33a683153e96a79c1" ,
145 u
"upload_date" : u
"20120521" ,
146 u
"description" : u
"The Royal Concept from Stockholm \r\n Filip / Povel / David / Magnus \r\n www.royalconceptband.com" ,
147 u
"uploader" : u
"The Royal Concept" ,
148 u
"title" : u
"In the End"
152 u
"file" : "47127631.mp3" ,
153 u
"md5" : "f9ba87aa940af7213f98949254f1c6e2" ,
155 u
"upload_date" : u
"20120521" ,
156 u
"description" : u
"The Royal Concept from Stockholm \r\n Filip / David / Povel / Magnus \r\n www.theroyalconceptband.com" ,
157 u
"uploader" : u
"The Royal Concept" ,
158 u
"title" : u
"Knocked Up"
162 u
"file" : "75206121.mp3" ,
163 u
"md5" : "f9d1fe9406717e302980c30de4af9353" ,
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\n As 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"
174 def _real_extract ( self
, url
):
175 mobj
= re
. match ( self
._ VALID
_U RL
, url
)
177 raise ExtractorError ( u
'Invalid URL: %s ' % url
)
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
)
185 self
. report_resolve ( full_title
)
187 url
= 'http://soundcloud.com/ %s /sets/ %s ' % ( uploader
, slug_title
)
188 resolv_url
= self
._ resolv
_u rl
( url
)
189 info_json
= self
._ download
_ webpage
( resolv_url
, full_title
)
192 info
= json
. loads ( info_json
)
194 for err
in info
[ 'errors' ]:
195 self
._ downloader
. report_error ( u
'unable to download video webpage: %s ' % compat_str ( err
[ 'error_message' ]))
198 self
. report_extraction ( full_title
)
199 return { '_type' : 'playlist' ,
200 'entries' : [ self
._ extract
_ info
_ dict
( track
) for track
in info
[ 'tracks' ]],
202 'title' : info
[ 'title' ],
206 class SoundcloudUserIE ( SoundcloudIE
):
207 _VALID_URL
= r
'https?://(www\.)?soundcloud.com/(?P<user>[^/]+)(/?(tracks/)?)?(\?.*)?$'
208 IE_NAME
= u
'soundcloud:user'
210 # it's in tests/test_playlists.py
213 def _real_extract ( self
, url
):
214 mobj
= re
. match ( self
._ VALID
_U RL
, url
)
215 uploader
= mobj
. group ( 'user' )
217 url
= 'http://soundcloud.com/ %s /' % uploader
218 resolv_url
= self
._ resolv
_u rl
( url
)
219 user_json
= self
._ download
_ webpage
( resolv_url
, uploader
,
220 u
'Downloading user info' )
221 user
= json
. loads ( user_json
)
224 for i
in itertools
. count ():
225 data
= compat_urllib_parse
. urlencode ({ 'offset' : i
* 50 ,
226 'client_id' : self
._ CLIENT
_ ID
,
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 :
238 'id' : compat_str ( user
[ 'id' ]),
239 'title' : user
[ 'username' ],