2 from __future__
import unicode_literals
11 from ..compat
import (
30 class SoundcloudEmbedIE(InfoExtractor
):
31 _VALID_URL
= r
'https?://(?:w|player|p)\.soundcloud\.com/player/?.*?\burl=(?P<id>.+)'
33 # from https://www.soundi.fi/uutiset/ennakkokuuntelussa-timo-kaukolammen-station-to-station-to-station-julkaisua-juhlitaan-tanaan-g-livelabissa/
34 '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',
35 'only_matching': True,
39 def _extract_urls(webpage
):
40 return [m
.group('url') for m
in re
.finditer(
41 r
'<iframe[^>]+src=(["\'])(?P
<url
>(?
:https?
://)?
(?
:w\
.)?soundcloud\
.com
/player
.+?
)\
1',
44 def _real_extract(self, url):
45 query = compat_urlparse.parse_qs(
46 compat_urlparse.urlparse(url).query)
47 api_url = query['url
'][0]
48 secret_token = query.get('secret_token
')
50 api_url = update_url_query(api_url, {'secret_token': secret_token[0]})
51 return self.url_result(api_url)
54 class SoundcloudIE(InfoExtractor):
55 """Information extractor for soundcloud.com
56 To access the media, the uid of the song and a stream token
57 must be extracted from the page source and the script must make
58 a request to media.soundcloud.com/crossdomain.xml. Then
59 the media can be grabbed by requesting from an url composed
60 of the stream token and uid
63 _VALID_URL = r'''(?x)^(?:https?://)?
64 (?:(?:(?:www\.|m\.)?soundcloud\.com/
66 (?P<uploader>[\w\d-]+)/
67 (?!(?:tracks|albums|sets(?:/.+?)?|reposts|likes|spotlight)/?(?:$|[?#]))
69 (?P<token>[^?]+?)?(?:[?].*)?$)
70 |(?:api(?:-v2)?\.soundcloud\.com/tracks/(?P<track_id>\d+)
71 (?:/?\?secret_token=(?P<secret_token>[^&]+))?)
74 IE_NAME = 'soundcloud
'
77 'url
': 'http
://soundcloud
.com
/ethmusic
/lostin
-powers
-she
-so
-heavy
',
78 'md5
': 'ebef0a451b909710ed1d7787dddbf0d7
',
82 'title
': 'Lostin Powers
- She so
Heavy (SneakPreview
) Adrian Ackers Blueprint
1',
83 '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
',
84 'uploader
': 'E
.T
. ExTerrestrial Music
',
85 'uploader_id
': '1571244',
86 'timestamp
': 1349920598,
87 'upload_date
': '20121011',
89 'license
': 'all
-rights
-reserved
',
98 'url
': 'https
://soundcloud
.com
/the
-concept
-band
/goldrushed
-mastered?
in=the
-concept
-band
/sets
/the
-royal
-concept
-ep
',
102 'title
': 'Goldrushed
',
103 'description
': 'From Stockholm Sweden
\r\nPovel
/ Magnus
/ Filip
/ David
\r\nwww
.theroyalconcept
.com
',
104 'uploader
': 'The Royal Concept
',
105 'uploader_id
': '9615865',
106 'timestamp
': 1337635207,
107 'upload_date
': '20120521',
109 'license
': 'all
-rights
-reserved
',
112 'comment_count
': int,
117 'skip_download
': True,
123 'url
': 'https
://soundcloud
.com
/jaimemf
/youtube
-dl
-test
-video
-a
-y
-baw
/s
-8Pjrp
',
124 'md5
': 'aa0dd32bfea9b0c5ef4f02aacd080604
',
128 'title
': 'Youtube
- Dl Test Video
\'\' Ä↭
',
129 'description
': 'test chars
: \"\'/\\ä↭
',
130 'uploader
': 'jaimeMF
',
131 'uploader_id
': '69767071',
132 'timestamp
': 1386604920,
133 'upload_date
': '20131209',
135 'license
': 'all
-rights
-reserved
',
138 'comment_count
': int,
142 # private link (alt format)
144 'url
': 'https
://api
.soundcloud
.com
/tracks
/123998367?secret_token
=s
-8Pjrp
',
145 'md5
': 'aa0dd32bfea9b0c5ef4f02aacd080604
',
149 'title
': 'Youtube
- Dl Test Video
\'\' Ä↭
',
150 'description
': 'test chars
: \"\'/\\ä↭
',
151 'uploader
': 'jaimeMF
',
152 'uploader_id
': '69767071',
153 'timestamp
': 1386604920,
154 'upload_date
': '20131209',
156 'license
': 'all
-rights
-reserved
',
159 'comment_count
': int,
165 'url
': 'https
://soundcloud
.com
/oddsamples
/bus
-brakes
',
166 'md5
': '7624f2351f8a3b2e7cd51522496e7631
',
170 'title
': 'Bus Brakes
',
171 'description
': 'md5
:0053ca6396e8d2fd7b7e1595ef12ab66
',
172 'uploader
': 'oddsamples
',
173 'uploader_id
': '73680509',
174 'timestamp
': 1389232924,
175 'upload_date
': '20140109',
177 'license
': 'cc
-by
-sa
',
180 'comment_count
': int,
184 # private link, downloadable format
186 'url
': 'https
://soundcloud
.com
/oriuplift
/uponly
-238-no
-talking
-wav
/s
-AyZUd
',
187 'md5
': '64a60b16e617d41d0bef032b7f55441e
',
191 'title
': 'Uplifting Only
238 [No Talking
] (incl
. Alex Feed Guestmix
) (Aug
31, 2017) [wav
]',
192 'description
': 'md5
:fa20ee0fca76a3d6df8c7e57f3715366
',
193 'uploader
': 'Ori Uplift Music
',
194 'uploader_id
': '12563093',
195 'timestamp
': 1504206263,
196 'upload_date
': '20170831',
197 'duration
': 7449.096,
198 'license
': 'all
-rights
-reserved
',
201 'comment_count
': int,
205 # no album art, use avatar pic for thumbnail
207 'url
': 'https
://soundcloud
.com
/garyvee
/sideways
-prod
-mad
-real
',
208 'md5
': '59c7872bc44e5d99b7211891664760c2
',
212 'title
': 'Sideways (Prod
. Mad Real
)',
213 'description
': 'md5
:d41d8cd98f00b204e9800998ecf8427e
',
214 'uploader
': 'garyvee
',
215 'uploader_id
': '2366352',
216 'timestamp
': 1488152409,
217 'upload_date
': '20170226',
219 'thumbnail
': r're
:https?
://.*\
.jpg
',
220 'license
': 'all
-rights
-reserved
',
223 'comment_count
': int,
227 'skip_download
': True,
230 # not available via api.soundcloud.com/i1/tracks/id/streams
232 'url
': 'https
://soundcloud
.com
/giovannisarani
/mezzo
-valzer
',
233 'md5
': 'e22aecd2bc88e0e4e432d7dcc0a1abf7
',
237 'title
': 'Mezzo Valzer
',
238 'description
': 'md5
:4138d582f81866a530317bae316e8b61
',
239 'uploader
': 'Giovanni Sarani
',
240 'uploader_id
': '3352531',
241 'timestamp
': 1551394171,
242 'upload_date
': '20190228',
244 'thumbnail
': r're
:https?
://.*\
.jpg
',
245 'license
': 'all
-rights
-reserved
',
248 'comment_count
': int,
251 'expected_warnings
': ['Unable to download JSON metadata
'],
255 _API_BASE = 'https
://api
.soundcloud
.com
/'
256 _API_V2_BASE = 'https
://api
-v2
.soundcloud
.com
/'
257 _BASE_URL = 'https
://soundcloud
.com
/'
258 _CLIENT_ID = 'UW9ajvMgVdMMW3cdeBi8lPfN6dvOVGji
'
259 _IMAGE_REPL_RE = r'-([0-9a
-z
]+)\
.jpg
'
275 def _resolv_url(cls, url):
276 return SoundcloudIE._API_V2_BASE + 'resolve?url
=' + url + '&client_id
=' + cls._CLIENT_ID
278 def _extract_info_dict(self, info, full_title=None, secret_token=None, version=2):
279 track_id = compat_str(info['id'])
280 title = info['title
']
281 track_base_url = self._API_BASE + 'tracks
/%s' % track_id
285 query = {'client_id': self._CLIENT_ID}
287 query['secret_token
'] = secret_token
289 if info.get('downloadable
') and info.get('has_downloads_left
'):
290 format_url = update_url_query(
291 info.get('download_url
') or track_base_url + '/download
', query)
292 format_urls.add(format_url)
294 v1_info = self._download_json(
295 track_base_url, track_id, query=query, fatal=False) or {}
299 'format_id
': 'download
',
300 'ext
': v1_info.get('original_format
') or 'mp3
',
301 'filesize
': int_or_none(v1_info.get('original_content_size
')),
306 def invalid_url(url):
307 return not url or url in format_urls or re.search(r'/(?
:preview|playlist
)/0/30/', url)
309 def add_format(f, protocol):
310 mobj = re.search(r'\
.(?P
<abr
>\d
+)\
.(?P
<ext
>[0-9a
-z
]{3,4}
)(?
=[/?
])', stream_url)
312 for k, v in mobj.groupdict().items():
317 format_id_list.append(protocol)
318 for k in ('ext
', 'abr
'):
321 format_id_list.append(v)
326 'format_id
': '_
'.join(format_id_list),
327 'protocol
': 'm3u8_native
' if protocol == 'hls
' else 'http
',
332 transcodings = try_get(
333 info, lambda x: x['media
']['transcodings
'], list) or []
334 for t in transcodings:
335 if not isinstance(t, dict):
337 format_url = url_or_none(t.get('url
'))
338 if not format_url or t.get('snipped
') or '/preview
/' in format_url:
340 stream = self._download_json(
341 format_url, track_id, query=query, fatal=False)
342 if not isinstance(stream, dict):
344 stream_url = url_or_none(stream.get('url
'))
345 if invalid_url(stream_url):
347 format_urls.add(stream_url)
348 stream_format = t.get('format
') or {}
349 protocol = stream_format.get('protocol
')
350 if protocol != 'hls
' and '/hls
' in format_url:
353 preset = str_or_none(t.get('preset
'))
355 ext = preset.split('_
')[0]
356 if ext not in KNOWN_EXTENSIONS:
357 ext = mimetype2ext(stream_format.get('mime_type
'))
361 }, 'http
' if protocol == 'progressive
' else protocol)
364 # Old API, does not work for some tracks (e.g.
365 # https://soundcloud.com/giovannisarani/mezzo-valzer)
366 # and might serve preview URLs (e.g.
367 # http://www.soundcloud.com/snbrn/ele)
368 format_dict = self._download_json(
369 track_base_url + '/streams
', track_id,
370 'Downloading track url
', query=query, fatal=False) or {}
372 for key, stream_url in format_dict.items():
373 if invalid_url(stream_url):
375 format_urls.add(stream_url)
376 mobj = re.search(r'(http|hls
)_([^_
]+)_(\d
+)_url
', key)
378 protocol, ext, abr = mobj.groups()
386 # We fallback to the stream_url in the original info, this
387 # cannot be always used, sometimes it can give an HTTP 404 error
388 urlh = self._request_webpage(
389 HEADRequest(info.get('stream_url
') or track_base_url + '/stream
'),
390 track_id, query=query, fatal=False)
392 stream_url = urlh.geturl()
393 if not invalid_url(stream_url):
394 add_format({'url': stream_url}, 'http
')
399 self._sort_formats(formats)
401 user = info.get('user
') or {}
404 artwork_url = info.get('artwork_url
')
405 thumbnail = artwork_url or user.get('avatar_url
')
406 if isinstance(thumbnail, compat_str):
407 if re.search(self._IMAGE_REPL_RE, thumbnail):
408 for image_id, size in self._ARTWORK_MAP.items():
411 'url
': re.sub(self._IMAGE_REPL_RE, '-%s.jpg
' % image_id, thumbnail),
413 if image_id == 'tiny
' and not artwork_url:
415 elif image_id == 'original
':
424 thumbnails = [{'url': thumbnail}]
426 def extract_count(key):
427 return int_or_none(info.get('%s_count
' % key))
431 'uploader
': user.get('username
'),
432 'uploader_id
': str_or_none(user.get('id')) or user.get('permalink
'),
433 'uploader_url
': user.get('permalink_url
'),
434 'timestamp
': unified_timestamp(info.get('created_at
')),
436 'description
': info.get('description
'),
437 'thumbnails
': thumbnails,
438 'duration
': float_or_none(info.get('duration
'), 1000),
439 'webpage_url
': info.get('permalink_url
'),
440 'license
': info.get('license
'),
441 'view_count
': extract_count('playback
'),
442 'like_count
': extract_count('favoritings
') or extract_count('likes
'),
443 'comment_count
': extract_count('comment
'),
444 'repost_count
': extract_count('reposts
'),
445 'genre
': info.get('genre
'),
449 def _real_extract(self, url):
450 mobj = re.match(self._VALID_URL, url)
452 track_id = mobj.group('track_id
')
455 'client_id
': self._CLIENT_ID,
458 info_json_url = self._API_V2_BASE + 'tracks
/' + track_id
459 full_title = track_id
460 token = mobj.group('secret_token
')
462 query['secret_token
'] = token
464 full_title = resolve_title = '%s/%s' % mobj.group('uploader
', 'title
')
465 token = mobj.group('token
')
467 resolve_title += '/%s' % token
468 info_json_url = self._resolv_url(self._BASE_URL + resolve_title)
471 info = self._download_json(
472 info_json_url, full_title, 'Downloading info JSON
', query=query, fatal=False)
474 info = self._download_json(
475 info_json_url.replace(self._API_V2_BASE, self._API_BASE),
476 full_title, 'Downloading info JSON
', query=query)
479 return self._extract_info_dict(info, full_title, token, version)
482 class SoundcloudPlaylistBaseIE(SoundcloudIE):
483 def _extract_track_entries(self, tracks, token=None):
486 track_id = str_or_none(track.get('id'))
487 url = track.get('permalink_url
')
491 url = self._API_V2_BASE + 'tracks
/' + track_id
493 url += '?secret_token
=' + token
494 entries.append(self.url_result(
495 url, SoundcloudIE.ie_key(), track_id))
499 class SoundcloudSetIE(SoundcloudPlaylistBaseIE):
500 _VALID_URL = r'https?
://(?
:(?
:www|m
)\
.)?soundcloud\
.com
/(?P
<uploader
>[\w\d
-]+)/sets
/(?P
<slug_title
>[\w\d
-]+)(?
:/(?P
<token
>[^?
/]+))?
'
501 IE_NAME = 'soundcloud
:set'
503 'url
': 'https
://soundcloud
.com
/the
-concept
-band
/sets
/the
-royal
-concept
-ep
',
506 'title
': 'The Royal Concept EP
',
508 'playlist_mincount
': 5,
510 'url
': 'https
://soundcloud
.com
/the
-concept
-band
/sets
/the
-royal
-concept
-ep
/token
',
511 'only_matching
': True,
514 def _real_extract(self, url):
515 mobj = re.match(self._VALID_URL, url)
517 full_title = '%s/sets
/%s' % mobj.group('uploader
', 'slug_title
')
518 token = mobj.group('token
')
520 full_title += '/' + token
522 info = self._download_json(self._resolv_url(
523 self._BASE_URL + full_title), full_title)
526 msgs = (compat_str(err['error_message
']) for err in info['errors
'])
527 raise ExtractorError('unable to download video webpage
: %s' % ','.join(msgs))
529 entries = self._extract_track_entries(info['tracks
'], token)
531 return self.playlist_result(
532 entries, str_or_none(info.get('id')), info.get('title
'))
535 class SoundcloudPagedPlaylistBaseIE(SoundcloudPlaylistBaseIE):
536 def _extract_playlist(self, base_url, playlist_id, playlist_title):
539 'client_id
': self._CLIENT_ID,
540 'linked_partitioning
': '1',
543 query = COMMON_QUERY.copy()
549 for i in itertools.count():
550 response = self._download_json(
551 next_href, playlist_id,
552 'Downloading track page
%s' % (i + 1), query=query)
554 collection = response['collection
']
556 if not isinstance(collection, list):
559 # Empty collection may be returned, in this case we proceed
560 # straight to next_href
562 def resolve_entry(candidates):
563 for cand in candidates:
564 if not isinstance(cand, dict):
566 permalink_url = url_or_none(cand.get('permalink_url
'))
567 if not permalink_url:
569 return self.url_result(
571 SoundcloudIE.ie_key() if SoundcloudIE.suitable(permalink_url) else None,
572 str_or_none(cand.get('id')), cand.get('title
'))
575 entry = resolve_entry((e, e.get('track
'), e.get('playlist
')))
577 entries.append(entry)
579 next_href = response.get('next_href
')
583 next_href = response['next_href
']
584 parsed_next_href = compat_urlparse.urlparse(next_href)
585 query = compat_urlparse.parse_qs(parsed_next_href.query)
586 query.update(COMMON_QUERY)
591 'title
': playlist_title,
596 class SoundcloudUserIE(SoundcloudPagedPlaylistBaseIE):
597 _VALID_URL = r'''(?x)
599 (?:(?:www|m)\.)?soundcloud\.com/
602 (?P<rsrc>tracks|albums|sets|reposts|likes|spotlight)
606 IE_NAME = 'soundcloud
:user
'
608 'url
': 'https
://soundcloud
.com
/soft
-cell
-official
',
611 'title
': 'Soft
Cell (All
)',
613 'playlist_mincount
': 28,
615 'url
': 'https
://soundcloud
.com
/soft
-cell
-official
/tracks
',
618 'title
': 'Soft
Cell (Tracks
)',
620 'playlist_mincount
': 27,
622 'url
': 'https
://soundcloud
.com
/soft
-cell
-official
/albums
',
625 'title
': 'Soft
Cell (Albums
)',
627 'playlist_mincount
': 1,
629 'url
': 'https
://soundcloud
.com
/jcv246
/sets
',
632 'title
': 'Jordi
/ cv (Sets
)',
634 'playlist_mincount
': 2,
636 'url
': 'https
://soundcloud
.com
/jcv246
/reposts
',
639 'title
': 'Jordi
/ cv (Reposts
)',
641 'playlist_mincount
': 6,
643 'url
': 'https
://soundcloud
.com
/clalberg
/likes
',
646 'title
': 'clalberg (Likes
)',
648 'playlist_mincount
': 5,
650 'url
': 'https
://soundcloud
.com
/grynpyret
/spotlight
',
653 'title
': 'Grynpyret (Spotlight
)',
655 'playlist_mincount
': 1,
659 'all
': 'stream
/users
/%s',
660 'tracks
': 'users
/%s/tracks
',
661 'albums
': 'users
/%s/albums
',
662 'sets
': 'users
/%s/playlists
',
663 'reposts
': 'stream
/users
/%s/reposts
',
664 'likes
': 'users
/%s/likes
',
665 'spotlight
': 'users
/%s/spotlight
',
668 def _real_extract(self, url):
669 mobj = re.match(self._VALID_URL, url)
670 uploader = mobj.group('user
')
672 user = self._download_json(
673 self._resolv_url(self._BASE_URL + uploader),
674 uploader, 'Downloading user info
')
676 resource = mobj.group('rsrc
') or 'all
'
678 return self._extract_playlist(
679 self._API_V2_BASE + self._BASE_URL_MAP[resource] % user['id'],
680 str_or_none(user.get('id')),
681 '%s (%s)' % (user['username
'], resource.capitalize()))
684 class SoundcloudTrackStationIE(SoundcloudPagedPlaylistBaseIE):
685 _VALID_URL = r'https?
://(?
:(?
:www|m
)\
.)?soundcloud\
.com
/stations
/track
/[^
/]+/(?P
<id>[^
/?
#&]+)'
686 IE_NAME
= 'soundcloud:trackstation'
688 'url': 'https://soundcloud.com/stations/track/officialsundial/your-text',
691 'title': 'Track station: your text',
693 'playlist_mincount': 47,
696 def _real_extract(self
, url
):
697 track_name
= self
._match
_id
(url
)
699 track
= self
._download
_json
(self
._resolv
_url
(url
), track_name
)
700 track_id
= self
._search
_regex
(
701 r
'soundcloud:track-stations:(\d+)', track
['id'], 'track id')
703 return self
._extract
_playlist
(
704 self
._API
_V
2_BASE
+ 'stations/%s/tracks' % track
['id'],
705 track_id
, 'Track station: %s' % track
['title'])
708 class SoundcloudPlaylistIE(SoundcloudPlaylistBaseIE
):
709 _VALID_URL
= r
'https?://api(?:-v2)?\.soundcloud\.com/playlists/(?P<id>[0-9]+)(?:/?\?secret_token=(?P<token>[^&]+?))?$'
710 IE_NAME
= 'soundcloud:playlist'
712 'url': 'https://api.soundcloud.com/playlists/4110309',
715 'title': 'TILT Brass - Bowery Poetry Club, August \'03 [Non-Site SCR 02]',
716 'description': 're:.*?TILT Brass - Bowery Poetry Club',
721 def _real_extract(self
, url
):
722 mobj
= re
.match(self
._VALID
_URL
, url
)
723 playlist_id
= mobj
.group('id')
726 'client_id': self
._CLIENT
_ID
,
728 token
= mobj
.group('token')
730 query
['secret_token'] = token
732 data
= self
._download
_json
(
733 self
._API
_V
2_BASE
+ 'playlists/' + playlist_id
,
734 playlist_id
, 'Downloading playlist', query
=query
)
736 entries
= self
._extract
_track
_entries
(data
['tracks'], token
)
738 return self
.playlist_result(
739 entries
, playlist_id
, data
.get('title'), data
.get('description'))
742 class SoundcloudSearchIE(SearchInfoExtractor
, SoundcloudIE
):
743 IE_NAME
= 'soundcloud:search'
744 IE_DESC
= 'Soundcloud search'
745 _MAX_RESULTS
= float('inf')
747 'url': 'scsearch15:post-avant jazzcore',
749 'title': 'post-avant jazzcore',
751 'playlist_count': 15,
754 _SEARCH_KEY
= 'scsearch'
755 _MAX_RESULTS_PER_PAGE
= 200
756 _DEFAULT_RESULTS_PER_PAGE
= 50
758 def _get_collection(self
, endpoint
, collection_id
, **query
):
760 query
.get('limit', self
._DEFAULT
_RESULTS
_PER
_PAGE
),
761 self
._MAX
_RESULTS
_PER
_PAGE
)
764 'client_id': self
._CLIENT
_ID
,
765 'linked_partitioning': 1,
768 next_url
= update_url_query(self
._API
_V
2_BASE
+ endpoint
, query
)
770 collected_results
= 0
772 for i
in itertools
.count(1):
773 response
= self
._download
_json
(
774 next_url
, collection_id
, 'Downloading page {0}'.format(i
),
775 'Unable to download API page')
777 collection
= response
.get('collection', [])
781 collection
= list(filter(bool, collection
))
782 collected_results
+= len(collection
)
784 for item
in collection
:
785 yield self
.url_result(item
['uri'], SoundcloudIE
.ie_key())
787 if not collection
or collected_results
>= limit
:
790 next_url
= response
.get('next_href')
794 def _get_n_results(self
, query
, n
):
795 tracks
= self
._get
_collection
('search/tracks', query
, limit
=n
, q
=query
)
796 return self
.playlist_result(tracks
, playlist_title
=query
)