twitter: add support for cookies and sensitive tweets

This commit is contained in:
wukko 2024-05-03 15:07:54 +06:00
parent a5a01cc0c6
commit 58f8301f21
No known key found for this signature in database
GPG Key ID: 3E30B3F26C7B4AA2

View File

@ -1,5 +1,6 @@
import { genericUserAgent } from "../../config.js"; import { genericUserAgent } from "../../config.js";
import { createStream } from "../../stream/manage.js"; import { createStream } from "../../stream/manage.js";
import { getCookie, updateCookie } from "../cookie/manager.js";
const graphqlURL = 'https://twitter.com/i/api/graphql/5GOHgZe-8U2j5sVHQzEm9A/TweetResultByRestId'; const graphqlURL = 'https://twitter.com/i/api/graphql/5GOHgZe-8U2j5sVHQzEm9A/TweetResultByRestId';
const tokenURL = 'https://api.twitter.com/1.1/guest/activate.json'; const tokenURL = 'https://api.twitter.com/1.1/guest/activate.json';
@ -49,9 +50,26 @@ const getGuestToken = async (forceReload = false) => {
} }
} }
const requestTweet = (tweetId, token) => { const requestTweet = async(tweetId, token, cookie) => {
const graphqlTweetURL = new URL(graphqlURL); const graphqlTweetURL = new URL(graphqlURL);
let headers = {
...commonHeaders,
'content-type': 'application/json',
'x-guest-token': token,
cookie: `guest_id=${encodeURIComponent(`v1:${token}`)}`
}
if (cookie) {
headers = {
...commonHeaders,
'X-Twitter-Auth-Type': 'OAuth2Session',
'content-type': 'application/json',
'x-csrf-token': cookie.values().ct0,
cookie
}
}
graphqlTweetURL.searchParams.set('variables', graphqlTweetURL.searchParams.set('variables',
JSON.stringify({ JSON.stringify({
tweetId, tweetId,
@ -62,31 +80,32 @@ const requestTweet = (tweetId, token) => {
); );
graphqlTweetURL.searchParams.set('features', tweetFeatures); graphqlTweetURL.searchParams.set('features', tweetFeatures);
return fetch(graphqlTweetURL, { let result = await fetch(graphqlTweetURL, { headers });
headers: {
...commonHeaders, if (cookie) {
'content-type': 'application/json', updateCookie(cookie, result.headers);
'x-guest-token': token,
cookie: `guest_id=${encodeURIComponent(`v1:${token}`)}`
} }
})
return result
} }
export default async function({ id, index, toGif }) { export default async function({ id, index, toGif }) {
const cookie = await getCookie('twitter');
let guestToken = await getGuestToken(); let guestToken = await getGuestToken();
if (!guestToken) return { error: 'ErrorCouldntFetch' }; if (!guestToken) return { error: 'ErrorCouldntFetch' };
let tweet = await requestTweet(id, guestToken); let tweet = await requestTweet(id, guestToken);
if ([403, 429].includes(tweet.status)) { // get new token & retry // get new token & retry if old one expired
if ([403, 429].includes(tweet.status)) {
guestToken = await getGuestToken(true); guestToken = await getGuestToken(true);
tweet = await requestTweet(id, guestToken) tweet = await requestTweet(id, guestToken)
} }
tweet = await tweet.json(); tweet = await tweet.json();
// {"data":{"tweetResult":{"result":{"__typename":"TweetUnavailable","reason":"Protected"}}}} let tweetTypename = tweet?.data?.tweetResult?.result?.__typename;
const tweetTypename = tweet?.data?.tweetResult?.result?.__typename;
if (tweetTypename === "TweetUnavailable") { if (tweetTypename === "TweetUnavailable") {
const reason = tweet?.data?.tweetResult?.result?.reason; const reason = tweet?.data?.tweetResult?.result?.reason;
@ -94,15 +113,25 @@ export default async function({ id, index, toGif }) {
case "Protected": case "Protected":
return { error: 'ErrorTweetProtected' } return { error: 'ErrorTweetProtected' }
case "NsfwLoggedOut": case "NsfwLoggedOut":
return { error: 'ErrorTweetNSFW' } if (cookie) {
tweet = await requestTweet(id, guestToken, cookie);
tweet = await tweet.json();
tweetTypename = tweet?.data?.tweetResult?.result?.__typename;
} else return { error: 'ErrorTweetNSFW' }
} }
} }
if (tweetTypename !== "Tweet") {
return { error: 'ErrorTweetUnavailable' }
}
const baseTweet = tweet.data.tweetResult.result.legacy, if (!["Tweet", "TweetWithVisibilityResults"].includes(tweetTypename)) {
repostedTweet = baseTweet.retweeted_status_result?.result.legacy.extended_entities; return { error: 'ErrorTweetUnavailable' }
}
let tweetResult = tweet.data.tweetResult.result,
baseTweet = tweetResult.legacy,
repostedTweet = baseTweet?.retweeted_status_result?.result.legacy.extended_entities;
if (tweetTypename === "TweetWithVisibilityResults") {
baseTweet = tweetResult.tweet.legacy;
repostedTweet = baseTweet?.retweeted_status_result?.result.tweet.legacy.extended_entities;
}
let media = (repostedTweet?.media || baseTweet?.extended_entities?.media); let media = (repostedTweet?.media || baseTweet?.extended_entities?.media);
media = media?.filter(m => m.video_info?.variants?.length); media = media?.filter(m => m.video_info?.variants?.length);