]>
Commit | Line | Data |
---|---|---|
dcdb292f | 1 | # coding: utf-8 |
fbcd7b5f PH |
2 | from __future__ import unicode_literals |
3 | ||
92790f4e | 4 | import itertools |
73602bcd | 5 | import re |
aad0d6d5 | 6 | |
2abf7cab | 7 | from .common import ( |
8 | InfoExtractor, | |
9 | SearchInfoExtractor | |
10 | ) | |
1cc79574 | 11 | from ..compat import ( |
3bed6217 RA |
12 | compat_HTTPError, |
13 | compat_kwargs, | |
aad0d6d5 | 14 | compat_str, |
668de34c | 15 | compat_urlparse, |
1cc79574 PH |
16 | ) |
17 | from ..utils import ( | |
de7aade2 | 18 | error_to_compat_str, |
aad0d6d5 | 19 | ExtractorError, |
e09965d5 | 20 | float_or_none, |
548c3957 | 21 | HEADRequest, |
eb920777 | 22 | int_or_none, |
e09965d5 | 23 | KNOWN_EXTENSIONS, |
e09965d5 S |
24 | mimetype2ext, |
25 | str_or_none, | |
f516f440 S |
26 | try_get, |
27 | unified_timestamp, | |
d7c7100e | 28 | update_url_query, |
7c5307f4 | 29 | url_or_none, |
a6c5859d | 30 | urlhandle_detect_ext, |
d7c7100e | 31 | ) |
aad0d6d5 PH |
32 | |
33 | ||
548c3957 | 34 | class SoundcloudEmbedIE(InfoExtractor): |
cf80ff18 RA |
35 | _VALID_URL = r'https?://(?:w|player|p)\.soundcloud\.com/player/?.*?\burl=(?P<id>.+)' |
36 | _TEST = { | |
37 | # from https://www.soundi.fi/uutiset/ennakkokuuntelussa-timo-kaukolammen-station-to-station-to-station-julkaisua-juhlitaan-tanaan-g-livelabissa/ | |
38 | 'url': 'https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F922213810&show_artwork=true&maxwidth=640&maxheight=960&dnt=1&secret_token=s-ziYey', | |
39 | 'only_matching': True, | |
40 | } | |
548c3957 RA |
41 | |
42 | @staticmethod | |
43 | def _extract_urls(webpage): | |
44 | return [m.group('url') for m in re.finditer( | |
45 | r'<iframe[^>]+src=(["\'])(?P<url>(?:https?://)?(?:w\.)?soundcloud\.com/player.+?)\1', | |
46 | webpage)] | |
47 | ||
48 | def _real_extract(self, url): | |
cf80ff18 RA |
49 | query = compat_urlparse.parse_qs( |
50 | compat_urlparse.urlparse(url).query) | |
51 | api_url = query['url'][0] | |
52 | secret_token = query.get('secret_token') | |
53 | if secret_token: | |
54 | api_url = update_url_query(api_url, {'secret_token': secret_token[0]}) | |
55 | return self.url_result(api_url) | |
548c3957 RA |
56 | |
57 | ||
aad0d6d5 PH |
58 | class SoundcloudIE(InfoExtractor): |
59 | """Information extractor for soundcloud.com | |
60 | To access the media, the uid of the song and a stream token | |
61 | must be extracted from the page source and the script must make | |
62 | a request to media.soundcloud.com/crossdomain.xml. Then | |
63 | the media can be grabbed by requesting from an url composed | |
64 | of the stream token and uid | |
65 | """ | |
66 | ||
20991253 | 67 | _VALID_URL = r'''(?x)^(?:https?://)? |
71507a11 | 68 | (?:(?:(?:www\.|m\.)?soundcloud\.com/ |
836ef264 | 69 | (?!stations/track) |
4ff50ef8 | 70 | (?P<uploader>[\w\d-]+)/ |
3ef2da2d | 71 | (?!(?:tracks|albums|sets(?:/.+?)?|reposts|likes|spotlight)/?(?:$|[?#])) |
22a6f150 | 72 | (?P<title>[\w\d-]+)/? |
de2dd4c5 | 73 | (?P<token>[^?]+?)?(?:[?].*)?$) |
548c3957 | 74 | |(?:api(?:-v2)?\.soundcloud\.com/tracks/(?P<track_id>\d+) |
0403b069 | 75 | (?:/?\?secret_token=(?P<secret_token>[^&]+))?) |
eb6a41ba JMF |
76 | ) |
77 | ''' | |
fbcd7b5f | 78 | IE_NAME = 'soundcloud' |
12c167c8 JMF |
79 | _TESTS = [ |
80 | { | |
fbcd7b5f | 81 | 'url': 'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy', |
fbcd7b5f PH |
82 | 'md5': 'ebef0a451b909710ed1d7787dddbf0d7', |
83 | 'info_dict': { | |
0eb9fb9f JMF |
84 | 'id': '62986583', |
85 | 'ext': 'mp3', | |
f516f440 | 86 | 'title': 'Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1', |
0eb9fb9f JMF |
87 | 'description': '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', |
88 | 'uploader': 'E.T. ExTerrestrial Music', | |
548c3957 | 89 | 'uploader_id': '1571244', |
f516f440 S |
90 | 'timestamp': 1349920598, |
91 | 'upload_date': '20121011', | |
e09965d5 | 92 | 'duration': 143.216, |
4bfd294e | 93 | 'license': 'all-rights-reserved', |
f516f440 S |
94 | 'view_count': int, |
95 | 'like_count': int, | |
96 | 'comment_count': int, | |
97 | 'repost_count': int, | |
12c167c8 JMF |
98 | } |
99 | }, | |
a6c5859d | 100 | # geo-restricted |
12c167c8 | 101 | { |
fbcd7b5f PH |
102 | 'url': 'https://soundcloud.com/the-concept-band/goldrushed-mastered?in=the-concept-band/sets/the-royal-concept-ep', |
103 | 'info_dict': { | |
104 | 'id': '47127627', | |
105 | 'ext': 'mp3', | |
106 | 'title': 'Goldrushed', | |
63ad0315 | 107 | 'description': 'From Stockholm Sweden\r\nPovel / Magnus / Filip / David\r\nwww.theroyalconcept.com', |
fbcd7b5f | 108 | 'uploader': 'The Royal Concept', |
548c3957 | 109 | 'uploader_id': '9615865', |
f516f440 | 110 | 'timestamp': 1337635207, |
fbcd7b5f | 111 | 'upload_date': '20120521', |
a6c5859d | 112 | 'duration': 227.155, |
4bfd294e | 113 | 'license': 'all-rights-reserved', |
f516f440 S |
114 | 'view_count': int, |
115 | 'like_count': int, | |
116 | 'comment_count': int, | |
117 | 'repost_count': int, | |
12c167c8 | 118 | }, |
12c167c8 | 119 | }, |
de2dd4c5 JMF |
120 | # private link |
121 | { | |
fbcd7b5f PH |
122 | 'url': 'https://soundcloud.com/jaimemf/youtube-dl-test-video-a-y-baw/s-8Pjrp', |
123 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
124 | 'info_dict': { | |
125 | 'id': '123998367', | |
126 | 'ext': 'mp3', | |
127 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
fbcd7b5f | 128 | 'description': 'test chars: \"\'/\\ä↭', |
f516f440 | 129 | 'uploader': 'jaimeMF', |
548c3957 | 130 | 'uploader_id': '69767071', |
f516f440 | 131 | 'timestamp': 1386604920, |
fbcd7b5f | 132 | 'upload_date': '20131209', |
e09965d5 | 133 | 'duration': 9.927, |
4bfd294e | 134 | 'license': 'all-rights-reserved', |
f516f440 S |
135 | 'view_count': int, |
136 | 'like_count': int, | |
137 | 'comment_count': int, | |
138 | 'repost_count': int, | |
de2dd4c5 JMF |
139 | }, |
140 | }, | |
9296738f | 141 | # private link (alt format) |
142 | { | |
143 | 'url': 'https://api.soundcloud.com/tracks/123998367?secret_token=s-8Pjrp', | |
144 | 'md5': 'aa0dd32bfea9b0c5ef4f02aacd080604', | |
145 | 'info_dict': { | |
146 | 'id': '123998367', | |
147 | 'ext': 'mp3', | |
148 | 'title': 'Youtube - Dl Test Video \'\' Ä↭', | |
9296738f | 149 | 'description': 'test chars: \"\'/\\ä↭', |
f516f440 | 150 | 'uploader': 'jaimeMF', |
548c3957 | 151 | 'uploader_id': '69767071', |
f516f440 | 152 | 'timestamp': 1386604920, |
9296738f | 153 | 'upload_date': '20131209', |
e09965d5 | 154 | 'duration': 9.927, |
4bfd294e | 155 | 'license': 'all-rights-reserved', |
f516f440 S |
156 | 'view_count': int, |
157 | 'like_count': int, | |
158 | 'comment_count': int, | |
159 | 'repost_count': int, | |
9296738f | 160 | }, |
161 | }, | |
f67ca84d JMF |
162 | # downloadable song |
163 | { | |
00a82ea8 | 164 | 'url': 'https://soundcloud.com/oddsamples/bus-brakes', |
eae12e3f | 165 | 'md5': '7624f2351f8a3b2e7cd51522496e7631', |
fbcd7b5f | 166 | 'info_dict': { |
00a82ea8 | 167 | 'id': '128590877', |
eae12e3f | 168 | 'ext': 'mp3', |
00a82ea8 | 169 | 'title': 'Bus Brakes', |
0eb9fb9f | 170 | 'description': 'md5:0053ca6396e8d2fd7b7e1595ef12ab66', |
00a82ea8 | 171 | 'uploader': 'oddsamples', |
548c3957 | 172 | 'uploader_id': '73680509', |
f516f440 | 173 | 'timestamp': 1389232924, |
00a82ea8 | 174 | 'upload_date': '20140109', |
e09965d5 | 175 | 'duration': 17.346, |
4bfd294e | 176 | 'license': 'cc-by-sa', |
f516f440 S |
177 | 'view_count': int, |
178 | 'like_count': int, | |
179 | 'comment_count': int, | |
180 | 'repost_count': int, | |
f67ca84d JMF |
181 | }, |
182 | }, | |
d7c7100e S |
183 | # private link, downloadable format |
184 | { | |
185 | 'url': 'https://soundcloud.com/oriuplift/uponly-238-no-talking-wav/s-AyZUd', | |
186 | 'md5': '64a60b16e617d41d0bef032b7f55441e', | |
187 | 'info_dict': { | |
188 | 'id': '340344461', | |
189 | 'ext': 'wav', | |
190 | 'title': 'Uplifting Only 238 [No Talking] (incl. Alex Feed Guestmix) (Aug 31, 2017) [wav]', | |
191 | 'description': 'md5:fa20ee0fca76a3d6df8c7e57f3715366', | |
192 | 'uploader': 'Ori Uplift Music', | |
548c3957 | 193 | 'uploader_id': '12563093', |
f516f440 | 194 | 'timestamp': 1504206263, |
d7c7100e | 195 | 'upload_date': '20170831', |
e09965d5 | 196 | 'duration': 7449.096, |
d7c7100e | 197 | 'license': 'all-rights-reserved', |
f516f440 S |
198 | 'view_count': int, |
199 | 'like_count': int, | |
200 | 'comment_count': int, | |
201 | 'repost_count': int, | |
d7c7100e S |
202 | }, |
203 | }, | |
0b0870f9 PV |
204 | # no album art, use avatar pic for thumbnail |
205 | { | |
206 | 'url': 'https://soundcloud.com/garyvee/sideways-prod-mad-real', | |
207 | 'md5': '59c7872bc44e5d99b7211891664760c2', | |
208 | 'info_dict': { | |
209 | 'id': '309699954', | |
210 | 'ext': 'mp3', | |
211 | 'title': 'Sideways (Prod. Mad Real)', | |
212 | 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e', | |
213 | 'uploader': 'garyvee', | |
548c3957 | 214 | 'uploader_id': '2366352', |
f516f440 | 215 | 'timestamp': 1488152409, |
0b0870f9 | 216 | 'upload_date': '20170226', |
e09965d5 | 217 | 'duration': 207.012, |
0b0870f9 PV |
218 | 'thumbnail': r're:https?://.*\.jpg', |
219 | 'license': 'all-rights-reserved', | |
f516f440 S |
220 | 'view_count': int, |
221 | 'like_count': int, | |
222 | 'comment_count': int, | |
223 | 'repost_count': int, | |
0b0870f9 PV |
224 | }, |
225 | 'params': { | |
226 | 'skip_download': True, | |
227 | }, | |
228 | }, | |
e09965d5 S |
229 | { |
230 | 'url': 'https://soundcloud.com/giovannisarani/mezzo-valzer', | |
231 | 'md5': 'e22aecd2bc88e0e4e432d7dcc0a1abf7', | |
232 | 'info_dict': { | |
233 | 'id': '583011102', | |
234 | 'ext': 'mp3', | |
235 | 'title': 'Mezzo Valzer', | |
236 | 'description': 'md5:4138d582f81866a530317bae316e8b61', | |
2a5c26c9 | 237 | 'uploader': 'Micronie', |
548c3957 | 238 | 'uploader_id': '3352531', |
e09965d5 S |
239 | 'timestamp': 1551394171, |
240 | 'upload_date': '20190228', | |
241 | 'duration': 180.157, | |
242 | 'thumbnail': r're:https?://.*\.jpg', | |
243 | 'license': 'all-rights-reserved', | |
244 | 'view_count': int, | |
245 | 'like_count': int, | |
246 | 'comment_count': int, | |
247 | 'repost_count': int, | |
248 | }, | |
e09965d5 | 249 | } |
12c167c8 | 250 | ] |
aad0d6d5 | 251 | |
548c3957 RA |
252 | _API_V2_BASE = 'https://api-v2.soundcloud.com/' |
253 | _BASE_URL = 'https://soundcloud.com/' | |
548c3957 RA |
254 | _IMAGE_REPL_RE = r'-([0-9a-z]+)\.jpg' |
255 | ||
256 | _ARTWORK_MAP = { | |
257 | 'mini': 16, | |
258 | 'tiny': 20, | |
259 | 'small': 32, | |
260 | 'badge': 47, | |
261 | 't67x67': 67, | |
262 | 'large': 100, | |
263 | 't300x300': 300, | |
264 | 'crop': 400, | |
265 | 't500x500': 500, | |
266 | 'original': 0, | |
267 | } | |
fbdf8d15 | 268 | |
de7aade2 RA |
269 | def _store_client_id(self, client_id): |
270 | self._downloader.cache.store('soundcloud', 'client_id', client_id) | |
271 | ||
3bed6217 RA |
272 | def _update_client_id(self): |
273 | webpage = self._download_webpage('https://soundcloud.com/', None) | |
274 | for src in reversed(re.findall(r'<script[^>]+src="([^"]+)"', webpage)): | |
275 | script = self._download_webpage(src, None, fatal=False) | |
276 | if script: | |
277 | client_id = self._search_regex( | |
278 | r'client_id\s*:\s*"([0-9a-zA-Z]{32})"', | |
279 | script, 'client id', default=None) | |
280 | if client_id: | |
281 | self._CLIENT_ID = client_id | |
de7aade2 | 282 | self._store_client_id(client_id) |
3bed6217 RA |
283 | return |
284 | raise ExtractorError('Unable to extract client id') | |
285 | ||
286 | def _download_json(self, *args, **kwargs): | |
de7aade2 RA |
287 | non_fatal = kwargs.get('fatal') is False |
288 | if non_fatal: | |
289 | del kwargs['fatal'] | |
3bed6217 RA |
290 | query = kwargs.get('query', {}).copy() |
291 | for _ in range(2): | |
292 | query['client_id'] = self._CLIENT_ID | |
293 | kwargs['query'] = query | |
294 | try: | |
295 | return super(SoundcloudIE, self)._download_json(*args, **compat_kwargs(kwargs)) | |
296 | except ExtractorError as e: | |
297 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401: | |
de7aade2 | 298 | self._store_client_id(None) |
3bed6217 RA |
299 | self._update_client_id() |
300 | continue | |
de7aade2 RA |
301 | elif non_fatal: |
302 | self._downloader.report_warning(error_to_compat_str(e)) | |
303 | return False | |
3bed6217 RA |
304 | raise |
305 | ||
306 | def _real_initialize(self): | |
307 | self._CLIENT_ID = self._downloader.cache.load('soundcloud', 'client_id') or 'YUKXoArFcqrlQn9tfNHvvyfnDISj04zk' | |
308 | ||
7d239269 JMF |
309 | @classmethod |
310 | def _resolv_url(cls, url): | |
3bed6217 | 311 | return SoundcloudIE._API_V2_BASE + 'resolve?url=' + url |
7d239269 | 312 | |
a6c5859d | 313 | def _extract_info_dict(self, info, full_title=None, secret_token=None): |
12c167c8 | 314 | track_id = compat_str(info['id']) |
f516f440 | 315 | title = info['title'] |
e09965d5 S |
316 | |
317 | format_urls = set() | |
5e114e4b | 318 | formats = [] |
73602bcd | 319 | query = {'client_id': self._CLIENT_ID} |
548c3957 | 320 | if secret_token: |
73602bcd | 321 | query['secret_token'] = secret_token |
548c3957 | 322 | |
2e9ad59a | 323 | if info.get('downloadable') and info.get('has_downloads_left'): |
a6c5859d RA |
324 | download_url = update_url_query( |
325 | self._API_V2_BASE + 'tracks/' + track_id + '/download', query) | |
326 | redirect_url = (self._download_json(download_url, track_id, fatal=False) or {}).get('redirectUri') | |
327 | if redirect_url: | |
328 | urlh = self._request_webpage( | |
329 | HEADRequest(redirect_url), track_id, fatal=False) | |
330 | if urlh: | |
331 | format_url = urlh.geturl() | |
332 | format_urls.add(format_url) | |
333 | formats.append({ | |
334 | 'format_id': 'download', | |
335 | 'ext': urlhandle_detect_ext(urlh) or 'mp3', | |
336 | 'filesize': int_or_none(urlh.headers.get('Content-Length')), | |
337 | 'url': format_url, | |
338 | 'preference': 10, | |
339 | }) | |
5e114e4b | 340 | |
548c3957 | 341 | def invalid_url(url): |
e4e5fa6e | 342 | return not url or url in format_urls |
e09965d5 | 343 | |
e4e5fa6e | 344 | def add_format(f, protocol, is_preview=False): |
548c3957 RA |
345 | mobj = re.search(r'\.(?P<abr>\d+)\.(?P<ext>[0-9a-z]{3,4})(?=[/?])', stream_url) |
346 | if mobj: | |
347 | for k, v in mobj.groupdict().items(): | |
348 | if not f.get(k): | |
349 | f[k] = v | |
350 | format_id_list = [] | |
351 | if protocol: | |
352 | format_id_list.append(protocol) | |
353 | for k in ('ext', 'abr'): | |
354 | v = f.get(k) | |
355 | if v: | |
356 | format_id_list.append(v) | |
e4e5fa6e S |
357 | preview = is_preview or re.search(r'/(?:preview|playlist)/0/30/', f['url']) |
358 | if preview: | |
359 | format_id_list.append('preview') | |
548c3957 RA |
360 | abr = f.get('abr') |
361 | if abr: | |
362 | f['abr'] = int(abr) | |
363 | f.update({ | |
364 | 'format_id': '_'.join(format_id_list), | |
365 | 'protocol': 'm3u8_native' if protocol == 'hls' else 'http', | |
e4e5fa6e | 366 | 'preference': -10 if preview else None, |
548c3957 RA |
367 | }) |
368 | formats.append(f) | |
e09965d5 S |
369 | |
370 | # New API | |
371 | transcodings = try_get( | |
372 | info, lambda x: x['media']['transcodings'], list) or [] | |
373 | for t in transcodings: | |
374 | if not isinstance(t, dict): | |
3a194cb4 | 375 | continue |
e09965d5 | 376 | format_url = url_or_none(t.get('url')) |
e4e5fa6e | 377 | if not format_url: |
e09965d5 S |
378 | continue |
379 | stream = self._download_json( | |
548c3957 | 380 | format_url, track_id, query=query, fatal=False) |
e09965d5 S |
381 | if not isinstance(stream, dict): |
382 | continue | |
383 | stream_url = url_or_none(stream.get('url')) | |
548c3957 | 384 | if invalid_url(stream_url): |
e09965d5 S |
385 | continue |
386 | format_urls.add(stream_url) | |
548c3957 RA |
387 | stream_format = t.get('format') or {} |
388 | protocol = stream_format.get('protocol') | |
e09965d5 S |
389 | if protocol != 'hls' and '/hls' in format_url: |
390 | protocol = 'hls' | |
391 | ext = None | |
392 | preset = str_or_none(t.get('preset')) | |
393 | if preset: | |
394 | ext = preset.split('_')[0] | |
548c3957 RA |
395 | if ext not in KNOWN_EXTENSIONS: |
396 | ext = mimetype2ext(stream_format.get('mime_type')) | |
397 | add_format({ | |
e09965d5 | 398 | 'url': stream_url, |
e09965d5 | 399 | 'ext': ext, |
e4e5fa6e S |
400 | }, 'http' if protocol == 'progressive' else protocol, |
401 | t.get('snipped') or '/preview/' in format_url) | |
548c3957 | 402 | |
3a194cb4 S |
403 | for f in formats: |
404 | f['vcodec'] = 'none' | |
2a15e706 | 405 | |
a6c5859d RA |
406 | if not formats and info.get('policy') == 'BLOCK': |
407 | self.raise_geo_restricted() | |
562ceab1 | 408 | self._sort_formats(formats) |
64bb5187 | 409 | |
548c3957 RA |
410 | user = info.get('user') or {} |
411 | ||
412 | thumbnails = [] | |
413 | artwork_url = info.get('artwork_url') | |
414 | thumbnail = artwork_url or user.get('avatar_url') | |
415 | if isinstance(thumbnail, compat_str): | |
416 | if re.search(self._IMAGE_REPL_RE, thumbnail): | |
417 | for image_id, size in self._ARTWORK_MAP.items(): | |
418 | i = { | |
419 | 'id': image_id, | |
420 | 'url': re.sub(self._IMAGE_REPL_RE, '-%s.jpg' % image_id, thumbnail), | |
421 | } | |
422 | if image_id == 'tiny' and not artwork_url: | |
423 | size = 18 | |
424 | elif image_id == 'original': | |
425 | i['preference'] = 10 | |
426 | if size: | |
427 | i.update({ | |
428 | 'width': size, | |
429 | 'height': size, | |
430 | }) | |
431 | thumbnails.append(i) | |
432 | else: | |
433 | thumbnails = [{'url': thumbnail}] | |
434 | ||
435 | def extract_count(key): | |
436 | return int_or_none(info.get('%s_count' % key)) | |
437 | ||
438 | return { | |
439 | 'id': track_id, | |
440 | 'uploader': user.get('username'), | |
441 | 'uploader_id': str_or_none(user.get('id')) or user.get('permalink'), | |
442 | 'uploader_url': user.get('permalink_url'), | |
443 | 'timestamp': unified_timestamp(info.get('created_at')), | |
444 | 'title': title, | |
445 | 'description': info.get('description'), | |
446 | 'thumbnails': thumbnails, | |
447 | 'duration': float_or_none(info.get('duration'), 1000), | |
448 | 'webpage_url': info.get('permalink_url'), | |
449 | 'license': info.get('license'), | |
450 | 'view_count': extract_count('playback'), | |
451 | 'like_count': extract_count('favoritings') or extract_count('likes'), | |
452 | 'comment_count': extract_count('comment'), | |
453 | 'repost_count': extract_count('reposts'), | |
454 | 'genre': info.get('genre'), | |
455 | 'formats': formats | |
456 | } | |
7d239269 | 457 | |
aad0d6d5 | 458 | def _real_extract(self, url): |
548c3957 | 459 | mobj = re.match(self._VALID_URL, url) |
aad0d6d5 | 460 | |
eb6a41ba | 461 | track_id = mobj.group('track_id') |
4bfd294e | 462 | |
3bed6217 | 463 | query = {} |
548c3957 RA |
464 | if track_id: |
465 | info_json_url = self._API_V2_BASE + 'tracks/' + track_id | |
eb6a41ba | 466 | full_title = track_id |
9296738f | 467 | token = mobj.group('secret_token') |
468 | if token: | |
548c3957 | 469 | query['secret_token'] = token |
eb6a41ba | 470 | else: |
548c3957 | 471 | full_title = resolve_title = '%s/%s' % mobj.group('uploader', 'title') |
de2dd4c5 | 472 | token = mobj.group('token') |
de2dd4c5 JMF |
473 | if token: |
474 | resolve_title += '/%s' % token | |
548c3957 | 475 | info_json_url = self._resolv_url(self._BASE_URL + resolve_title) |
5f6a1245 | 476 | |
e09965d5 | 477 | info = self._download_json( |
a6c5859d RA |
478 | info_json_url, full_title, 'Downloading info JSON', query=query) |
479 | ||
480 | return self._extract_info_dict(info, full_title, token) | |
aad0d6d5 | 481 | |
20991253 | 482 | |
7518a61d | 483 | class SoundcloudPlaylistBaseIE(SoundcloudIE): |
2a5c26c9 RA |
484 | def _extract_set(self, playlist, token=None): |
485 | playlist_id = compat_str(playlist['id']) | |
486 | tracks = playlist.get('tracks') or [] | |
487 | if not all([t.get('permalink_url') for t in tracks]) and token: | |
488 | tracks = self._download_json( | |
489 | self._API_V2_BASE + 'tracks', playlist_id, | |
490 | 'Downloading tracks', query={ | |
491 | 'ids': ','.join([compat_str(t['id']) for t in tracks]), | |
492 | 'playlistId': playlist_id, | |
493 | 'playlistSecretToken': token, | |
494 | }) | |
548c3957 RA |
495 | entries = [] |
496 | for track in tracks: | |
497 | track_id = str_or_none(track.get('id')) | |
498 | url = track.get('permalink_url') | |
499 | if not url: | |
500 | if not track_id: | |
501 | continue | |
502 | url = self._API_V2_BASE + 'tracks/' + track_id | |
503 | if token: | |
504 | url += '?secret_token=' + token | |
505 | entries.append(self.url_result( | |
506 | url, SoundcloudIE.ie_key(), track_id)) | |
2a5c26c9 RA |
507 | return self.playlist_result( |
508 | entries, playlist_id, | |
509 | playlist.get('title'), | |
510 | playlist.get('description')) | |
8e45e1cc S |
511 | |
512 | ||
7518a61d | 513 | class SoundcloudSetIE(SoundcloudPlaylistBaseIE): |
c808ef81 | 514 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/(?P<uploader>[\w\d-]+)/sets/(?P<slug_title>[\w\d-]+)(?:/(?P<token>[^?/]+))?' |
fbcd7b5f | 515 | IE_NAME = 'soundcloud:set' |
22a6f150 PH |
516 | _TESTS = [{ |
517 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep', | |
518 | 'info_dict': { | |
a9551e90 | 519 | 'id': '2284613', |
22a6f150 | 520 | 'title': 'The Royal Concept EP', |
2a5c26c9 | 521 | 'description': 'md5:71d07087c7a449e8941a70a29e34671e', |
22a6f150 | 522 | }, |
bf2dc9cc | 523 | 'playlist_mincount': 5, |
f7043ef3 S |
524 | }, { |
525 | 'url': 'https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep/token', | |
526 | 'only_matching': True, | |
22a6f150 | 527 | }] |
aad0d6d5 | 528 | |
aad0d6d5 PH |
529 | def _real_extract(self, url): |
530 | mobj = re.match(self._VALID_URL, url) | |
aad0d6d5 | 531 | |
548c3957 | 532 | full_title = '%s/sets/%s' % mobj.group('uploader', 'slug_title') |
2f834e93 | 533 | token = mobj.group('token') |
534 | if token: | |
535 | full_title += '/' + token | |
aad0d6d5 | 536 | |
548c3957 RA |
537 | info = self._download_json(self._resolv_url( |
538 | self._BASE_URL + full_title), full_title) | |
aad0d6d5 | 539 | |
aad0d6d5 | 540 | if 'errors' in info: |
214e74bf JMF |
541 | msgs = (compat_str(err['error_message']) for err in info['errors']) |
542 | raise ExtractorError('unable to download video webpage: %s' % ','.join(msgs)) | |
aad0d6d5 | 543 | |
2a5c26c9 | 544 | return self._extract_set(info, token) |
92790f4e JMF |
545 | |
546 | ||
2a5c26c9 | 547 | class SoundcloudPagedPlaylistBaseIE(SoundcloudIE): |
836ef264 S |
548 | def _extract_playlist(self, base_url, playlist_id, playlist_title): |
549 | COMMON_QUERY = { | |
548c3957 | 550 | 'limit': 2000000000, |
836ef264 S |
551 | 'linked_partitioning': '1', |
552 | } | |
553 | ||
554 | query = COMMON_QUERY.copy() | |
555 | query['offset'] = 0 | |
556 | ||
548c3957 | 557 | next_href = base_url |
836ef264 S |
558 | |
559 | entries = [] | |
560 | for i in itertools.count(): | |
561 | response = self._download_json( | |
548c3957 RA |
562 | next_href, playlist_id, |
563 | 'Downloading track page %s' % (i + 1), query=query) | |
836ef264 S |
564 | |
565 | collection = response['collection'] | |
3ef2da2d S |
566 | |
567 | if not isinstance(collection, list): | |
568 | collection = [] | |
569 | ||
570 | # Empty collection may be returned, in this case we proceed | |
571 | # straight to next_href | |
836ef264 | 572 | |
7c5307f4 S |
573 | def resolve_entry(candidates): |
574 | for cand in candidates: | |
575 | if not isinstance(cand, dict): | |
576 | continue | |
577 | permalink_url = url_or_none(cand.get('permalink_url')) | |
578 | if not permalink_url: | |
579 | continue | |
580 | return self.url_result( | |
581 | permalink_url, | |
548c3957 RA |
582 | SoundcloudIE.ie_key() if SoundcloudIE.suitable(permalink_url) else None, |
583 | str_or_none(cand.get('id')), cand.get('title')) | |
836ef264 S |
584 | |
585 | for e in collection: | |
7c5307f4 S |
586 | entry = resolve_entry((e, e.get('track'), e.get('playlist'))) |
587 | if entry: | |
588 | entries.append(entry) | |
836ef264 S |
589 | |
590 | next_href = response.get('next_href') | |
591 | if not next_href: | |
592 | break | |
593 | ||
548c3957 RA |
594 | next_href = response['next_href'] |
595 | parsed_next_href = compat_urlparse.urlparse(next_href) | |
596 | query = compat_urlparse.parse_qs(parsed_next_href.query) | |
597 | query.update(COMMON_QUERY) | |
836ef264 S |
598 | |
599 | return { | |
600 | '_type': 'playlist', | |
601 | 'id': playlist_id, | |
602 | 'title': playlist_title, | |
603 | 'entries': entries, | |
604 | } | |
605 | ||
606 | ||
607 | class SoundcloudUserIE(SoundcloudPagedPlaylistBaseIE): | |
16a08978 S |
608 | _VALID_URL = r'''(?x) |
609 | https?:// | |
610 | (?:(?:www|m)\.)?soundcloud\.com/ | |
611 | (?P<user>[^/]+) | |
612 | (?:/ | |
3ef2da2d | 613 | (?P<rsrc>tracks|albums|sets|reposts|likes|spotlight) |
16a08978 S |
614 | )? |
615 | /?(?:[?#].*)?$ | |
616 | ''' | |
fbcd7b5f | 617 | IE_NAME = 'soundcloud:user' |
22a6f150 | 618 | _TESTS = [{ |
b6423e6c | 619 | 'url': 'https://soundcloud.com/soft-cell-official', |
22a6f150 | 620 | 'info_dict': { |
b6423e6c S |
621 | 'id': '207965082', |
622 | 'title': 'Soft Cell (All)', | |
22a6f150 | 623 | }, |
b6423e6c | 624 | 'playlist_mincount': 28, |
22a6f150 | 625 | }, { |
b6423e6c | 626 | 'url': 'https://soundcloud.com/soft-cell-official/tracks', |
22a6f150 | 627 | 'info_dict': { |
b6423e6c S |
628 | 'id': '207965082', |
629 | 'title': 'Soft Cell (Tracks)', | |
22a6f150 | 630 | }, |
b6423e6c | 631 | 'playlist_mincount': 27, |
03b9c944 | 632 | }, { |
b6423e6c S |
633 | 'url': 'https://soundcloud.com/soft-cell-official/albums', |
634 | 'info_dict': { | |
635 | 'id': '207965082', | |
636 | 'title': 'Soft Cell (Albums)', | |
637 | }, | |
638 | 'playlist_mincount': 1, | |
639 | }, { | |
640 | 'url': 'https://soundcloud.com/jcv246/sets', | |
80fb6d4a | 641 | 'info_dict': { |
b6423e6c | 642 | 'id': '12982173', |
548c3957 | 643 | 'title': 'Jordi / cv (Sets)', |
80fb6d4a | 644 | }, |
8e45e1cc | 645 | 'playlist_mincount': 2, |
80fb6d4a | 646 | }, { |
b6423e6c | 647 | 'url': 'https://soundcloud.com/jcv246/reposts', |
80fb6d4a | 648 | 'info_dict': { |
b6423e6c S |
649 | 'id': '12982173', |
650 | 'title': 'Jordi / cv (Reposts)', | |
80fb6d4a | 651 | }, |
b6423e6c | 652 | 'playlist_mincount': 6, |
80fb6d4a | 653 | }, { |
b6423e6c | 654 | 'url': 'https://soundcloud.com/clalberg/likes', |
80fb6d4a | 655 | 'info_dict': { |
b6423e6c S |
656 | 'id': '11817582', |
657 | 'title': 'clalberg (Likes)', | |
80fb6d4a | 658 | }, |
b6423e6c | 659 | 'playlist_mincount': 5, |
80fb6d4a S |
660 | }, { |
661 | 'url': 'https://soundcloud.com/grynpyret/spotlight', | |
662 | 'info_dict': { | |
663 | 'id': '7098329', | |
bf2dc9cc | 664 | 'title': 'Grynpyret (Spotlight)', |
80fb6d4a S |
665 | }, |
666 | 'playlist_mincount': 1, | |
22a6f150 | 667 | }] |
92790f4e | 668 | |
80fb6d4a | 669 | _BASE_URL_MAP = { |
548c3957 RA |
670 | 'all': 'stream/users/%s', |
671 | 'tracks': 'users/%s/tracks', | |
672 | 'albums': 'users/%s/albums', | |
673 | 'sets': 'users/%s/playlists', | |
674 | 'reposts': 'stream/users/%s/reposts', | |
675 | 'likes': 'users/%s/likes', | |
676 | 'spotlight': 'users/%s/spotlight', | |
80fb6d4a S |
677 | } |
678 | ||
92790f4e JMF |
679 | def _real_extract(self, url): |
680 | mobj = re.match(self._VALID_URL, url) | |
681 | uploader = mobj.group('user') | |
682 | ||
20991253 | 683 | user = self._download_json( |
548c3957 RA |
684 | self._resolv_url(self._BASE_URL + uploader), |
685 | uploader, 'Downloading user info') | |
80fb6d4a S |
686 | |
687 | resource = mobj.group('rsrc') or 'all' | |
80fb6d4a | 688 | |
836ef264 | 689 | return self._extract_playlist( |
548c3957 RA |
690 | self._API_V2_BASE + self._BASE_URL_MAP[resource] % user['id'], |
691 | str_or_none(user.get('id')), | |
692 | '%s (%s)' % (user['username'], resource.capitalize())) | |
97afd99a | 693 | |
92790f4e | 694 | |
836ef264 S |
695 | class SoundcloudTrackStationIE(SoundcloudPagedPlaylistBaseIE): |
696 | _VALID_URL = r'https?://(?:(?:www|m)\.)?soundcloud\.com/stations/track/[^/]+/(?P<id>[^/?#&]+)' | |
697 | IE_NAME = 'soundcloud:trackstation' | |
698 | _TESTS = [{ | |
699 | 'url': 'https://soundcloud.com/stations/track/officialsundial/your-text', | |
700 | 'info_dict': { | |
701 | 'id': '286017854', | |
548c3957 | 702 | 'title': 'Track station: your text', |
836ef264 S |
703 | }, |
704 | 'playlist_mincount': 47, | |
705 | }] | |
80fb6d4a | 706 | |
836ef264 S |
707 | def _real_extract(self, url): |
708 | track_name = self._match_id(url) | |
80fb6d4a | 709 | |
548c3957 | 710 | track = self._download_json(self._resolv_url(url), track_name) |
836ef264 | 711 | track_id = self._search_regex( |
548c3957 | 712 | r'soundcloud:track-stations:(\d+)', track['id'], 'track id') |
92790f4e | 713 | |
836ef264 | 714 | return self._extract_playlist( |
548c3957 RA |
715 | self._API_V2_BASE + 'stations/%s/tracks' % track['id'], |
716 | track_id, 'Track station: %s' % track['title']) | |
20991253 PH |
717 | |
718 | ||
7518a61d | 719 | class SoundcloudPlaylistIE(SoundcloudPlaylistBaseIE): |
548c3957 | 720 | _VALID_URL = r'https?://api(?:-v2)?\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$' |
20991253 | 721 | IE_NAME = 'soundcloud:playlist' |
46f74bcf | 722 | _TESTS = [{ |
f1c05100 | 723 | 'url': 'https://api.soundcloud.com/playlists/4110309', |
46f74bcf PH |
724 | 'info_dict': { |
725 | 'id': '4110309', | |
726 | 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]', | |
727 | 'description': 're:.*?TILT Brass - Bowery Poetry Club', | |
728 | }, | |
729 | 'playlist_count': 6, | |
730 | }] | |
20991253 PH |
731 | |
732 | def _real_extract(self, url): | |
733 | mobj = re.match(self._VALID_URL, url) | |
734 | playlist_id = mobj.group('id') | |
20991253 | 735 | |
3bed6217 | 736 | query = {} |
2f834e93 | 737 | token = mobj.group('token') |
2f834e93 | 738 | if token: |
548c3957 | 739 | query['secret_token'] = token |
2f834e93 | 740 | |
20991253 | 741 | data = self._download_json( |
548c3957 RA |
742 | self._API_V2_BASE + 'playlists/' + playlist_id, |
743 | playlist_id, 'Downloading playlist', query=query) | |
20991253 | 744 | |
2a5c26c9 | 745 | return self._extract_set(data, token) |
2abf7cab | 746 | |
747 | ||
748 | class SoundcloudSearchIE(SearchInfoExtractor, SoundcloudIE): | |
749 | IE_NAME = 'soundcloud:search' | |
750 | IE_DESC = 'Soundcloud search' | |
328a22e1 | 751 | _MAX_RESULTS = float('inf') |
2abf7cab | 752 | _TESTS = [{ |
753 | 'url': 'scsearch15:post-avant jazzcore', | |
754 | 'info_dict': { | |
755 | 'title': 'post-avant jazzcore', | |
756 | }, | |
757 | 'playlist_count': 15, | |
758 | }] | |
759 | ||
760 | _SEARCH_KEY = 'scsearch' | |
328a22e1 | 761 | _MAX_RESULTS_PER_PAGE = 200 |
762 | _DEFAULT_RESULTS_PER_PAGE = 50 | |
2abf7cab | 763 | |
764 | def _get_collection(self, endpoint, collection_id, **query): | |
a3372437 | 765 | limit = min( |
328a22e1 | 766 | query.get('limit', self._DEFAULT_RESULTS_PER_PAGE), |
767 | self._MAX_RESULTS_PER_PAGE) | |
548c3957 RA |
768 | query.update({ |
769 | 'limit': limit, | |
548c3957 RA |
770 | 'linked_partitioning': 1, |
771 | 'offset': 0, | |
772 | }) | |
773 | next_url = update_url_query(self._API_V2_BASE + endpoint, query) | |
2abf7cab | 774 | |
2abf7cab | 775 | collected_results = 0 |
776 | ||
f6c903e7 | 777 | for i in itertools.count(1): |
7e347275 | 778 | response = self._download_json( |
f6c903e7 | 779 | next_url, collection_id, 'Downloading page {0}'.format(i), |
7e347275 | 780 | 'Unable to download API page') |
2abf7cab | 781 | |
f6c903e7 S |
782 | collection = response.get('collection', []) |
783 | if not collection: | |
784 | break | |
2abf7cab | 785 | |
f6c903e7 | 786 | collection = list(filter(bool, collection)) |
2abf7cab | 787 | collected_results += len(collection) |
788 | ||
f6c903e7 S |
789 | for item in collection: |
790 | yield self.url_result(item['uri'], SoundcloudIE.ie_key()) | |
2abf7cab | 791 | |
f6c903e7 | 792 | if not collection or collected_results >= limit: |
2abf7cab | 793 | break |
794 | ||
7e347275 | 795 | next_url = response.get('next_href') |
f6c903e7 S |
796 | if not next_url: |
797 | break | |
2abf7cab | 798 | |
799 | def _get_n_results(self, query, n): | |
548c3957 | 800 | tracks = self._get_collection('search/tracks', query, limit=n, q=query) |
f6c903e7 | 801 | return self.playlist_result(tracks, playlist_title=query) |