← back to browse
goat command

alldl

Download video from any supported URL using the all-downloader API

0
views
0
likes
0
installs

Aliases: allvideo, downloader

raw source
const axios = require("axios");
const fs = require("fs-extra");
const path = require("path");
const {
  MAX_FB_SIZE,
  fetchDownloaderInfo,
  chooseBestVideoFormat,
  chooseBestAudioFormat,
  downloadToFile,
} = require("../../includes/allDownloader");

const CACHE_DIR = path.join(__dirname, "cache");
fs.ensureDirSync(CACHE_DIR);

function sanitizeFileName(name) {
  return String(name || "file")
    .replace(/[\/\\?%*:|"<>]/g, "-")
    .replace(/\s+/g, " ")
    .trim()
    .slice(0, 100);
}

module.exports.config = {
  name: "alldl",
  version: "1.0.0",
  aliases: ["allvideo", "downloader"],
  author: "π“†©π‘…π‘œπ’·π’Ύπ“ƒπ“†ͺ π’œπ“π’Ύ",
  role: 0,
  description:
    "Download video from any supported URL using the all-downloader API",
  category: "media",
  usages: "{pn} <video link>",
  countDown: 5,
  usePrefix: true,
};

module.exports.onStart = async ({ api, event, args }) => {
  try {
    if (!args.length) {
      return api.sendMessage(
        "❌ Please provide a video link. Example: alldl https://example.com/video",
        event.threadID,
        event.messageID,
      );
    }

    let type = "video";
    if (args[0].toLowerCase() === "audio") {
      type = "audio";
      args.shift();
    } else if (args[0].toLowerCase() === "video") {
      args.shift();
    }

    const url = args.join(" ").trim();
    if (!url) {
      return api.sendMessage(
        "❌ Please provide a valid URL after the command.",
        event.threadID,
        event.messageID,
      );
    }

    const info = await fetchDownloaderInfo(url);
    const format =
      type === "audio"
        ? await chooseBestAudioFormat(info.formats)
        : await chooseBestVideoFormat(info.formats);

    if (!format) {
      return api.sendMessage(
        "❌ No downloadable video format found for this URL.",
        event.threadID,
        event.messageID,
      );
    }

    if (format.size !== null && format.size > MAX_FB_SIZE) {
      return api.sendMessage(
        `⚠️ Video is larger than 25MB and cannot be sent directly on Facebook.
You can still download it here:\n${format.url}`,
        event.threadID,
        event.messageID,
      );
    }

    const ext = String(format.ext || "mp4").replace(/[^a-z0-9]+/gi, "").toLowerCase() || "mp4";
    const fileName = `${sanitizeFileName(info.title || "download")}.${ext}`;
    const filePath = path.join(CACHE_DIR, `${Date.now()}_${fileName}`);

    await downloadToFile(format.url, filePath);

    const fileStat = await fs.stat(filePath);
    if (fileStat.size > MAX_FB_SIZE) {
      await fs.remove(filePath);
      return api.sendMessage(
        `⚠️ Downloaded video exceeds Facebook size limit of 25MB. Use this link instead:\n${format.url}`,
        event.threadID,
        event.messageID,
      );
    }

    await api.sendMessage(
      {
        body: `🎬 ${info.title || "Video Download"}`,
        attachment: fs.createReadStream(filePath),
      },
      event.threadID,
      () => fs.remove(filePath).catch(() => {}),
      event.messageID,
    );
  } catch (e) {
    console.error("[alldl]", e.message);
    return api.sendMessage(
      `❌ Error downloading file: ${e.message}`,
      event.threadID,
      event.messageID,
    );
  }
};

View raw file