mirror of
https://github.com/wukko/cobalt.git
synced 2025-01-12 20:25:06 +01:00
twitter: cache token, get new one & retry on error
This commit is contained in:
parent
02b67a3145
commit
7b41dfef9e
1 changed files with 18 additions and 5 deletions
|
@ -31,14 +31,20 @@ const commonHeaders = {
|
||||||
"accept-language": "en"
|
"accept-language": "en"
|
||||||
}
|
}
|
||||||
|
|
||||||
const getGuestToken = async () => {
|
let _cachedToken
|
||||||
|
const getGuestToken = async (forceReload = false) => {
|
||||||
|
if (_cachedToken && !forceReload) {
|
||||||
|
return _cachedToken;
|
||||||
|
}
|
||||||
|
|
||||||
const tokenResponse = await fetch(
|
const tokenResponse = await fetch(
|
||||||
'https://api.twitter.com/1.1/guest/activate.json',
|
'https://api.twitter.com/1.1/guest/activate.json',
|
||||||
{ method: 'POST', headers: commonHeaders }
|
{ method: 'POST', headers: commonHeaders }
|
||||||
).then(r => r.status === 200 && r.json()).catch(() => {})
|
).then(r => r.status === 200 && r.json()).catch(() => {})
|
||||||
|
|
||||||
if (tokenResponse?.guest_token)
|
if (tokenResponse?.guest_token) {
|
||||||
return tokenResponse.guest_token
|
return _cachedToken = tokenResponse.guest_token
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const requestTweet = (tweetId, token) => {
|
const requestTweet = (tweetId, token) => {
|
||||||
|
@ -69,8 +75,15 @@ export default async function({ id }) {
|
||||||
let guestToken = await getGuestToken();
|
let guestToken = await getGuestToken();
|
||||||
if (!guestToken) return { error: 'ErrorCouldntFetch' };
|
if (!guestToken) return { error: 'ErrorCouldntFetch' };
|
||||||
|
|
||||||
const tweet = await requestTweet(id, guestToken).then(t => t.json());
|
let tweet = await requestTweet(id, guestToken);
|
||||||
|
|
||||||
|
if ([403, 429].includes(tweet.status)) { // get new token & retry
|
||||||
|
guestToken = await getGuestToken(true);
|
||||||
|
tweet = await requestTweet(id, guestToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
tweet = await tweet.json();
|
||||||
|
|
||||||
// {"data":{"tweetResult":{"result":{"__typename":"TweetUnavailable","reason":"Protected"}}}}
|
// {"data":{"tweetResult":{"result":{"__typename":"TweetUnavailable","reason":"Protected"}}}}
|
||||||
if (tweet?.data?.tweetResult?.result?.__typename !== "Tweet") {
|
if (tweet?.data?.tweetResult?.result?.__typename !== "Tweet") {
|
||||||
return { error: 'ErrorTweetUnavailable' }
|
return { error: 'ErrorTweetUnavailable' }
|
||||||
|
|
Loading…
Reference in a new issue