File size: 2,787 Bytes
21c6159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { cmd } = require('../command');
const axios = require('axios');
const fs = require('fs');
const path = require('path');

// Simulated fetchJson function for API requests
const fetchJson = async (url) => {
    try {
        const response = await axios.get(url);
        return response.data;
    } catch (error) {
        console.error(`fetchJson Error: ${error.message}`);
        if (error.response) {
            console.error(`Response Status: ${error.response.status}`);
            console.error(`Response Data: ${JSON.stringify(error.response.data, null, 2)}`);
        }
        return null;
    }
};

cmd({
    pattern: "xdownload",
    react: "πŸ“₯",
    desc: "Download videos from Xvideos.",
    category: "utility",
    filename: __filename
}, async (conn, mek, m, { q, reply }) => {
    try {
        if (!q) return reply("❌ Please provide a valid Xvideos URL.");

        const apiUrl = `https://api.giftedtech.web.id/api/download/xvideosdl?apikey=gifted&url=${encodeURIComponent(q)}`;
        console.log("Requesting API with URL:", apiUrl);

        // Fetch video data
        const response = await fetchJson(apiUrl);
        if (!response || !response.result) {
            console.error("❌ Failed to fetch video. Please check the URL or try again later.");
            return reply("❌ Failed to fetch video. Please check the URL or try again later.");
        }

        const videoData = response.result;
        const videoUrl = videoData.download_url;
        const videoTitle = videoData.title.replace(/[<>:"/\\|?*]/g, ''); // Clean the title for file naming
        const videoPath = path.join(__dirname, `${videoTitle}.mp4`);

        // Download the video
        console.log("Downloading video...");
        const videoResponse = await axios({
            url: videoUrl,
            method: 'GET',
            responseType: 'stream',
        });

        // Save the video file locally
        const writer = fs.createWriteStream(videoPath);
        videoResponse.data.pipe(writer);

        await new Promise((resolve, reject) => {
            writer.on('finish', resolve);
            writer.on('error', reject);
        });

        console.log(`Video downloaded successfully: ${videoPath}`);

        // Send the video to the user
        await conn.sendMessage(m.chat, { video: fs.readFileSync(videoPath), caption: `πŸŽ₯ *Title:* ${videoData.title}\nπŸ‘€ *Views:* ${videoData.views}\nπŸ‘ *Likes:* ${videoData.likes}` });

        // Clean up the downloaded file
        fs.unlinkSync(videoPath);

        console.log("Video sent and file cleaned up.");
    } catch (error) {
        console.error("Unexpected Error:", error.message);
        return reply("❌ An error occurred while processing your request. Please try again.");
    }
});