Advertisement
samiroexpikachu

gimg

Apr 26th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require('axios');
  2. const fs = require('fs');
  3. const path = require('path');
  4.  
  5. module.exports = {
  6.   config: {
  7.     name: "gimg",
  8.     author: "Samir Œ",
  9.     version: "1.0",
  10.     shortDescription: "Get images from a given search query",
  11.     longDescription: "Get images from a Google search ",
  12.     category: "Image"
  13.   },
  14.  
  15.   onStart: async function ({ api, event, args }) {
  16.     let searchQuery = args.join(' ');
  17.  
  18.     if (searchQuery) {
  19.       try {
  20.         const response = await axios.get(`https://apis-samir.onrender.com/google/imagesearch?q=${encodeURIComponent(searchQuery)}`);
  21.         const data = response.data.data;
  22.         const imgData = [];
  23.  
  24.         for (let i = 0; i < Math.min(6, data.length); i++) {
  25.           const imgResponse = await axios.get(data[i], { responseType: 'arraybuffer' });
  26.           const imgPath = path.join(__dirname, 'cache', `${i + 1}.jpg`);
  27.           await fs.promises.writeFile(imgPath, imgResponse.data);
  28.           imgData.push(fs.createReadStream(imgPath));
  29.         }
  30.  
  31.         await api.sendMessage({
  32.           attachment: imgData,
  33.           body: `Here are some images for "${searchQuery}"`
  34.         }, event.threadID, event.messageID);
  35.  
  36.       } catch (error) {
  37.         console.error("Failed to fetch or send images:", error.message);
  38.         api.sendMessage({ body: "Failed to get random images." }, event.threadID);
  39.       }
  40.     } else {
  41.       let links = [];
  42.  
  43.       for (let attachment of event.messageReply.attachments) {
  44.         links.push(attachment.url);
  45.       }
  46.  
  47.       try {
  48.         const shortLink1 = await global.utils.uploadImgbb(links[0]);
  49.         const imageUrl = shortLink1.image.url;
  50.         const response = await axios.get(`https://apis-samir.onrender.com/find?imageUrl=${imageUrl}`);
  51.         const data = response.data.data;
  52.         const imgData = [];
  53.  
  54.         for (let i = 0; i < Math.min(6, data.length); i++) {
  55.           const imgResponse = await axios.get(data[i], { responseType: 'arraybuffer' });
  56.           const imgPath = path.join(__dirname, 'cache', `${i + 1}.jpg`);
  57.           await fs.promises.writeFile(imgPath, imgResponse.data);
  58.           imgData.push(fs.createReadStream(imgPath));
  59.         }
  60.  
  61.         await api.sendMessage({
  62.           attachment: imgData,
  63.           body: `Here are some similar images`
  64.         }, event.threadID, event.messageID);
  65.  
  66.       } catch (error) {
  67.         console.error("Failed to fetch or send images:", error.message);
  68.         api.sendMessage({ body: "Failed to get random images." }, event.threadID);
  69.       }
  70.     }
  71.   }
  72. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement