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";
|
2023-02-12 13:40:49 +06:00
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
return mediaTimestamp > badContainerStart && mediaTimestamp < badContainerEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
function bestQuality(arr) {
|
|
|
|
return arr.filter(v => v["content_type"] === "video/mp4").sort((a, b) => Number(b.bitrate) - Number(a.bitrate))[0]["url"]
|
|
|
|
}
|
2023-12-17 23:05:43 +06:00
|
|
|
|
2023-02-12 13:40:49 +06:00
|
|
|
export default async function(obj) {
|
|
|
|
let _headers = {
|
|
|
|
"user-agent": genericUserAgent,
|
|
|
|
"authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
|
2023-04-27 09:26:19 +06:00
|
|
|
"host": "api.twitter.com",
|
|
|
|
"x-twitter-client-language": "en",
|
|
|
|
"x-twitter-active-user": "yes",
|
2023-08-15 14:37:59 +06:00
|
|
|
"accept-language": "en"
|
2023-02-12 13:40:49 +06:00
|
|
|
};
|
2023-08-15 14:37:59 +06:00
|
|
|
|
|
|
|
let activateURL = `https://api.twitter.com/1.1/guest/activate.json`;
|
2023-11-07 22:37:47 +06:00
|
|
|
let graphqlTweetURL = `https://twitter.com/i/api/graphql/5GOHgZe-8U2j5sVHQzEm9A/TweetResultByRestId`;
|
2023-04-27 09:26:19 +06:00
|
|
|
|
|
|
|
let req_act = await fetch(activateURL, {
|
2023-02-12 13:40:49 +06:00
|
|
|
method: "POST",
|
|
|
|
headers: _headers
|
|
|
|
}).then((r) => { return r.status === 200 ? r.json() : false }).catch(() => { return false });
|
|
|
|
if (!req_act) return { error: 'ErrorCouldntFetch' };
|
|
|
|
|
2023-08-15 14:37:59 +06:00
|
|
|
_headers["host"] = "twitter.com";
|
|
|
|
_headers["content-type"] = "application/json";
|
|
|
|
|
2023-02-12 13:40:49 +06:00
|
|
|
_headers["x-guest-token"] = req_act["guest_token"];
|
2023-08-15 14:37:59 +06:00
|
|
|
_headers["cookie"] = `guest_id=v1%3A${req_act["guest_token"]}`;
|
|
|
|
|
2023-11-07 22:37:47 +06:00
|
|
|
let query = {
|
|
|
|
variables: { "tweetId": obj.id, "withCommunity": false, "includePromotedContent": false, "withVoice": false },
|
|
|
|
features: { "creator_subscriptions_tweet_preview_api_enabled": true, "c9s_tweet_anatomy_moderator_badge_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": false, "tweet_awards_web_tipping_enabled": false, "responsive_web_home_pinned_timelines_enabled": true, "freedom_of_speech_not_reach_fetch_enabled": true, "standardized_nudges_misinfo": true, "tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled": true, "longform_notetweets_rich_text_read_enabled": true, "longform_notetweets_inline_media_enabled": true, "responsive_web_graphql_exclude_directive_enabled": true, "verified_phone_label_enabled": false, "responsive_web_media_download_video_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 }
|
|
|
|
}
|
|
|
|
query.variables = encodeURIComponent(JSON.stringify(query.variables));
|
|
|
|
query.features = encodeURIComponent(JSON.stringify(query.features));
|
|
|
|
query = `${graphqlTweetURL}?variables=${query.variables}&features=${query.features}`;
|
2023-08-15 14:37:59 +06:00
|
|
|
|
2023-11-07 22:37:47 +06:00
|
|
|
let tweet = await fetch(query, { headers: _headers }).then((r) => {
|
|
|
|
return r.status === 200 ? r.json() : false
|
2023-12-02 20:44:19 +06:00
|
|
|
}).catch(() => { return false });
|
2023-03-24 23:16:10 +06:00
|
|
|
|
2023-11-07 22:37:47 +06:00
|
|
|
// {"data":{"tweetResult":{"result":{"__typename":"TweetUnavailable","reason":"Protected"}}}}
|
|
|
|
if (tweet?.data?.tweetResult?.result?.__typename !== "Tweet") {
|
|
|
|
return { error: 'ErrorTweetUnavailable' }
|
|
|
|
}
|
2023-08-15 14:37:59 +06:00
|
|
|
|
2023-11-07 22:37:47 +06:00
|
|
|
let baseMedia,
|
|
|
|
baseTweet = tweet.data.tweetResult.result.legacy;
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2023-11-07 22:37:47 +06:00
|
|
|
if (baseTweet.retweeted_status_result?.result.legacy.extended_entities.media) {
|
|
|
|
baseMedia = baseTweet.retweeted_status_result.result.legacy.extended_entities
|
|
|
|
} else if (baseTweet.extended_entities?.media) {
|
|
|
|
baseMedia = baseTweet.extended_entities
|
2023-08-15 14:37:59 +06:00
|
|
|
}
|
2023-11-07 22:37:47 +06:00
|
|
|
if (!baseMedia) return { error: 'ErrorNoVideosInTweet' };
|
|
|
|
|
|
|
|
let single, multiple = [], media = baseMedia["media"];
|
|
|
|
media = media.filter((i) => { if (i["type"] === "video" || i["type"] === "animated_gif") return true });
|
|
|
|
|
2023-12-17 23:45:15 +06:00
|
|
|
if (media.length === 0) {
|
|
|
|
return { error: 'ErrorNoVideosInTweet' }
|
|
|
|
}
|
2023-12-17 23:05:43 +06:00
|
|
|
|
2023-11-07 22:37:47 +06:00
|
|
|
if (media.length > 1) {
|
|
|
|
for (let i in media) {
|
2023-12-17 23:05:43 +06:00
|
|
|
let downloadUrl = bestQuality(media[i]["video_info"]["variants"]);
|
2023-12-17 23:45:15 +06:00
|
|
|
if (needsFixing(media[i])) {
|
2023-12-17 23:05:43 +06:00
|
|
|
downloadUrl = createStream({
|
2023-12-02 20:44:19 +06:00
|
|
|
service: "twitter",
|
|
|
|
type: "remux",
|
|
|
|
u: bestQuality(media[i]["video_info"]["variants"]),
|
|
|
|
filename: `twitter_${obj.id}_${Number(i) + 1}.mp4`
|
|
|
|
})
|
2023-12-17 23:05:43 +06:00
|
|
|
}
|
|
|
|
multiple.push({
|
|
|
|
type: "video",
|
|
|
|
thumb: media[i]["media_url_https"],
|
|
|
|
url: downloadUrl
|
2023-11-07 22:37:47 +06:00
|
|
|
})
|
2023-02-12 13:40:49 +06:00
|
|
|
}
|
2023-11-07 22:37:47 +06:00
|
|
|
} else {
|
2023-12-17 23:45:15 +06:00
|
|
|
single = bestQuality(media[0]["video_info"]["variants"])
|
2023-11-07 22:37:47 +06:00
|
|
|
}
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2023-11-07 22:37:47 +06:00
|
|
|
if (single) {
|
2023-02-12 13:40:49 +06:00
|
|
|
return {
|
2023-12-17 23:45:15 +06:00
|
|
|
type: needsFixing(media[0]) ? "remux" : "normal",
|
2023-11-07 22:37:47 +06:00
|
|
|
urls: single,
|
|
|
|
filename: `twitter_${obj.id}.mp4`,
|
|
|
|
audioFilename: `twitter_${obj.id}_audio`
|
2023-02-12 13:40:49 +06:00
|
|
|
}
|
2023-11-07 22:37:47 +06:00
|
|
|
} else if (multiple) {
|
|
|
|
return { picker: multiple }
|
|
|
|
} else {
|
|
|
|
return { error: 'ErrorNoVideosInTweet' }
|
2023-02-12 13:40:49 +06:00
|
|
|
}
|
|
|
|
}
|