Merge remote-tracking branch 'rzhxeo/blip'
[youtube-dl] / youtube_dl / extractor / soundcloud.py
1 # encoding: utf-8
2 import json
3 import re
4 import itertools
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_str,
9     compat_urlparse,
10     compat_urllib_parse,
11
12     ExtractorError,
13     unified_strdate,
14 )
15
16
17 class SoundcloudIE(InfoExtractor):
18     """Information extractor for soundcloud.com
19        To access the media, the uid of the song and a stream token
20        must be extracted from the page source and the script must make
21        a request to media.soundcloud.com/crossdomain.xml. Then
22        the media can be grabbed by requesting from an url composed
23        of the stream token and uid
24      """
25
26     _VALID_URL = r'''^(?:https?://)?
27                     (?:(?:(?:www\.|m\.)?soundcloud\.com/
28                             (?P<uploader>[\w\d-]+)/
29                             (?!sets/)(?P<title>[\w\d-]+)/?
30                             (?P<token>[^?]+?)?(?:[?].*)?$)
31                        |(?:api\.soundcloud\.com/tracks/(?P<track_id>\d+))
32                        |(?P<player>(?:w|player|p.)\.soundcloud\.com/player/?.*?url=.*)
33                     )
34                     '''
35     IE_NAME = u'soundcloud'
36     _TESTS = [
37         {
38             u'url': u'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy',
39             u'file': u'62986583.mp3',
40             u'md5': u'ebef0a451b909710ed1d7787dddbf0d7',
41             u'info_dict': {
42                 u"upload_date": u"20121011", 
43                 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", 
44                 u"uploader": u"E.T. ExTerrestrial Music", 
45                 u"title": u"Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1"
46             }
47         },
48         # not streamable song
49         {
50             u'url': u'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep',
51             u'info_dict': {
52                 u'id': u'47127627',
53                 u'ext': u'mp3',
54                 u'title': u'Goldrushed',
55                 u'uploader': u'The Royal Concept',
56                 u'upload_date': u'20120521',
57             },
58             u'params': {
59                 # rtmp
60                 u'skip_download': True,
61             },
62         },
63         # private link
64         {
65             u'url': u'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp',
66             u'md5': u'aa0dd32bfea9b0c5ef4f02aacd080604',
67             u'info_dict': {
68                 u'id': u'123998367',
69                 u'ext': u'mp3',
70                 u'title': u'Youtube - Dl Test Video \'\' Ä↭',
71                 u'uploader': u'jaimeMF',
72                 u'description': u'test chars:  \"\'/\\ä↭',
73                 u'upload_date': u'20131209',
74             },
75         },
76         # downloadable song
77         {
78             u'url': u'https://soundcloud.com/simgretina/just-your-problem-baby-1',
79             u'md5': u'56a8b69568acaa967b4c49f9d1d52d19',
80             u'info_dict': {
81                 u'id': u'105614606',
82                 u'ext': u'wav',
83                 u'title': u'Just Your Problem Baby (Acapella)',
84                 u'description': u'Vocals',
85                 u'uploader': u'Sim Gretina',
86                 u'upload_date': u'20130815',
87             },
88         },
89     ]
90
91     _CLIENT_ID = 'b45b1aa10f1ac2941910a7f0d10f8e28'
92     _IPHONE_CLIENT_ID = '376f225bf427445fc4bfb6b99b72e0bf'
93
94     @classmethod
95     def suitable(cls, url):
96         return re.match(cls._VALID_URL, url, flags=re.VERBOSE) is not None
97
98     def report_resolve(self, video_id):
99         """Report information extraction."""
100         self.to_screen(u'%s: Resolving id' % video_id)
101
102     @classmethod
103     def _resolv_url(cls, url):
104         return 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=' + cls._CLIENT_ID
105
106     def _extract_info_dict(self, info, full_title=None, quiet=False, secret_token=None):
107         track_id = compat_str(info['id'])
108         name = full_title or track_id
109         if quiet:
110             self.report_extraction(name)
111
112         thumbnail = info['artwork_url']
113         if thumbnail is not None:
114             thumbnail = thumbnail.replace('-large', '-t500x500')
115         ext = u'mp3'
116         result = {
117             'id': track_id,
118             'uploader': info['user']['username'],
119             'upload_date': unified_strdate(info['created_at']),
120             'title': info['title'],
121             'description': info['description'],
122             'thumbnail': thumbnail,
123         }
124         if info.get('downloadable', False):
125             # We can build a direct link to the song
126             format_url = (
127                 u'https://api.soundcloud.com/tracks/{0}/download?client_id={1}'.format(
128                     track_id, self._CLIENT_ID))
129             result['formats'] = [{
130                 'format_id': 'download',
131                 'ext': info.get('original_format', u'mp3'),
132                 'url': format_url,
133                 'vcodec': 'none',
134             }]
135         else:
136             # We have to retrieve the url
137             streams_url = ('http://api.soundcloud.com/i1/tracks/{0}/streams?'
138                 'client_id={1}&secret_token={2}'.format(track_id, self._IPHONE_CLIENT_ID, secret_token))
139             stream_json = self._download_webpage(
140                 streams_url,
141                 track_id, u'Downloading track url')
142
143             formats = []
144             format_dict = json.loads(stream_json)
145             for key, stream_url in format_dict.items():
146                 if key.startswith(u'http'):
147                     formats.append({
148                         'format_id': key,
149                         'ext': ext,
150                         'url': stream_url,
151                         'vcodec': 'none',
152                     })
153                 elif key.startswith(u'rtmp'):
154                     # The url doesn't have an rtmp app, we have to extract the playpath
155                     url, path = stream_url.split('mp3:', 1)
156                     formats.append({
157                         'format_id': key,
158                         'url': url,
159                         'play_path': 'mp3:' + path,
160                         'ext': ext,
161                         'vcodec': 'none',
162                     })
163
164             if not formats:
165                 # We fallback to the stream_url in the original info, this
166                 # cannot be always used, sometimes it can give an HTTP 404 error
167                 formats.append({
168                     'format_id': u'fallback',
169                     'url': info['stream_url'] + '?client_id=' + self._CLIENT_ID,
170                     'ext': ext,
171                     'vcodec': 'none',
172                 })
173
174             def format_pref(f):
175                 if f['format_id'].startswith('http'):
176                     return 2
177                 if f['format_id'].startswith('rtmp'):
178                     return 1
179                 return 0
180
181             formats.sort(key=format_pref)
182             result['formats'] = formats
183
184         return result
185
186     def _real_extract(self, url):
187         mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE)
188         if mobj is None:
189             raise ExtractorError(u'Invalid URL: %s' % url)
190
191         track_id = mobj.group('track_id')
192         token = None
193         if track_id is not None:
194             info_json_url = 'http://api.soundcloud.com/tracks/' + track_id + '.json?client_id=' + self._CLIENT_ID
195             full_title = track_id
196         elif mobj.group('player'):
197             query = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
198             return self.url_result(query['url'][0], ie='Soundcloud')
199         else:
200             # extract uploader (which is in the url)
201             uploader = mobj.group('uploader')
202             # extract simple title (uploader + slug of song title)
203             slug_title =  mobj.group('title')
204             token = mobj.group('token')
205             full_title = resolve_title = '%s/%s' % (uploader, slug_title)
206             if token:
207                 resolve_title += '/%s' % token
208     
209             self.report_resolve(full_title)
210     
211             url = 'http://soundcloud.com/%s' % resolve_title
212             info_json_url = self._resolv_url(url)
213         info_json = self._download_webpage(info_json_url, full_title, u'Downloading info JSON')
214
215         info = json.loads(info_json)
216         return self._extract_info_dict(info, full_title, secret_token=token)
217
218 class SoundcloudSetIE(SoundcloudIE):
219     _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)(?:[?].*)?$'
220     IE_NAME = u'soundcloud:set'
221     # it's in tests/test_playlists.py
222     _TESTS = []
223
224     def _real_extract(self, url):
225         mobj = re.match(self._VALID_URL, url)
226         if mobj is None:
227             raise ExtractorError(u'Invalid URL: %s' % url)
228
229         # extract uploader (which is in the url)
230         uploader = mobj.group(1)
231         # extract simple title (uploader + slug of song title)
232         slug_title =  mobj.group(2)
233         full_title = '%s/sets/%s' % (uploader, slug_title)
234
235         self.report_resolve(full_title)
236
237         url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title)
238         resolv_url = self._resolv_url(url)
239         info_json = self._download_webpage(resolv_url, full_title)
240
241         info = json.loads(info_json)
242         if 'errors' in info:
243             for err in info['errors']:
244                 self._downloader.report_error(u'unable to download video webpage: %s' % compat_str(err['error_message']))
245             return
246
247         self.report_extraction(full_title)
248         return {'_type': 'playlist',
249                 'entries': [self._extract_info_dict(track) for track in info['tracks']],
250                 'id': info['id'],
251                 'title': info['title'],
252                 }
253
254
255 class SoundcloudUserIE(SoundcloudIE):
256     _VALID_URL = r'https?://(www\.)?soundcloud\.com/(?P<user>[^/]+)(/?(tracks/)?)?(\?.*)?$'
257     IE_NAME = u'soundcloud:user'
258
259     # it's in tests/test_playlists.py
260     _TESTS = []
261
262     def _real_extract(self, url):
263         mobj = re.match(self._VALID_URL, url)
264         uploader = mobj.group('user')
265
266         url = 'http://soundcloud.com/%s/' % uploader
267         resolv_url = self._resolv_url(url)
268         user_json = self._download_webpage(resolv_url, uploader,
269             u'Downloading user info')
270         user = json.loads(user_json)
271
272         tracks = []
273         for i in itertools.count():
274             data = compat_urllib_parse.urlencode({'offset': i*50,
275                                                   'client_id': self._CLIENT_ID,
276                                                   })
277             tracks_url = 'http://api.soundcloud.com/users/%s/tracks.json?' % user['id'] + data
278             response = self._download_webpage(tracks_url, uploader, 
279                 u'Downloading tracks page %s' % (i+1))
280             new_tracks = json.loads(response)
281             tracks.extend(self._extract_info_dict(track, quiet=True) for track in new_tracks)
282             if len(new_tracks) < 50:
283                 break
284
285         return {
286             '_type': 'playlist',
287             'id': compat_str(user['id']),
288             'title': user['username'],
289             'entries': tracks,
290         }