2023-02-12 13:40:49 +06:00
|
|
|
import { genericUserAgent } from "../../config.js";
|
2023-12-02 20:44:19 +06:00
|
|
|
import { createStream } from "../../stream/manage.js";
|
2024-05-03 18:22:33 +06:00
|
|
|
import { getCookie, updateCookie } from "../cookie/manager.js";
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2024-05-23 09:21:38 +06:00
|
|
|
const graphqlURL = 'https://api.x.com/graphql/I9GDzyCGZL2wSoYFFrrTVw/TweetResultByRestId';
|
|
|
|
const tokenURL = 'https://api.x.com/1.1/guest/activate.json';
|
2024-01-07 13:48:30 +06:00
|
|
|
|
2024-05-23 09:21:38 +06:00
|
|
|
const tweetFeatures = JSON.stringify({"creator_subscriptions_tweet_preview_api_enabled":true,"communities_web_enable_tweet_community_results_fetch":true,"c9s_tweet_anatomy_moderator_badge_enabled":true,"articles_preview_enabled":true,"tweetypie_unmention_optimization_enabled":true,"responsive_web_edit_tweet_api_enabled":true,"graphql_is_translatable_rweb_tweet_is_translatable_enabled":true,"view_counts_everywhere_api_enabled":true,"longform_notetweets_consumption_enabled":true,"responsive_web_twitter_article_tweet_consumption_enabled":true,"tweet_awards_web_tipping_enabled":false,"creator_subscriptions_quote_tweet_preview_enabled":false,"freedom_of_speech_not_reach_fetch_enabled":true,"standardized_nudges_misinfo":true,"tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled":true,"rweb_video_timestamps_enabled":true,"longform_notetweets_rich_text_read_enabled":true,"longform_notetweets_inline_media_enabled":true,"rweb_tipjar_consumption_enabled":true,"responsive_web_graphql_exclude_directive_enabled":true,"verified_phone_label_enabled":false,"responsive_web_graphql_skip_user_profile_image_extensions_enabled":false,"responsive_web_graphql_timeline_navigation_enabled":true,"responsive_web_enhance_cards_enabled":false});
|
|
|
|
|
|
|
|
const tweetFieldToggles = JSON.stringify({"withArticleRichContentState":true,"withArticlePlainText":false,"withGrokAnalyze":false});
|
2024-01-07 13:48:30 +06:00
|
|
|
|
|
|
|
const commonHeaders = {
|
|
|
|
"user-agent": genericUserAgent,
|
|
|
|
"authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
|
|
|
|
"x-twitter-client-language": "en",
|
|
|
|
"x-twitter-active-user": "yes",
|
|
|
|
"accept-language": "en"
|
|
|
|
}
|
|
|
|
|
2023-12-17 23:05:43 +06:00
|
|
|
// fix all videos affected by the container bug in twitter muxer (took them over two weeks to fix it????)
|
2023-12-17 23:50:04 +06:00
|
|
|
const TWITTER_EPOCH = 1288834974657n;
|
2023-12-17 23:05:43 +06:00
|
|
|
const badContainerStart = new Date(1701446400000);
|
|
|
|
const badContainerEnd = new Date(1702605600000);
|
2023-12-17 23:45:15 +06:00
|
|
|
|
|
|
|
function needsFixing(media) {
|
|
|
|
const representativeId = media.source_status_id_str ?? media.id_str;
|
|
|
|
const mediaTimestamp = new Date(
|
|
|
|
Number((BigInt(representativeId) >> 22n) + TWITTER_EPOCH)
|
2024-01-07 13:48:30 +06:00
|
|
|
);
|
|
|
|
return mediaTimestamp > badContainerStart && mediaTimestamp < badContainerEnd
|
2023-12-17 23:45:15 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
function bestQuality(arr) {
|
2024-01-07 13:48:30 +06:00
|
|
|
return arr.filter(v => v.content_type === "video/mp4")
|
|
|
|
.reduce((a, b) => Number(a?.bitrate) > Number(b?.bitrate) ? a : b)
|
|
|
|
.url
|
2023-12-17 23:45:15 +06:00
|
|
|
}
|
2023-12-17 23:05:43 +06:00
|
|
|
|
2024-01-07 13:48:30 +06:00
|
|
|
let _cachedToken;
|
2024-05-23 09:22:33 +06:00
|
|
|
const getGuestToken = async (dispatcher, forceReload = false) => {
|
2024-01-04 16:35:58 +00:00
|
|
|
if (_cachedToken && !forceReload) {
|
|
|
|
return _cachedToken;
|
|
|
|
}
|
|
|
|
|
2024-01-07 13:48:30 +06:00
|
|
|
const tokenResponse = await fetch(tokenURL, {
|
|
|
|
method: 'POST',
|
2024-05-23 09:22:33 +06:00
|
|
|
headers: commonHeaders,
|
|
|
|
dispatcher
|
2024-01-07 13:48:30 +06:00
|
|
|
}).then(r => r.status === 200 && r.json()).catch(() => {})
|
2023-08-15 14:37:59 +06:00
|
|
|
|
2024-01-04 16:35:58 +00:00
|
|
|
if (tokenResponse?.guest_token) {
|
|
|
|
return _cachedToken = tokenResponse.guest_token
|
|
|
|
}
|
2024-01-04 16:26:52 +00:00
|
|
|
}
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2024-05-23 09:22:33 +06:00
|
|
|
const requestTweet = async(dispatcher, tweetId, token, cookie) => {
|
2024-01-07 13:48:30 +06:00
|
|
|
const graphqlTweetURL = new URL(graphqlURL);
|
|
|
|
|
2024-05-03 18:22:33 +06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 13:48:30 +06:00
|
|
|
graphqlTweetURL.searchParams.set('variables',
|
2024-01-04 16:26:52 +00:00
|
|
|
JSON.stringify({
|
|
|
|
tweetId,
|
|
|
|
withCommunity: false,
|
|
|
|
includePromotedContent: false,
|
|
|
|
withVoice: false
|
|
|
|
})
|
|
|
|
);
|
|
|
|
graphqlTweetURL.searchParams.set('features', tweetFeatures);
|
2024-05-23 09:21:38 +06:00
|
|
|
graphqlTweetURL.searchParams.set('fieldToggles', tweetFieldToggles);
|
2024-01-04 16:26:52 +00:00
|
|
|
|
2024-05-23 09:22:33 +06:00
|
|
|
let result = await fetch(graphqlTweetURL, { headers, dispatcher });
|
2024-05-03 18:22:33 +06:00
|
|
|
updateCookie(cookie, result.headers);
|
|
|
|
|
|
|
|
// we might have been missing the `ct0` cookie, retry
|
|
|
|
if (result.status === 403 && result.headers.get('set-cookie')) {
|
|
|
|
result = await fetch(graphqlTweetURL, {
|
|
|
|
headers: {
|
|
|
|
...headers,
|
|
|
|
'x-csrf-token': cookie.values().ct0
|
2024-05-23 09:22:33 +06:00
|
|
|
},
|
|
|
|
dispatcher
|
2024-05-03 18:22:33 +06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2024-01-04 16:26:52 +00:00
|
|
|
}
|
2023-11-07 22:37:47 +06:00
|
|
|
|
2024-05-23 09:22:33 +06:00
|
|
|
export default async function({ id, index, toGif, dispatcher }) {
|
2024-05-03 18:22:33 +06:00
|
|
|
const cookie = await getCookie('twitter');
|
|
|
|
|
2024-05-23 09:22:33 +06:00
|
|
|
let guestToken = await getGuestToken(dispatcher);
|
2024-08-20 21:10:37 +06:00
|
|
|
if (!guestToken) return { error: "fetch.fail" };
|
2023-11-07 22:37:47 +06:00
|
|
|
|
2024-05-23 09:22:33 +06:00
|
|
|
let tweet = await requestTweet(dispatcher, id, guestToken);
|
2024-01-04 16:35:58 +00:00
|
|
|
|
2024-05-03 18:22:33 +06:00
|
|
|
// get new token & retry if old one expired
|
|
|
|
if ([403, 429].includes(tweet.status)) {
|
2024-05-23 09:22:33 +06:00
|
|
|
guestToken = await getGuestToken(dispatcher, true);
|
|
|
|
tweet = await requestTweet(dispatcher, id, guestToken)
|
2024-01-04 16:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tweet = await tweet.json();
|
|
|
|
|
2024-05-03 18:22:33 +06:00
|
|
|
let tweetTypename = tweet?.data?.tweetResult?.result?.__typename;
|
2024-02-02 23:12:05 +06:00
|
|
|
|
|
|
|
if (tweetTypename === "TweetUnavailable") {
|
|
|
|
const reason = tweet?.data?.tweetResult?.result?.reason;
|
|
|
|
switch(reason) {
|
|
|
|
case "Protected":
|
2024-08-20 21:10:37 +06:00
|
|
|
return { error: "content.post.private" }
|
2024-02-02 23:12:05 +06:00
|
|
|
case "NsfwLoggedOut":
|
2024-05-03 18:22:33 +06:00
|
|
|
if (cookie) {
|
2024-05-23 09:22:33 +06:00
|
|
|
tweet = await requestTweet(dispatcher, id, guestToken, cookie);
|
2024-05-03 18:22:33 +06:00
|
|
|
tweet = await tweet.json();
|
|
|
|
tweetTypename = tweet?.data?.tweetResult?.result?.__typename;
|
2024-08-20 21:10:37 +06:00
|
|
|
} else return { error: "content.post.age" }
|
2024-02-02 23:12:05 +06:00
|
|
|
}
|
|
|
|
}
|
2024-05-03 18:22:33 +06:00
|
|
|
|
2024-08-20 21:10:37 +06:00
|
|
|
if (!tweetTypename) {
|
|
|
|
return { error: "link.invalid" }
|
|
|
|
}
|
|
|
|
|
2024-05-03 18:22:33 +06:00
|
|
|
if (!["Tweet", "TweetWithVisibilityResults"].includes(tweetTypename)) {
|
2024-08-20 21:10:37 +06:00
|
|
|
return { error: "content.post.unavailable" }
|
2023-12-17 23:45:15 +06:00
|
|
|
}
|
2023-12-17 23:05:43 +06:00
|
|
|
|
2024-05-03 18:22:33 +06:00
|
|
|
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;
|
|
|
|
}
|
2024-01-04 16:26:52 +00:00
|
|
|
|
2024-03-07 00:27:17 +06:00
|
|
|
let media = (repostedTweet?.media || baseTweet?.extended_entities?.media);
|
2024-01-04 16:26:52 +00:00
|
|
|
|
2024-01-07 14:22:59 +06:00
|
|
|
// check if there's a video at given index (/video/<index>)
|
2024-05-03 18:22:33 +06:00
|
|
|
if (index >= 0 && index < media?.length) {
|
2024-01-07 13:48:30 +06:00
|
|
|
media = [media[index]]
|
2024-01-05 12:21:59 +00:00
|
|
|
}
|
2024-01-07 14:22:59 +06:00
|
|
|
|
2024-01-04 16:26:52 +00:00
|
|
|
switch (media?.length) {
|
|
|
|
case undefined:
|
|
|
|
case 0:
|
2024-08-20 21:10:37 +06:00
|
|
|
return {
|
|
|
|
error: "fetch.empty"
|
|
|
|
}
|
2024-01-04 16:26:52 +00:00
|
|
|
case 1:
|
2024-07-26 21:17:37 +06:00
|
|
|
if (media[0].type === "photo") {
|
|
|
|
return {
|
2024-08-22 17:37:31 +06:00
|
|
|
type: "proxy",
|
2024-07-26 21:17:37 +06:00
|
|
|
isPhoto: true,
|
|
|
|
urls: `${media[0].media_url_https}?name=4096x4096`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 16:26:52 +00:00
|
|
|
return {
|
2024-08-22 17:37:31 +06:00
|
|
|
type: needsFixing(media[0]) ? "remux" : "proxy",
|
2024-01-04 16:26:52 +00:00
|
|
|
urls: bestQuality(media[0].video_info.variants),
|
|
|
|
filename: `twitter_${id}.mp4`,
|
2024-01-17 11:38:51 +06:00
|
|
|
audioFilename: `twitter_${id}_audio`,
|
|
|
|
isGif: media[0].type === "animated_gif"
|
2024-07-26 21:17:37 +06:00
|
|
|
}
|
2024-01-04 16:26:52 +00:00
|
|
|
default:
|
2024-08-22 13:38:16 +06:00
|
|
|
const proxyThumb = (url) =>
|
|
|
|
createStream({
|
|
|
|
service: "twitter",
|
2024-08-22 17:37:31 +06:00
|
|
|
type: "proxy",
|
2024-08-22 13:38:16 +06:00
|
|
|
u: url,
|
|
|
|
filename: `image.${new URL(url).pathname.split(".", 2)[1]}`
|
|
|
|
})
|
|
|
|
|
2024-01-18 15:57:21 +00:00
|
|
|
const picker = media.map((content, i) => {
|
2024-07-26 21:17:37 +06:00
|
|
|
if (content.type === "photo") {
|
|
|
|
let url = `${content.media_url_https}?name=4096x4096`;
|
|
|
|
return {
|
|
|
|
type: "photo",
|
|
|
|
url,
|
2024-08-22 13:38:16 +06:00
|
|
|
thumb: proxyThumb(url),
|
2024-07-26 21:17:37 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:57:21 +00:00
|
|
|
let url = bestQuality(content.video_info.variants);
|
2024-08-22 17:37:31 +06:00
|
|
|
const shouldRenderGif = content.type === "animated_gif" && toGif;
|
2024-01-18 15:57:21 +00:00
|
|
|
|
2024-07-26 21:17:37 +06:00
|
|
|
let type = "video";
|
|
|
|
if (shouldRenderGif) type = "gif";
|
|
|
|
|
2024-01-18 15:57:21 +00:00
|
|
|
if (needsFixing(content) || shouldRenderGif) {
|
2024-01-04 16:26:52 +00:00
|
|
|
url = createStream({
|
2024-08-22 17:37:31 +06:00
|
|
|
service: "twitter",
|
|
|
|
type: shouldRenderGif ? "gif" : "remux",
|
2024-01-07 13:48:30 +06:00
|
|
|
u: url,
|
2024-01-18 15:57:21 +00:00
|
|
|
filename: `twitter_${id}_${i + 1}.mp4`
|
2024-01-04 16:26:52 +00:00
|
|
|
})
|
|
|
|
}
|
2024-01-18 15:57:21 +00:00
|
|
|
|
2024-01-04 16:26:52 +00:00
|
|
|
return {
|
2024-07-26 21:17:37 +06:00
|
|
|
type,
|
2024-01-07 13:48:30 +06:00
|
|
|
url,
|
2024-08-22 13:38:16 +06:00
|
|
|
thumb: proxyThumb(content.media_url_https),
|
2024-01-04 16:26:52 +00:00
|
|
|
}
|
|
|
|
});
|
2024-01-07 14:22:59 +06:00
|
|
|
return { picker };
|
2023-02-12 13:40:49 +06:00
|
|
|
}
|
|
|
|
}
|