2023-02-12 07:40:49 +00:00
|
|
|
import { genericUserAgent } from "../../config.js";
|
2023-04-27 04:26:19 +01:00
|
|
|
import crypto from "crypto";
|
2023-02-12 07:40:49 +00:00
|
|
|
|
|
|
|
function bestQuality(arr) {
|
|
|
|
return arr.filter((v) => { if (v["content_type"] === "video/mp4") return true }).sort((a, b) => Number(b.bitrate) - Number(a.bitrate))[0]["url"].split("?")[0]
|
|
|
|
}
|
2023-04-27 04:26:19 +01:00
|
|
|
const apiURL = "https://api.twitter.com"
|
2023-02-12 07:40:49 +00:00
|
|
|
|
|
|
|
export default async function(obj) {
|
|
|
|
let _headers = {
|
|
|
|
"user-agent": genericUserAgent,
|
|
|
|
"authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
|
|
|
|
// ^ no explicit content, but with multi media support
|
2023-04-27 04:26:19 +01:00
|
|
|
"host": "api.twitter.com",
|
|
|
|
"x-twitter-client-language": "en",
|
|
|
|
"x-twitter-active-user": "yes",
|
|
|
|
"Accept-Language": "en"
|
2023-02-12 07:40:49 +00:00
|
|
|
};
|
2023-04-27 04:26:19 +01:00
|
|
|
let conversationURL = `${apiURL}/2/timeline/conversation/${obj.id}.json?cards_platform=Web-12&tweet_mode=extended&include_cards=1&include_ext_media_availability=true&include_ext_sensitive_media_warning=true&simple_quoted_tweet=true&trim_user=1`;
|
|
|
|
let activateURL = `${apiURL}/1.1/guest/activate.json`;
|
|
|
|
|
|
|
|
let req_act = await fetch(activateURL, {
|
2023-02-12 07:40:49 +00:00
|
|
|
method: "POST",
|
|
|
|
headers: _headers
|
|
|
|
}).then((r) => { return r.status === 200 ? r.json() : false }).catch(() => { return false });
|
|
|
|
if (!req_act) return { error: 'ErrorCouldntFetch' };
|
|
|
|
|
|
|
|
_headers["x-guest-token"] = req_act["guest_token"];
|
2023-04-27 04:26:19 +01:00
|
|
|
_headers["cookie"] = [
|
|
|
|
`guest_id_ads=v1%3A${req_act["guest_token"]}`,
|
|
|
|
`guest_id_marketing=v1%3A${req_act["guest_token"]}`,
|
|
|
|
`guest_id=v1%3A${req_act["guest_token"]}`,
|
|
|
|
`ct0=${crypto.randomUUID().replace(/-/g, '')};`
|
|
|
|
].join('; ');
|
2023-02-12 07:40:49 +00:00
|
|
|
|
|
|
|
if (!obj.spaceId) {
|
2023-04-27 04:26:19 +01:00
|
|
|
let conversation = await fetch(conversationURL, { headers: _headers }).then((r) => { return r.status === 200 ? r.json() : false }).catch((e) => { return false });
|
|
|
|
if (!conversation || !conversation.globalObjects.tweets[obj.id]) {
|
2023-02-12 07:40:49 +00:00
|
|
|
_headers.authorization = "Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw";
|
|
|
|
// ^ explicit content, but no multi media support
|
2023-04-27 04:26:19 +01:00
|
|
|
delete _headers["x-guest-token"];
|
|
|
|
delete _headers["cookie"];
|
2023-02-12 07:40:49 +00:00
|
|
|
|
2023-04-27 04:26:19 +01:00
|
|
|
req_act = await fetch(activateURL, {
|
2023-02-12 07:40:49 +00:00
|
|
|
method: "POST",
|
|
|
|
headers: _headers
|
|
|
|
}).then((r) => { return r.status === 200 ? r.json() : false}).catch(() => { return false });
|
|
|
|
if (!req_act) return { error: 'ErrorCouldntFetch' };
|
|
|
|
|
2023-04-27 04:26:19 +01:00
|
|
|
_headers["x-guest-token"] = req_act["guest_token"]
|
|
|
|
_headers['cookie'] = [
|
|
|
|
`guest_id_ads=v1%3A${req_act["guest_token"]}`,
|
|
|
|
`guest_id_marketing=v1%3A${req_act["guest_token"]}`,
|
|
|
|
`guest_id=v1%3A${req_act["guest_token"]}`,
|
|
|
|
`ct0=${crypto.randomUUID().replace(/-/g, '')};`
|
|
|
|
].join('; ');
|
|
|
|
|
|
|
|
conversation = await fetch(conversationURL, { headers: _headers }).then((r) => { return r.status === 200 ? r.json() : false }).catch(() => { return false });
|
2023-02-12 07:40:49 +00:00
|
|
|
}
|
2023-04-27 04:26:19 +01:00
|
|
|
if (!conversation || !conversation.globalObjects.tweets[obj.id]) return { error: 'ErrorTweetUnavailable' };
|
2023-02-12 07:40:49 +00:00
|
|
|
|
2023-04-27 04:26:19 +01:00
|
|
|
let baseMedia, baseTweet = conversation.globalObjects.tweets[obj.id];
|
|
|
|
if (baseTweet.retweeted_status_id_str && conversation.globalObjects.tweets[baseTweet.retweeted_status_id_str].extended_entities) {
|
|
|
|
baseMedia = conversation.globalObjects.tweets[baseTweet.retweeted_status_id_str].extended_entities
|
|
|
|
} else if (baseTweet.extended_entities && baseTweet.extended_entities.media) {
|
|
|
|
baseMedia = baseTweet.extended_entities
|
2023-03-24 17:16:10 +00:00
|
|
|
}
|
2023-04-27 04:26:19 +01:00
|
|
|
if (!baseMedia) return { error: 'ErrorNoVideosInTweet' };
|
2023-03-24 17:16:10 +00:00
|
|
|
|
2023-04-27 04:26:19 +01:00
|
|
|
let single, multiple = [], media = baseMedia["media"];
|
2023-02-12 07:40:49 +00:00
|
|
|
media = media.filter((i) => { if (i["type"] === "video" || i["type"] === "animated_gif") return true })
|
|
|
|
if (media.length > 1) {
|
|
|
|
for (let i in media) { multiple.push({type: "video", thumb: media[i]["media_url_https"], url: bestQuality(media[i]["video_info"]["variants"])}) }
|
|
|
|
} else if (media.length === 1) {
|
|
|
|
single = bestQuality(media[0]["video_info"]["variants"])
|
|
|
|
} else {
|
|
|
|
return { error: 'ErrorNoVideosInTweet' }
|
|
|
|
}
|
|
|
|
|
|
|
|
if (single) {
|
|
|
|
return { urls: single, filename: `twitter_${obj.id}.mp4`, audioFilename: `twitter_${obj.id}_audio` }
|
|
|
|
} else if (multiple) {
|
|
|
|
return { picker: multiple }
|
|
|
|
} else {
|
|
|
|
return { error: 'ErrorNoVideosInTweet' }
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-24 17:16:10 +00:00
|
|
|
_headers["host"] = "twitter.com";
|
|
|
|
_headers["content-type"] = "application/json";
|
2023-02-12 07:40:49 +00:00
|
|
|
|
|
|
|
let query = {
|
2023-03-24 17:16:10 +00:00
|
|
|
variables: {"id": obj.spaceId,"isMetatagsQuery":true,"withDownvotePerspective":false,"withReactionsMetadata":false,"withReactionsPerspective":false,"withReplays":true},
|
|
|
|
features: {"spaces_2022_h2_clipping":true,"spaces_2022_h2_spaces_communities":true,"responsive_web_twitter_blue_verified_badge_is_enabled":true,"responsive_web_graphql_exclude_directive_enabled":true,"verified_phone_label_enabled":false,"responsive_web_graphql_skip_user_profile_image_extensions_enabled":false,"tweetypie_unmention_optimization_enabled":true,"vibe_api_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,"tweet_awards_web_tipping_enabled":false,"freedom_of_speech_not_reach_fetch_enabled":false,"standardized_nudges_misinfo":true,"tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled":false,"responsive_web_graphql_timeline_navigation_enabled":true,"interactive_text_enabled":true,"responsive_web_text_conversations_enabled":false,"longform_notetweets_richtext_consumption_enabled":false,"responsive_web_enhance_cards_enabled":false}
|
2023-02-12 07:40:49 +00:00
|
|
|
}
|
|
|
|
query.variables = new URLSearchParams(JSON.stringify(query.variables)).toString().slice(0, -1);
|
|
|
|
query.features = new URLSearchParams(JSON.stringify(query.features)).toString().slice(0, -1);
|
2023-03-24 17:16:10 +00:00
|
|
|
query = `https://twitter.com/i/api/graphql/Gdz2uCtmIGMmhjhHG3V7nA/AudioSpaceById?variables=${query.variables}&features=${query.features}`;
|
2023-02-12 07:40:49 +00:00
|
|
|
|
|
|
|
let AudioSpaceById = await fetch(query, { headers: _headers }).then((r) => {return r.status === 200 ? r.json() : false}).catch((e) => { return false });
|
|
|
|
if (!AudioSpaceById) return { error: 'ErrorEmptyDownload' };
|
|
|
|
|
|
|
|
if (!AudioSpaceById.data.audioSpace.metadata) return { error: 'ErrorEmptyDownload' };
|
2023-02-13 14:02:52 +00:00
|
|
|
if (AudioSpaceById.data.audioSpace.metadata.is_space_available_for_replay !== true) return { error: 'TwitterSpaceWasntRecorded' };
|
2023-02-12 07:40:49 +00:00
|
|
|
|
|
|
|
let streamStatus = await fetch(
|
|
|
|
`https://twitter.com/i/api/1.1/live_video_stream/status/${AudioSpaceById.data.audioSpace.metadata.media_key}`, { headers: _headers }
|
|
|
|
).then((r) =>{ return r.status === 200 ? r.json() : false }).catch(() => { return false });
|
|
|
|
if (!streamStatus) return { error: 'ErrorCouldntFetch' };
|
|
|
|
|
2023-03-15 16:18:31 +00:00
|
|
|
let participants = AudioSpaceById.data.audioSpace.participants.speakers,
|
|
|
|
listOfParticipants = `Twitter Space speakers: `;
|
2023-02-12 07:40:49 +00:00
|
|
|
for (let i in participants) { listOfParticipants += `@${participants[i]["twitter_screen_name"]}, ` }
|
|
|
|
listOfParticipants = listOfParticipants.slice(0, -2);
|
|
|
|
|
|
|
|
return {
|
|
|
|
urls: streamStatus.source.noRedirectPlaybackUrl,
|
|
|
|
audioFilename: `twitterspaces_${obj.spaceId}`,
|
|
|
|
isAudioOnly: true,
|
|
|
|
fileMetadata: {
|
|
|
|
title: AudioSpaceById.data.audioSpace.metadata.title,
|
|
|
|
artist: `Twitter Space by @${AudioSpaceById.data.audioSpace.metadata.creator_results.result.legacy.screen_name}`,
|
|
|
|
comment: listOfParticipants,
|
|
|
|
// cover: AudioSpaceById.data.audioSpace.metadata.creator_results.result.legacy.profile_image_url_https.replace("_normal", "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|