twitter: cache token, get new one & retry on error

This commit is contained in:
dumbmoron 2024-01-04 16:35:58 +00:00
parent 02b67a3145
commit 7b41dfef9e
No known key found for this signature in database
GPG key ID: C59997C76C6A8E5F

View file

@ -31,14 +31,20 @@ const commonHeaders = {
"accept-language": "en"
}
const getGuestToken = async () => {
let _cachedToken
const getGuestToken = async (forceReload = false) => {
if (_cachedToken && !forceReload) {
return _cachedToken;
}
const tokenResponse = await fetch(
'https://api.twitter.com/1.1/guest/activate.json',
{ method: 'POST', headers: commonHeaders }
).then(r => r.status === 200 && r.json()).catch(() => {})
if (tokenResponse?.guest_token)
return tokenResponse.guest_token
if (tokenResponse?.guest_token) {
return _cachedToken = tokenResponse.guest_token
}
}
const requestTweet = (tweetId, token) => {
@ -69,8 +75,15 @@ export default async function({ id }) {
let guestToken = await getGuestToken();
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"}}}}
if (tweet?.data?.tweetResult?.result?.__typename !== "Tweet") {
return { error: 'ErrorTweetUnavailable' }