goat
command
autolink
Auto Download FB / TikTok / IG / CapCut
0
views
0
likes
0
installs
raw source
const axios = require("axios");
const fs = require("fs-extra");
const tinyurl = require("tinyurl");
const API = "https://autodl-api-y1eh.onrender.com";
module.exports = {
config: {
name: "autolink",
version: "3.0",
author: "SUJON-BOSS",
countDown: 2,
role: 0,
shortDescription: "Auto Download FB / TikTok / IG / CapCut",
longDescription: "Automatically downloads videos from supported social media links.",
category: "media"
},
onStart: async function () {},
onChat: async function ({ api, event }) {
const url = event.body;
if (!url) return;
try {
// ๐ฅ VALID LINKS CHECK
const valid =
url.includes("tiktok.com") ||
url.includes("facebook.com") ||
url.includes("fb.watch") ||
url.includes("instagram.com") ||
url.includes("capcut.com") ||
url.includes("pin.it");
if (!valid) return;
// โณ Loading message
const loading = await api.sendMessage(
"โณ Downloading... Please wait",
event.threadID
);
try {
const apiUrl =
`${API}/api/download?url=${encodeURIComponent(url)}`;
// ๐ Short URL
let shortUrl = apiUrl;
try {
shortUrl = await tinyurl.shorten(apiUrl);
} catch (e) {
console.log("TinyURL Error:", e.message);
}
// ๐ฅ CALL API
const apiRes = await axios.get(apiUrl, {
timeout: 30000
});
// ๐ฅ UNIVERSAL VIDEO PARSER
const videoUrl =
apiRes.data?.data?.video ||
apiRes.data?.video ||
apiRes.data?.url ||
(typeof apiRes.data === "string"
? apiRes.data
: null);
if (!videoUrl) {
try {
api.unsendMessage(loading.messageID);
} catch (e) {}
return api.sendMessage(
"โ Video not found!",
event.threadID
);
}
// ๐ฅ STREAM DOWNLOAD
const response = await axios({
method: "GET",
url: videoUrl,
responseType: "stream",
timeout: 30000
});
const cacheDir = __dirname + "/cache";
await fs.ensureDir(cacheDir);
const filePath =
`${cacheDir}/video_${Date.now()}.mp4`;
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
// ๐ฅ STREAM ERROR
response.data.on("error", async () => {
try {
await fs.remove(filePath);
} catch (e) {}
try {
api.unsendMessage(loading.messageID);
} catch (e) {}
api.sendMessage(
"โ Stream error occurred!",
event.threadID
);
});
writer.on("finish", async () => {
try {
if (!fs.existsSync(filePath)) {
try {
api.unsendMessage(loading.messageID);
} catch (e) {}
return api.sendMessage(
"โ Downloaded file not found!",
event.threadID
);
}
const stats = await fs.stat(filePath);
const sizeMB =
stats.size / (1024 * 1024);
// 25MB LIMIT
if (sizeMB > 25) {
await fs.remove(filePath);
try {
api.unsendMessage(
loading.messageID
);
} catch (e) {}
return api.sendMessage(
`โ Video is too large!\n๐ฆ Size: ${sizeMB.toFixed(2)} MB\nโ ๏ธ Maximum allowed: 25 MB`,
event.threadID
);
}
await api.sendMessage(
{
body:
`โ
Download Complete!
๐ค SUJON-BOSS
๐ฆ Size: ${sizeMB.toFixed(2)} MB
๐ ${shortUrl}`,
attachment:
fs.createReadStream(filePath)
},
event.threadID,
() => {
fs.remove(filePath).catch(() => {});
try {
api.unsendMessage(
loading.messageID
);
} catch (e) {}
}
);
} catch (err) {
console.log(
"Send Error:",
err.message
);
await fs.remove(filePath).catch(() => {});
try {
api.unsendMessage(
loading.messageID
);
} catch (e) {}
api.sendMessage(
"โ Failed to send downloaded video!",
event.threadID
);
}
});
writer.on("error", async (err) => {
console.log(
"File Write Error:",
err.message
);
await fs.remove(filePath).catch(() => {});
try {
api.unsendMessage(
loading.messageID
);
} catch (e) {}
api.sendMessage(
"โ File write failed!",
event.threadID
);
});
} catch (e) {
console.log("Download Error:", e.message);
try {
api.unsendMessage(loading.messageID);
} catch (err) {}
api.sendMessage(
"โ Download failed (API error)!",
event.threadID
);
}
} catch (e) {
console.log("Autolink Error:", e.message);
}
}
};