]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/soundcloud.py
Prepare for release.
[youtubedl] / youtube_dl / extractor / soundcloud.py
1 import json
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6 compat_str,
7
8 ExtractorError,
9 unified_strdate,
10 )
11
12
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
20 """
21
22 _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/([\w\d-]+)(?:[?].*)?$'
23 IE_NAME = u'soundcloud'
24 _TEST = {
25 u'url': u'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy',
26 u'file': u'62986583.mp3',
27 u'md5': u'ebef0a451b909710ed1d7787dddbf0d7',
28 u'info_dict': {
29 u"upload_date": u"20121011",
30 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",
31 u"uploader": u"E.T. ExTerrestrial Music",
32 u"title": u"Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1"
33 }
34 }
35
36 def report_resolve(self, video_id):
37 """Report information extraction."""
38 self.to_screen(u'%s: Resolving id' % video_id)
39
40 def _real_extract(self, url):
41 mobj = re.match(self._VALID_URL, url)
42 if mobj is None:
43 raise ExtractorError(u'Invalid URL: %s' % url)
44
45 # extract uploader (which is in the url)
46 uploader = mobj.group(1)
47 # extract simple title (uploader + slug of song title)
48 slug_title = mobj.group(2)
49 full_title = '%s/%s' % (uploader, slug_title)
50
51 self.report_resolve(full_title)
52
53 url = 'http://soundcloud.com/%s/%s' % (uploader, slug_title)
54 resolv_url = 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
55 info_json = self._download_webpage(resolv_url, full_title, u'Downloading info JSON')
56
57 info = json.loads(info_json)
58 video_id = info['id']
59 self.report_extraction(full_title)
60
61 streams_url = 'https://api.sndcdn.com/i1/tracks/' + str(video_id) + '/streams?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
62 stream_json = self._download_webpage(streams_url, full_title,
63 u'Downloading stream definitions',
64 u'unable to download stream definitions')
65
66 streams = json.loads(stream_json)
67 mediaURL = streams['http_mp3_128_url']
68 upload_date = unified_strdate(info['created_at'])
69
70 return [{
71 'id': info['id'],
72 'url': mediaURL,
73 'uploader': info['user']['username'],
74 'upload_date': upload_date,
75 'title': info['title'],
76 'ext': u'mp3',
77 'description': info['description'],
78 }]
79
80 class SoundcloudSetIE(InfoExtractor):
81 """Information extractor for soundcloud.com sets
82 To access the media, the uid of the song and a stream token
83 must be extracted from the page source and the script must make
84 a request to media.soundcloud.com/crossdomain.xml. Then
85 the media can be grabbed by requesting from an url composed
86 of the stream token and uid
87 """
88
89 _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)(?:[?].*)?$'
90 IE_NAME = u'soundcloud:set'
91 _TEST = {
92 u"url":"https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep",
93 u"playlist": [
94 {
95 u"file":"30510138.mp3",
96 u"md5":"f9136bf103901728f29e419d2c70f55d",
97 u"info_dict": {
98 u"upload_date": u"20111213",
99 u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
100 u"uploader": u"The Royal Concept",
101 u"title": u"D-D-Dance"
102 }
103 },
104 {
105 u"file":"47127625.mp3",
106 u"md5":"09b6758a018470570f8fd423c9453dd8",
107 u"info_dict": {
108 u"upload_date": u"20120521",
109 u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
110 u"uploader": u"The Royal Concept",
111 u"title": u"The Royal Concept - Gimme Twice"
112 }
113 },
114 {
115 u"file":"47127627.mp3",
116 u"md5":"154abd4e418cea19c3b901f1e1306d9c",
117 u"info_dict": {
118 u"upload_date": u"20120521",
119 u"uploader": u"The Royal Concept",
120 u"title": u"Goldrushed"
121 }
122 },
123 {
124 u"file":"47127629.mp3",
125 u"md5":"2f5471edc79ad3f33a683153e96a79c1",
126 u"info_dict": {
127 u"upload_date": u"20120521",
128 u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
129 u"uploader": u"The Royal Concept",
130 u"title": u"In the End"
131 }
132 },
133 {
134 u"file":"47127631.mp3",
135 u"md5":"f9ba87aa940af7213f98949254f1c6e2",
136 u"info_dict": {
137 u"upload_date": u"20120521",
138 u"description": u"The Royal Concept from Stockholm\r\nFilip / David / Povel / Magnus\r\nwww.theroyalconceptband.com",
139 u"uploader": u"The Royal Concept",
140 u"title": u"Knocked Up"
141 }
142 },
143 {
144 u"file":"75206121.mp3",
145 u"md5":"f9d1fe9406717e302980c30de4af9353",
146 u"info_dict": {
147 u"upload_date": u"20130116",
148 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! ",
149 u"uploader": u"The Royal Concept",
150 u"title": u"World On Fire"
151 }
152 }
153 ]
154 }
155
156 def report_resolve(self, video_id):
157 """Report information extraction."""
158 self.to_screen(u'%s: Resolving id' % video_id)
159
160 def _real_extract(self, url):
161 mobj = re.match(self._VALID_URL, url)
162 if mobj is None:
163 raise ExtractorError(u'Invalid URL: %s' % url)
164
165 # extract uploader (which is in the url)
166 uploader = mobj.group(1)
167 # extract simple title (uploader + slug of song title)
168 slug_title = mobj.group(2)
169 full_title = '%s/sets/%s' % (uploader, slug_title)
170
171 self.report_resolve(full_title)
172
173 url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title)
174 resolv_url = 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
175 info_json = self._download_webpage(resolv_url, full_title)
176
177 videos = []
178 info = json.loads(info_json)
179 if 'errors' in info:
180 for err in info['errors']:
181 self._downloader.report_error(u'unable to download video webpage: %s' % compat_str(err['error_message']))
182 return
183
184 self.report_extraction(full_title)
185 for track in info['tracks']:
186 video_id = track['id']
187
188 streams_url = 'https://api.sndcdn.com/i1/tracks/' + str(video_id) + '/streams?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
189 stream_json = self._download_webpage(streams_url, video_id, u'Downloading track info JSON')
190
191 self.report_extraction(video_id)
192 streams = json.loads(stream_json)
193 mediaURL = streams['http_mp3_128_url']
194
195 videos.append({
196 'id': video_id,
197 'url': mediaURL,
198 'uploader': track['user']['username'],
199 'upload_date': unified_strdate(track['created_at']),
200 'title': track['title'],
201 'ext': u'mp3',
202 'description': track['description'],
203 })
204 return videos