File size: 3,613 Bytes
2821330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const cloudscraper = require("cloudscraper");
const path = require("path");
const fs = require("fs-extra");
const axios = require("axios");

module.exports.config = {
    name: "image",
    version: "1.5",
    hasPermission: 0,
    description: "Find images from Google Search",
    credits: "Jonell Magallanes",
    usePrefix: false,
    commandCategory: "Search",
    usages: "[query]",
    cooldowns: 0,
};

module.exports.run = async function ({ api, event, args }) {
    try {
        const keySearch = args.join(" ");

        if (!keySearch.includes("-")) {
            return api.sendMessage(
                "โ›” ๐—œ๐—ป๐˜ƒ๐—ฎ๐—น๐—ถ๐—ฑ ๐—จ๐˜€๐—ฒ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n\nPlease enter the search query and number of images (1-99). Example: wallpaper -5",
                event.threadID,
                event.messageID
            );
        }

        api.sendMessage("๐Ÿ” Searching for images... Please wait.", event.threadID, event.messageID);

        const keySearchs = keySearch.substr(0, keySearch.indexOf('-')).trim();
        let numberSearch = parseInt(keySearch.split("-").pop().trim()) || 10;

        if (isNaN(numberSearch) || numberSearch < 1 || numberSearch > 10) {
            return api.sendMessage(
                "โ›” ๐—œ๐—ป๐˜ƒ๐—ฎ๐—น๐—ถ๐—ฑ ๐—ก๐˜‚๐—บ๐—ฏ๐—ฒ๐—ฟ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n\nPlease enter a valid number of images (1-99). Example: wallpaper -5",
                event.threadID,
                event.messageID
            );
        }

        const apiUrl = `https://ccprojectsapis.zetsu.xyz/api/imagesearch?title=${encodeURIComponent(keySearchs)}&count=${numberSearch}&safe=true`;
        console.log(`Fetching data from API: ${apiUrl}`);

        const res = await axios.get(apiUrl);
        const data = res.data.results;

        if (!data || data.length === 0) {
            return api.sendMessage(
                `โŒ No results found for "${keySearchs}". Try another search.`,
                event.threadID,
                event.messageID
            );
        }

        const imgData = [];

        for (let i = 0; i < Math.min(numberSearch, data.length); i++) {
            console.log(`Downloading image ${i + 1} from: ${data[i].url}`);

            let imgResponse;
            try {
                imgResponse = await cloudscraper.get({ uri: data[i].url, encoding: null });
            } catch (err) {
                console.error(`Error downloading image ${i + 1}:`, err);
                continue;
            }

            const imgPath = path.join(__dirname, "cache", `${i + 1}.jpg`);
            await fs.outputFile(imgPath, imgResponse);
            imgData.push(fs.createReadStream(imgPath));
        }

        await api.sendMessage({
            body: `๐Ÿ“ธ ๐—œ๐—บ๐—ฎ๐—ด๐—ฒ ๐—ฆ๐—ฒ๐—ฎ๐—ฟ๐—ฐ๐—ต ๐—š๐—ผ๐—ผ๐—ด๐—น๐—ฒ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n\nHere are the top ${numberSearch} results for "${keySearchs}".`,
            attachment: imgData,
        }, event.threadID, event.messageID);

        console.log(`Images successfully sent to thread ${event.threadID}`);

        await fs.remove(path.join(__dirname, "cache"));
        console.log("Cache directory cleaned up.");

    } catch (error) {
        console.error("Error fetching images:", error);
        return api.sendMessage(
            "โš ๏ธ An error occurred while fetching images. Please try again later.",
            event.threadID,
            event.messageID
        );
    }
};