Update index.js
Browse files318 unmodified lines319320321322323324325326327328329330331332333334335336337338322323324325326327328329330331332333334335336337338339340341342340341342343344345343344345346347348349350351352353354355356347348349350351352353354355356357358359360361362363364365358359366367368184 unmodified lines553554555550556557558559318 unmodified lines};
//TWITTER APIasync function twitterAPI(url, options = {text:true}) { let input = {}; if (typeof url === 'object') if (url.url) input = url; else return { found: false, error: 'No URL provided' }; else if (typeof url === 'string') input.url = url; else return { found: false, error: 'Invalid first argument' }; if (options) Object.keys(options).forEach((key) => input[key] = options[key]); if (/\/\/twitter.com/.test(input.url)) { let apiURL = input.url.replace('//twitter.com', '//api.vxtwitter.com'); let result = await axios.get(apiURL, {}).then((res) => res.data).catch((err) => { return { found: false, error: 'An issue occured. Make sure the twitter link is valid.' };async function twitterDL(url) { try { let form = new FormData(); form.append('q', url); form.append('lang', 'en');
let response = await fetch('https://x2twitter.com/api/ajaxSearch', { method: 'POST', body: form, headers: form.getHeaders() });
let data = await response.json();
const $ = cheerio.load(data?.data); const downloads = []; $('.dl-action a').each((index, element) => { const format = $(element).text().trim(); const url = $(element).attr('href'); downloads.push({ format, url }); }); if (!result.media_extended) return { found: false, error: 'No media found' }; let output = { found: true, type: result.media_extended[0].type, url: result.media_extended[0].url
const convertToMP3 = { url: $('.tw-middle a').attr('href') }; if (input.text) output.text = result.text; if (input.buffer) output.buffer = await axios.get(output.url, { responseType: 'arraybuffer', headers: headers }).then((res) => { return Buffer.from(res.data, 'binary'); }).catch((err) => { console.log('Error getting buffer: ', err); return undefined; }); return output;
const result = { downloads, convert_to_mp3: convertToMP3 };
const hrefArray = [];
$('a').each((index, element) => { const href = $(element).attr('href'); if (href && href !== '/' && href !== '#') { hrefArray.push(href); } });
return { result, hrefArray }; } catch (error) { return error; } else return { found: false, error: `Invalid URL: ${input.url}` };}
const app = express();184 unmodified lines if (!/https?:\/\/(www\.)?(twitter|x)\.com\/.*\/status/.test(url)) { return res.json({ message: 'Invalid URL' }); } let data = await twitterAPI(url); let data = await twitterDL(url); return res.json(data); } catch (e) { console.log(e);index.js+42-36