File size: 1,753 Bytes
3929273 3c2ff1f 0c6a4ee 523b92a 0c6a4ee 3929273 0c6a4ee 3929273 0c6a4ee 3929273 0c6a4ee 523b92a 3929273 3289e37 cc3d1da 3929273 523b92a fd431dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import got from 'got';
import * as cheerio from 'cheerio';
import { TwitterDlArgsSchema, TwitterDLResponseSchema, TwitterDlSchema } from '../types/twitter-v1.js';
import { DEFAULT_HEADERS } from '../constant.js';
import { stringifyCookies, generateTokenId } from './util.js';
export async function twitterdl(url) {
if (!url || typeof url !== 'string') {
throw new Error("Invalid or missing URL parameter.");
}
TwitterDlArgsSchema.parse([url]);
const idMatch = url.match(/status\/(\d+)/) || url.match(/(\d+)/);
if (!idMatch) {
throw new Error("Invalid Twitter URL: Cannot extract tweet ID.");
}
const id = idMatch[1];
const token = generateTokenId(id);
try {
const data = await got(`https://api.redketchup.io/tweetAttachments-v6?id=${encodeURIComponent(token)}`, {
headers: {
...DEFAULT_HEADERS,
origin: 'https://redketchup.io',
referer: 'https://redketchup.io/',
}
}).json();
if (!data || !data.includes || !data.includes.media) {
throw new Error("Invalid API response: Missing media data.");
}
const json = TwitterDLResponseSchema.parse(data);
const videos = json.includes.media
.filter((m) => m.type === 'video')
.flatMap((m) => m.variants)
.filter((v) => v.content_type !== 'application/x-mpegURL');
const result = Array.isArray(videos) ? videos : [videos];
return result
// return TwitterDlSchema.parse(result);
} catch (error) {
console.error("Error fetching Twitter video:", error.message);
throw new Error("Failed to fetch Twitter video. Please try again later.");
}
}
|