| const axios = require("axios"); | |
| const path = require("path"); | |
| const fs = require("fs-extra"); | |
| module.exports.config = { | |
| name: "pint", | |
| version: "1.0", | |
| hasPermission: 0, | |
| description: "Finding Image from Pinterest", | |
| 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: tomozaki -5", | |
| event.threadID, | |
| event.messageID | |
| ); | |
| } | |
| api.sendMessage("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://ccprojectapis.ddns.net/api/pin?title=${keySearch}&count=${numberSearch}`; | |
| console.log(`Fetching data from API: ${apiUrl}`); | |
| const res = await axios.get(apiUrl); | |
| const data = res.data.data; | |
| if (!data || data.length === 0) { | |
| return api.sendMessage( | |
| `No results found for your query "${keySearchs}". Please try with a different query.`, | |
| event.threadID, | |
| event.messageID | |
| ); | |
| } | |
| const imgData = []; | |
| for (let i = 0; i < Math.min(numberSearch, data.length); i++) { | |
| console.log(`Fetching image ${i + 1} from URL: ${data[i]}`); | |
| const imgResponse = await axios.get(data[i], { responseType: "arraybuffer" }); | |
| const imgPath = path.join(__dirname, "cache", `${i + 1}.jpg`); | |
| await fs.outputFile(imgPath, imgResponse.data); | |
| imgData.push(fs.createReadStream(imgPath)); | |
| } | |
| await api.sendMessage({ | |
| body: `๐ธ ๐ฃ๐ถ๐ป๐๐ฒ๐ฟ๐ฒ๐๐\nโโโโโโโโโโโโโโโ\n\nHere are the top ${numberSearch} results for your query "${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 from Pinterest:", error); | |
| return api.sendMessage( | |
| "An error occurred while fetching images. Please try again later.", | |
| event.threadID, | |
| event.messageID | |
| ); | |
| } | |
| }; |