File size: 2,932 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
104
105
106
107
108
109
110
const axios = require('axios');

async function fbdl(url) {
  if (!url) {
    return {
      author: "Herza",
      success: false,
      error: "URL is required"
    };
  }

  try {
    const verifyResponse = await axios.post('https://fdownloader.net/api/userverify', 
      `url=${encodeURIComponent(url)}`,
      {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
          'Accept': '*/*',
          'X-Requested-With': 'XMLHttpRequest'
        }
      }
    );

    if (!verifyResponse.data.success) {
      return {
        author: "Herza",
        success: false,
        error: 'User verification failed'
      };
    }

    const token = verifyResponse.data.token;
    const currentTime = Math.floor(Date.now() / 1000);
    const k_exp = currentTime + 300;

    const searchResponse = await axios.post('https://v3.fdownloader.net/api/ajaxSearch',
      `k_exp=${k_exp}&k_token=${token.split('.')[2]}&q=${encodeURIComponent(url)}&lang=en&web=fdownloader.net&v=v2&w=&cftoken=${token}`,
      {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
          'Accept': '*/*'
        }
      }
    );

    const htmlData = searchResponse.data.data;
    const titleMatch = htmlData.match(/<h3>(.+?)<\/h3>/);
    const durationMatch = htmlData.match(/<p>(\d+:\d+)<\/p>/);
    const thumbnailMatch = htmlData.match(/src="(https:\/\/scontent[^"]+)"/);

    const hdMatch = htmlData.match(/href="(https:\/\/dl\.snapcdn\.app\/download\?token=[^"]+)"[^>]*title="Download 720p \(HD\)"/);
    const sdMatch = htmlData.match(/href="(https:\/\/dl\.snapcdn\.app\/download\?token=[^"]+)"[^>]*title="Download 360p \(SD\)"/);

    return {
      author: 'Herza',
      success: true,
      data: {
        title: titleMatch ? titleMatch[1] : 'Facebook Video',
        duration: durationMatch ? durationMatch[1] : '0:00',
        thumbnail: thumbnailMatch ? thumbnailMatch[1].replace(/&amp;/g, '&') : null,
        downloads: {
          hd: hdMatch ? hdMatch[1] : null,
          sd: sdMatch ? sdMatch[1] : null
        }
      }
    };
  } catch (error) {
    return {
      author: 'Herza',
      success: false,
      error: error.message
    };
  }
}

const handler = async (req, res) => {
  try {
    const { url } = req.query;

    if (!url) {
      return res.status(400).json({
        author: "Herza",
        success: false,
        error: 'Missing required parameter: url'
      });
    }

    const result = await fbdl(url);
    res.json(result);

  } catch (error) {
    res.status(500).json({
      author: "Herza",
      success: false,
      error: error.message
    });
  }
};

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