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); } } };