Skip to content

Commit

Permalink
[soundcloud] automatically update client id on failing requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Remita Amine committed Dec 31, 2019
1 parent 0c15a56 commit 3bed621
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions youtube_dl/extractor/soundcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
SearchInfoExtractor
)
from ..compat import (
compat_HTTPError,
compat_kwargs,
compat_str,
compat_urlparse,
)
Expand Down Expand Up @@ -255,7 +257,6 @@ class SoundcloudIE(InfoExtractor):
_API_BASE = 'https://api.soundcloud.com/'
_API_V2_BASE = 'https://api-v2.soundcloud.com/'
_BASE_URL = 'https://soundcloud.com/'
_CLIENT_ID = 'YUKXoArFcqrlQn9tfNHvvyfnDISj04zk'
_IMAGE_REPL_RE = r'-([0-9a-z]+)\.jpg'

_ARTWORK_MAP = {
Expand All @@ -271,9 +272,39 @@ class SoundcloudIE(InfoExtractor):
'original': 0,
}

def _update_client_id(self):
webpage = self._download_webpage('https://soundcloud.com/', None)
for src in reversed(re.findall(r'<script[^>]+src="([^"]+)"', webpage)):
script = self._download_webpage(src, None, fatal=False)
if script:
client_id = self._search_regex(
r'client_id\s*:\s*"([0-9a-zA-Z]{32})"',
script, 'client id', default=None)
if client_id:
self._CLIENT_ID = client_id
self._downloader.cache.store('soundcloud', 'client_id', client_id)
return
raise ExtractorError('Unable to extract client id')

def _download_json(self, *args, **kwargs):
query = kwargs.get('query', {}).copy()
for _ in range(2):
query['client_id'] = self._CLIENT_ID
kwargs['query'] = query
try:
return super(SoundcloudIE, self)._download_json(*args, **compat_kwargs(kwargs))
except ExtractorError as e:
if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
self._update_client_id()
continue
raise

def _real_initialize(self):
self._CLIENT_ID = self._downloader.cache.load('soundcloud', 'client_id') or 'YUKXoArFcqrlQn9tfNHvvyfnDISj04zk'

@classmethod
def _resolv_url(cls, url):
return SoundcloudIE._API_V2_BASE + 'resolve?url=' + url + '&client_id=' + cls._CLIENT_ID
return SoundcloudIE._API_V2_BASE + 'resolve?url=' + url

def _extract_info_dict(self, info, full_title=None, secret_token=None, version=2):
track_id = compat_str(info['id'])
Expand Down Expand Up @@ -451,9 +482,7 @@ def _real_extract(self, url):

track_id = mobj.group('track_id')

query = {
'client_id': self._CLIENT_ID,
}
query = {}
if track_id:
info_json_url = self._API_V2_BASE + 'tracks/' + track_id
full_title = track_id
Expand Down Expand Up @@ -536,7 +565,6 @@ class SoundcloudPagedPlaylistBaseIE(SoundcloudPlaylistBaseIE):
def _extract_playlist(self, base_url, playlist_id, playlist_title):
COMMON_QUERY = {
'limit': 2000000000,
'client_id': self._CLIENT_ID,
'linked_partitioning': '1',
}

Expand Down Expand Up @@ -722,9 +750,7 @@ def _real_extract(self, url):
mobj = re.match(self._VALID_URL, url)
playlist_id = mobj.group('id')

query = {
'client_id': self._CLIENT_ID,
}
query = {}
token = mobj.group('token')
if token:
query['secret_token'] = token
Expand Down Expand Up @@ -761,7 +787,6 @@ def _get_collection(self, endpoint, collection_id, **query):
self._MAX_RESULTS_PER_PAGE)
query.update({
'limit': limit,
'client_id': self._CLIENT_ID,
'linked_partitioning': 1,
'offset': 0,
})
Expand Down

0 comments on commit 3bed621

Please sign in to comment.