← back to browse
goat command

videosearch

No description

0
views
0
likes
0
installs

Aliases: vseaech, v

raw source
const axios = require('axios');
const fs = require('fs');
const path = require('path');

const xApi = async () => {
  const a = await axios.get(
    "https://raw.githubusercontent.com/nazrul4x/Noobs/main/Apis.json"
  );
  return a.data.api2;
};

async function downloadAndSendVideo(videoUrl, api, event) {
  try {
    const { messageID, threadID } = event;
    
    api.setMessageReaction("πŸ¦†", messageID, () => {}, true);

    const { data } = await axios.get(`${await xApi()}/ytMp4?url=${encodeURIComponent(videoUrl)}`);
    
    if (!data.downloads.data.fileUrl) {
      throw new Error("Download Link Not found!");
    }

    const downloadUrl = data.downloads.data.fileUrl;
    const fileName = `video_${Date.now()}.mp4`;
    const filePath = path.join(__dirname, fileName);

    const response = await axios({
      url: downloadUrl,
      method: 'GET',
      responseType: 'stream'
    });

    const writer = fs.createWriteStream(filePath);
    response.data.pipe(writer);

    return new Promise((resolve, reject) => {
      writer.on('finish', () => {
        let messageBody = "βœ… Here's your video";
        if (data.title) messageBody += `\nπŸ› οΈ Title: ${data.title}`;
        if (data.duration) messageBody += `\n⏱ Duration: ${data.duration}`;
        
        api.sendMessage({
          body: messageBody,
          attachment: fs.createReadStream(filePath)
        }, threadID, (err) => {
          fs.unlinkSync(filePath);
          if (err) reject(err);
          else {
            api.setMessageReaction("βœ…", messageID, () => {}, true);
            resolve();
          }
        });
      });

      writer.on('error', (err) => {
        fs.unlinkSync(filePath); 
        reject(new Error(`πŸ¦†failed: ${err.message}`));
      });
    });
  } catch (err) {
    api.setMessageReaction("❌", event.messageID, () => {}, true);
    throw err;
  }
}

module.exports.config = {
  name: "videosearch",
  aliases: ["vseaech","v"],
  version: "1.6.9",
  author: "Nazrul",
  role: 0,
  countDown: 15,
  category: "media",
  guide: {
    en: "{pn} [YouTube URL] - Download video\n{pn} [search query] - Search and download"
  }
};

module.exports.onStart = async ({ api, event, args }) => {
  const { threadID, messageID } = event;
  let query = args.join(" ");
 
  const ytRegex = /https?:\/\/(www\.)?(youtube\.com|youtu\.be)\/\S+/gi;
  let videoUrl = event.messageReply?.body?.match(ytRegex)?.[0] || query.match(ytRegex)?.[0];

  try {
    if (!videoUrl) {
      if (!query) {
        return api.sendMessage("❌ Please provide a YouTube URL or search query", threadID, messageID);
      }
      
      const searchResponse = await axios.get(`${await xApi()}/ytSearch?query=${encodeURIComponent(query)}`);
      const videos = searchResponse.data?.data;
      
      if (!videos?.length) {
        return api.sendMessage("πŸ¦† No videos found for your search", threadID, messageID);
      }
      
      videoUrl = videos[Math.floor(Math.random() * Math.min(3, videos.length))].url;
    }

    await downloadAndSendVideo(videoUrl, api, event);
  } catch (err) {
    api.sendMessage(`πŸ¦† error: ${err.message}`, threadID, messageID);
  }
};

View raw file