File size: 2,909 Bytes
7e9ddb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const axios = require('axios');
const cheerio = require('cheerio');
const crypto = require('crypto');

async function twitterDownloader(url) {
  try {
    const urlParams = new URLSearchParams(url.split('?')[1]);
    const tweetUrl = urlParams.get('url') || url;
    const decodedUrl = decodeURIComponent(tweetUrl);

    const timestamp = Math.floor(Date.now() / 1000);
    const hash = crypto.createHash('md5');
    hash.update(timestamp.toString() + 'ssstwitter');
    const tt = hash.digest('hex');

    const postData = new URLSearchParams({
      'id': decodedUrl,
      'locale': 'en',
      'tt': tt,
      'ts': timestamp.toString(),
      'source': 'form'
    });

    const { data: html } = await axios.post('https://ssstwitter.com/', postData, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'HX-Request': 'true',
        'HX-Target': 'target',
        'HX-Current-URL': 'https://ssstwitter.com/en-2',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        'Origin': 'https://ssstwitter.com',
        'Referer': 'https://ssstwitter.com/en-2'
      }
    });

    const $ = cheerio.load(html);
    const title = $('h2').first().text().trim();
    const downloads = [];

    $('.download-btn').each((i, elem) => {
      const $elem = $(elem);
      const downloadUrl = $elem.attr('href');
      const qualityText = $elem.find('span').text().trim();
      const quality = qualityText.replace('Download', '').trim();

      if (downloadUrl && downloadUrl.startsWith('http')) {
        downloads.push({
          quality: quality,
          url: downloadUrl
        });
      }
    });

    const mediaType = downloads.length > 0 ? 'video' : 'unknown';
    const hdDownload = downloads.find(d => d.quality.includes('HD')) || downloads[0];

    return {
      title: title || 'Twitter Media',
      mediaType: mediaType,
      downloads: downloads,
      hdUrl: hdDownload ? hdDownload.url : null,
      hdQuality: hdDownload ? hdDownload.quality : null
    };

  } catch (error) {
    throw new Error(error.message);
  }
}

const handler = async (req, res) => {
  try {
    const { url } = req.query;
    if (!url) {
      return res.status(400).json({
        success: false,
        error: 'Missing required parameter: url'
      });
    }
    const result = await twitterDownloader(url);
    res.json({
      author: "Herza",
      success: true,
      msg: result
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
};

module.exports = {
  name: 'Twitter DL',
  description: 'Download Twitter/X Video and Images',
  type: 'GET',
  routes: ['api/download/twitter'],
  tags: ['downloader', 'tools', 'misc'],
  parameters: ['url', 'key'],
  enabled: true,
  main: ['Downloader'],
  handler
};