]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/soundcloud.py
4 from .common
import InfoExtractor
13 class SoundcloudIE(InfoExtractor
):
14 """Information extractor for soundcloud.com
15 To access the media, the uid of the song and a stream token
16 must be extracted from the page source and the script must make
17 a request to media.soundcloud.com/crossdomain.xml. Then
18 the media can be grabbed by requesting from an url composed
19 of the stream token and uid
22 _VALID_URL
= r
'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/([\w\d-]+)'
23 IE_NAME
= u
'soundcloud'
25 def report_resolve(self
, video_id
):
26 """Report information extraction."""
27 self
.to_screen(u
'%s: Resolving id' % video_id
)
29 def _real_extract(self
, url
):
30 mobj
= re
.match(self
._VALID
_URL
, url
)
32 raise ExtractorError(u
'Invalid URL: %s' % url
)
34 # extract uploader (which is in the url)
35 uploader
= mobj
.group(1)
36 # extract simple title (uploader + slug of song title)
37 slug_title
= mobj
.group(2)
38 full_title
= '%s/%s' % (uploader
, slug_title
)
40 self
.report_resolve(full_title
)
42 url
= 'http://soundcloud.com/%s/%s' % (uploader
, slug_title
)
43 resolv_url
= 'http://api.soundcloud.com/resolve.json?url=' + url
+ '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
44 info_json
= self
._download
_webpage
(resolv_url
, full_title
, u
'Downloading info JSON')
46 info
= json
.loads(info_json
)
48 self
.report_extraction(full_title
)
50 streams_url
= 'https://api.sndcdn.com/i1/tracks/' + str(video_id
) + '/streams?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
51 stream_json
= self
._download
_webpage
(streams_url
, full_title
,
52 u
'Downloading stream definitions',
53 u
'unable to download stream definitions')
55 streams
= json
.loads(stream_json
)
56 mediaURL
= streams
['http_mp3_128_url']
57 upload_date
= unified_strdate(info
['created_at'])
62 'uploader': info
['user']['username'],
63 'upload_date': upload_date
,
64 'title': info
['title'],
66 'description': info
['description'],
69 class SoundcloudSetIE(InfoExtractor
):
70 """Information extractor for soundcloud.com sets
71 To access the media, the uid of the song and a stream token
72 must be extracted from the page source and the script must make
73 a request to media.soundcloud.com/crossdomain.xml. Then
74 the media can be grabbed by requesting from an url composed
75 of the stream token and uid
78 _VALID_URL
= r
'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)'
79 IE_NAME
= u
'soundcloud:set'
81 def report_resolve(self
, video_id
):
82 """Report information extraction."""
83 self
.to_screen(u
'%s: Resolving id' % video_id
)
85 def _real_extract(self
, url
):
86 mobj
= re
.match(self
._VALID
_URL
, url
)
88 raise ExtractorError(u
'Invalid URL: %s' % url
)
90 # extract uploader (which is in the url)
91 uploader
= mobj
.group(1)
92 # extract simple title (uploader + slug of song title)
93 slug_title
= mobj
.group(2)
94 full_title
= '%s/sets/%s' % (uploader
, slug_title
)
96 self
.report_resolve(full_title
)
98 url
= 'http://soundcloud.com/%s/sets/%s' % (uploader
, slug_title
)
99 resolv_url
= 'http://api.soundcloud.com/resolve.json?url=' + url
+ '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
100 info_json
= self
._download
_webpage
(resolv_url
, full_title
)
103 info
= json
.loads(info_json
)
105 for err
in info
['errors']:
106 self
._downloader
.report_error(u
'unable to download video webpage: %s' % compat_str(err
['error_message']))
109 self
.report_extraction(full_title
)
110 for track
in info
['tracks']:
111 video_id
= track
['id']
113 streams_url
= 'https://api.sndcdn.com/i1/tracks/' + str(video_id
) + '/streams?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
114 stream_json
= self
._download
_webpage
(streams_url
, video_id
, u
'Downloading track info JSON')
116 self
.report_extraction(video_id
)
117 streams
= json
.loads(stream_json
)
118 mediaURL
= streams
['http_mp3_128_url']
123 'uploader': track
['user']['username'],
124 'upload_date': unified_strdate(track
['created_at']),
125 'title': track
['title'],
127 'description': track
['description'],