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