7e8cb0acf11b0ed63783e80b596f36d38b642f9f
[youtube-dl] / 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
25     def report_resolve(self, video_id):
26         """Report information extraction."""
27         self.to_screen(u'%s: Resolving id' % video_id)
28
29     def _real_extract(self, url):
30         mobj = re.match(self._VALID_URL, url)
31         if mobj is None:
32             raise ExtractorError(u'Invalid URL: %s' % url)
33
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)
39
40         self.report_resolve(full_title)
41
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')
45
46         info = json.loads(info_json)
47         video_id = info['id']
48         self.report_extraction(full_title)
49
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')
54
55         streams = json.loads(stream_json)
56         mediaURL = streams['http_mp3_128_url']
57         upload_date = unified_strdate(info['created_at'])
58
59         return [{
60             'id':       info['id'],
61             'url':      mediaURL,
62             'uploader': info['user']['username'],
63             'upload_date': upload_date,
64             'title':    info['title'],
65             'ext':      u'mp3',
66             'description': info['description'],
67         }]
68
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
76      """
77
78     _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)'
79     IE_NAME = u'soundcloud:set'
80     _TEST = {
81         u"name": u"SoundcloudSet",
82         u"url":"https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep",
83         u"playlist": [
84             {
85                 u"file":"30510138.mp3",
86                 u"md5":"f9136bf103901728f29e419d2c70f55d",
87                 u"info_dict": {
88                     u"upload_date": u"20111213",
89                     u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
90                     u"uploader": u"The Royal Concept",
91                     u"title": u"D-D-Dance"
92                 }
93             },
94             {
95                 u"file":"47127625.mp3",
96                 u"md5":"09b6758a018470570f8fd423c9453dd8",
97                 u"info_dict": {
98                     u"upload_date": u"20120521",
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"The Royal Concept - Gimme Twice"
102                 }
103             },
104             {
105                 u"file":"47127627.mp3",
106                 u"md5":"154abd4e418cea19c3b901f1e1306d9c",
107                 u"info_dict": {
108                     u"upload_date": u"20120521",
109                     u"uploader": u"The Royal Concept",
110                     u"title": u"Goldrushed"
111                 }
112             },
113             {
114                 u"file":"47127629.mp3",
115                 u"md5":"2f5471edc79ad3f33a683153e96a79c1",
116                 u"info_dict": {
117                     u"upload_date": u"20120521",
118                     u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
119                     u"uploader": u"The Royal Concept",
120                     u"title": u"In the End"
121                 }
122             },
123             {
124                 u"file":"47127631.mp3",
125                 u"md5":"f9ba87aa940af7213f98949254f1c6e2",
126                 u"info_dict": {
127                     u"upload_date": u"20120521",
128                     u"description": u"The Royal Concept from Stockholm\r\nFilip / David / Povel / Magnus\r\nwww.theroyalconceptband.com",
129                     u"uploader": u"The Royal Concept",
130                     u"title": u"Knocked Up"
131                 }
132             },
133             {
134                 u"file":"75206121.mp3",
135                 u"md5":"f9d1fe9406717e302980c30de4af9353",
136                 u"info_dict": {
137                     u"upload_date": u"20130116",
138                     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!  ",
139                     u"uploader": u"The Royal Concept",
140                     u"title": u"World On Fire"
141                 }
142             }
143         ]
144     }
145
146     def report_resolve(self, video_id):
147         """Report information extraction."""
148         self.to_screen(u'%s: Resolving id' % video_id)
149
150     def _real_extract(self, url):
151         mobj = re.match(self._VALID_URL, url)
152         if mobj is None:
153             raise ExtractorError(u'Invalid URL: %s' % url)
154
155         # extract uploader (which is in the url)
156         uploader = mobj.group(1)
157         # extract simple title (uploader + slug of song title)
158         slug_title =  mobj.group(2)
159         full_title = '%s/sets/%s' % (uploader, slug_title)
160
161         self.report_resolve(full_title)
162
163         url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title)
164         resolv_url = 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
165         info_json = self._download_webpage(resolv_url, full_title)
166
167         videos = []
168         info = json.loads(info_json)
169         if 'errors' in info:
170             for err in info['errors']:
171                 self._downloader.report_error(u'unable to download video webpage: %s' % compat_str(err['error_message']))
172             return
173
174         self.report_extraction(full_title)
175         for track in info['tracks']:
176             video_id = track['id']
177
178             streams_url = 'https://api.sndcdn.com/i1/tracks/' + str(video_id) + '/streams?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
179             stream_json = self._download_webpage(streams_url, video_id, u'Downloading track info JSON')
180
181             self.report_extraction(video_id)
182             streams = json.loads(stream_json)
183             mediaURL = streams['http_mp3_128_url']
184
185             videos.append({
186                 'id':       video_id,
187                 'url':      mediaURL,
188                 'uploader': track['user']['username'],
189                 'upload_date':  unified_strdate(track['created_at']),
190                 'title':    track['title'],
191                 'ext':      u'mp3',
192                 'description': track['description'],
193             })
194         return videos