Fix some recent api woes (still not complete)

This commit is contained in:
dangered wolf 2023-08-14 21:01:13 -04:00
parent 3b6759566b
commit d6c1b6a82a
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
5 changed files with 15 additions and 10 deletions

View file

@ -1,4 +1,3 @@
{ {
"FixTweet": "https://github.com/FixTweet/FixTweet", "FixTweet - Recovering from API woes": "https://github.com/FixTweet/FixTweet/issues/333"
"FixTweet - Embed videos, polls & more": "https://github.com/FixTweet/FixTweet"
} }

View file

@ -199,6 +199,8 @@ export const statusAPI = async (
res = await fetchConversation(status, event, true); res = await fetchConversation(status, event, true);
} }
console.log(JSON.stringify(tweet))
if (tweet.__typename === 'TweetUnavailable') { if (tweet.__typename === 'TweetUnavailable') {
if (tweet.reason === 'Protected') { if (tweet.reason === 'Protected') {
writeDataPoint(event, language, wasMediaBlockedNSFW, 'PRIVATE_TWEET', flags); writeDataPoint(event, language, wasMediaBlockedNSFW, 'PRIVATE_TWEET', flags);

View file

@ -20,7 +20,7 @@ export const Constants = {
GUEST_TOKEN_MAX_AGE: 3 * 60 * 60, GUEST_TOKEN_MAX_AGE: 3 * 60 * 60,
/* Twitter Web App actually uses Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA /* Twitter Web App actually uses Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA
instead, but accounts marked as 18+ wouldn't show up then */ instead, but accounts marked as 18+ wouldn't show up then */
GUEST_BEARER_TOKEN: `Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw`, GUEST_BEARER_TOKEN: `Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA`,
GUEST_FETCH_PARAMETERS: [ GUEST_FETCH_PARAMETERS: [
'cards_platform=Web-12', 'cards_platform=Web-12',
'include_cards=1', 'include_cards=1',

View file

@ -59,6 +59,7 @@ export const handleStatus = async (
case 404: case 404:
return returnError(Strings.ERROR_TWEET_NOT_FOUND); return returnError(Strings.ERROR_TWEET_NOT_FOUND);
case 500: case 500:
console.log(api);
return returnError(Strings.ERROR_API_FAIL); return returnError(Strings.ERROR_API_FAIL);
} }

View file

@ -2,7 +2,13 @@ import { Constants } from './constants';
import { generateUserAgent } from './helpers/useragent'; import { generateUserAgent } from './helpers/useragent';
import { isGraphQLTweet } from './utils/graphql'; import { isGraphQLTweet } from './utils/graphql';
const API_ATTEMPTS = 16; const API_ATTEMPTS = 3;
function generateCSRFToken() {
const randomBytes = new Uint8Array(160/2);
crypto.getRandomValues(randomBytes);
return Array.from(randomBytes, byte => byte.toString(16).padStart(2, '0')).join('');
}
export const twitterFetch = async ( export const twitterFetch = async (
url: string, url: string,
@ -52,12 +58,9 @@ export const twitterFetch = async (
const cache = caches.default; const cache = caches.default;
while (apiAttempts < API_ATTEMPTS) { while (apiAttempts < API_ATTEMPTS) {
const csrfToken = crypto /* Generate a random CSRF token, Twitter just cares that header and cookie match,
.randomUUID() REST can use shorter csrf tokens (32 bytes) but graphql prefers 160 bytes */
.replace( const csrfToken = generateCSRFToken();
/-/g,
''
); /* Generate a random CSRF token, this doesn't matter, Twitter just cares that header and cookie match */
const headers: Record<string, string> = { const headers: Record<string, string> = {
Authorization: Constants.GUEST_BEARER_TOKEN, Authorization: Constants.GUEST_BEARER_TOKEN,