Update index.js
Browse files318 unmodified lines319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362184 unmodified lines547548549550551552553318 unmodified lines};//TWITTER APIasync function twitterAPI(url, options = {text:true}) {let input = {};if (typeof url === 'object')if (url.url)input = url;elsereturn { found: false, error: 'No URL provided' };else if (typeof url === 'string')input.url = url;elsereturn { 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.' };});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};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;}elsereturn { found: false, error: `Invalid URL: ${input.url}` };}const app = express();184 unmodified linesif (!/https?:\/\/(www\.)?(twitter|x)\.com\/.*\/status/.test(url)) {return res.json({ message: 'Invalid URL' });}let data = await twitterAPI(url);return res.json(data);} catch (e) {console.log(e);318 unmodified lines319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368184 unmodified lines553554555556557558559318 unmodified lines};//TWITTER APIasync 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 });});const convertToMP3 = {url: $('.tw-middle a').attr('href')};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;}}const app = express();184 unmodified linesif (!/https?:\/\/(www\.)?(twitter|x)\.com\/.*\/status/.test(url)) {return res.json({ message: 'Invalid URL' });}let data = await twitterDL(url);return res.json(data);} catch (e) {console.log(e);
index.js+42-36