1
0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-11-28 01:20:57 +01:00

[keep2share] share access token across calls to _real_extract

This commit is contained in:
schn0sch 2021-04-24 14:03:17 +02:00
parent 5f3cc3bbea
commit 14bf10c648

View File

@ -30,13 +30,13 @@ class Keep2ShareIE(InfoExtractor):
}, },
}] }]
def _real_extract(self, url): def _get_app_secret(self, video_id):
video_id = self._match_id(url) """ retrieve REACT_APP_API_CLIENT_SECRET """
# TODO share app_secret across calls to _real_extract if getattr(self, '_app_secret', None) is not None:
app_secret = None return self._app_secret
# retrieve app_secret url = 'https://k2s.cc/file/' + video_id
webpage = self._download_webpage(url, video_id) webpage = self._download_webpage(url, video_id)
scripts = re.finditer(r'<script\s+src="(?P<src>/static/[^"]*)"', webpage) scripts = re.finditer(r'<script\s+src="(?P<src>/static/[^"]*)"', webpage)
for mobj in scripts: for mobj in scripts:
@ -47,26 +47,35 @@ class Keep2ShareIE(InfoExtractor):
'string': r'"(?:[^"]|\\")*"' + '|' + r"'(?:[^']|\\')*'", 'string': r'"(?:[^"]|\\")*"' + '|' + r"'(?:[^']|\\')*'",
}, script, 'app secret', group='secret', default=None) }, script, 'app secret', group='secret', default=None)
if secret is not None: if secret is not None:
app_secret = self._parse_json(secret, video_id, transform_source=js_to_json) self._app_secret = self._parse_json(secret, video_id,
break transform_source=js_to_json)
else: return self._app_secret
raise RegexNotFoundError('Unable to extract app secret')
raise RegexNotFoundError('Unable to extract app secret')
def _get_access_token(self, video_id):
""" retrieve access_token """
if getattr(self, '_access_token', None) is not None:
return self._access_token
# retrieve access_token
# TODO share access_token across calls fo _real_extract
data = { data = {
'grant_type': 'client_credentials', 'grant_type': 'client_credentials',
'client_id': 'k2s_web_app', 'client_id': 'k2s_web_app',
'client_secret': app_secret, 'client_secret': self._get_app_secret(video_id),
} }
data = json.dumps(data, separators=(',', ':')).encode('utf-8') data = json.dumps(data, separators=(',', ':')).encode('utf-8')
headers = {'Content-Type': 'application/json'} headers = {'Content-Type': 'application/json'}
tokens = self._download_json('https://api.k2s.cc/v1/auth/token', tokens = self._download_json('https://api.k2s.cc/v1/auth/token',
video_id, data=data, headers=headers) video_id, data=data, headers=headers)
access_token = tokens['access_token'] self._access_token = tokens['access_token']
# retrieve API page return self._access_token
headers = {'Cookie': 'accessToken=' + access_token}
def _real_extract(self, url):
video_id = self._match_id(url)
headers = {'Cookie': 'accessToken=' + self._get_access_token(url)}
info = self._download_json('https://api.k2s.cc/v1/files/' + video_id, info = self._download_json('https://api.k2s.cc/v1/files/' + video_id,
video_id, headers=headers) video_id, headers=headers)