ilhamdev commited on
Commit
ac7b6ab
·
verified ·
1 Parent(s): 9f5c833

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +42 -36
index.js CHANGED
@@ -319,44 +319,50 @@ const youtubedl = async (url) => {
319
  };
320
 
321
  //TWITTER API
322
- async function twitterAPI(url, options = {text:true}) {
323
- let input = {};
324
- if (typeof url === 'object')
325
- if (url.url)
326
- input = url;
327
- else
328
- return { found: false, error: 'No URL provided' };
329
- else if (typeof url === 'string')
330
- input.url = url;
331
- else
332
- return { found: false, error: 'Invalid first argument' };
333
- if (options)
334
- Object.keys(options).forEach((key) => input[key] = options[key]);
335
- if (/\/\/twitter.com/.test(input.url)) {
336
- let apiURL = input.url.replace('//twitter.com', '//api.vxtwitter.com');
337
- let result = await axios.get(apiURL, {}).then((res) => res.data).catch((err) => {
338
- return { found: false, error: 'An issue occured. Make sure the twitter link is valid.' };
 
 
 
339
  });
340
- if (!result.media_extended)
341
- return { found: false, error: 'No media found' };
342
- let output = {
343
- found: true,
344
- type: result.media_extended[0].type,
345
- url: result.media_extended[0].url
346
  };
347
- if (input.text)
348
- output.text = result.text;
349
- if (input.buffer)
350
- output.buffer = await axios.get(output.url, { responseType: 'arraybuffer', headers: headers }).then((res) => {
351
- return Buffer.from(res.data, 'binary');
352
- }).catch((err) => {
353
- console.log('Error getting buffer: ', err);
354
- return undefined;
355
- });
356
- return output;
 
 
 
 
 
 
 
 
357
  }
358
- else
359
- return { found: false, error: `Invalid URL: ${input.url}` };
360
  }
361
 
362
  const app = express();
@@ -547,7 +553,7 @@ app.all('/', (req, res) => {
547
  if (!/https?:\/\/(www\.)?(twitter|x)\.com\/.*\/status/.test(url)) {
548
  return res.json({ message: 'Invalid URL' });
549
  }
550
- let data = await twitterAPI(url);
551
  return res.json(data);
552
  } catch (e) {
553
  console.log(e);
 
319
  };
320
 
321
  //TWITTER API
322
+ async function twitterDL(url) {
323
+ try {
324
+ let form = new FormData();
325
+ form.append('q', url);
326
+ form.append('lang', 'en');
327
+
328
+ let response = await fetch('https://x2twitter.com/api/ajaxSearch', {
329
+ method: 'POST',
330
+ body: form,
331
+ headers: form.getHeaders()
332
+ });
333
+
334
+ let data = await response.json();
335
+
336
+ const $ = cheerio.load(data?.data);
337
+ const downloads = [];
338
+ $('.dl-action a').each((index, element) => {
339
+ const format = $(element).text().trim();
340
+ const url = $(element).attr('href');
341
+ downloads.push({ format, url });
342
  });
343
+
344
+ const convertToMP3 = {
345
+ url: $('.tw-middle a').attr('href')
 
 
 
346
  };
347
+
348
+ const result = {
349
+ downloads,
350
+ convert_to_mp3: convertToMP3
351
+ };
352
+
353
+ const hrefArray = [];
354
+
355
+ $('a').each((index, element) => {
356
+ const href = $(element).attr('href');
357
+ if (href && href !== '/' && href !== '#') {
358
+ hrefArray.push(href);
359
+ }
360
+ });
361
+
362
+ return { result, hrefArray };
363
+ } catch (error) {
364
+ return error;
365
  }
 
 
366
  }
367
 
368
  const app = express();
 
553
  if (!/https?:\/\/(www\.)?(twitter|x)\.com\/.*\/status/.test(url)) {
554
  return res.json({ message: 'Invalid URL' });
555
  }
556
+ let data = await twitterDL(url);
557
  return res.json(data);
558
  } catch (e) {
559
  console.log(e);