mirror of
https://github.com/imputnet/cobalt.git
synced 2024-12-29 11:06:10 +00:00
twitter: add support for cookies and sensitive tweets (#485)
Co-authored-by: dumbmoron <log@riseup.net>
This commit is contained in:
parent
a5a01cc0c6
commit
182e32d5c3
|
@ -4,5 +4,8 @@
|
||||||
],
|
],
|
||||||
"reddit": [
|
"reddit": [
|
||||||
"client_id=<replace_this>; client_secret=<replace_this>; refresh_token=<replace_this>"
|
"client_id=<replace_this>; client_secret=<replace_this>; refresh_token=<replace_this>"
|
||||||
|
],
|
||||||
|
"twitter": [
|
||||||
|
"auth_token=<replace_this>; ct0=<replace_this>"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
'content-type': 'application/json',
|
||||||
|
'X-Twitter-Auth-Type': 'OAuth2Session',
|
||||||
|
'x-csrf-token': cookie.values().ct0,
|
||||||
|
cookie
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
graphqlTweetURL.searchParams.set('variables',
|
graphqlTweetURL.searchParams.set('variables',
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
tweetId,
|
tweetId,
|
||||||
|
@ -62,31 +80,39 @@ const requestTweet = (tweetId, token) => {
|
||||||
);
|
);
|
||||||
graphqlTweetURL.searchParams.set('features', tweetFeatures);
|
graphqlTweetURL.searchParams.set('features', tweetFeatures);
|
||||||
|
|
||||||
return fetch(graphqlTweetURL, {
|
let result = await fetch(graphqlTweetURL, { headers });
|
||||||
headers: {
|
updateCookie(cookie, result.headers);
|
||||||
...commonHeaders,
|
|
||||||
'content-type': 'application/json',
|
// we might have been missing the `ct0` cookie, retry
|
||||||
'x-guest-token': token,
|
if (result.status === 403 && result.headers.get('set-cookie')) {
|
||||||
cookie: `guest_id=${encodeURIComponent(`v1:${token}`)}`
|
result = await fetch(graphqlTweetURL, {
|
||||||
}
|
headers: {
|
||||||
})
|
...headers,
|
||||||
|
'x-csrf-token': cookie.values().ct0
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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,21 +120,32 @@ 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") {
|
|
||||||
|
if (!["Tweet", "TweetWithVisibilityResults"].includes(tweetTypename)) {
|
||||||
return { error: 'ErrorTweetUnavailable' }
|
return { error: 'ErrorTweetUnavailable' }
|
||||||
}
|
}
|
||||||
|
|
||||||
const baseTweet = tweet.data.tweetResult.result.legacy,
|
let tweetResult = tweet.data.tweetResult.result,
|
||||||
repostedTweet = baseTweet.retweeted_status_result?.result.legacy.extended_entities;
|
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);
|
||||||
|
|
||||||
// check if there's a video at given index (/video/<index>)
|
// check if there's a video at given index (/video/<index>)
|
||||||
if ([0, 1, 2, 3].includes(index) && index < media?.length) {
|
if (index >= 0 && index < media?.length) {
|
||||||
media = [media[index]]
|
media = [media[index]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue