← back to browse
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);
		}
	}
};

View raw file