import * as __ns_780 from 'node-fetch' const fetch = __ns_780.default import * as __ns_781 from 'node:crypto' const crypto = __ns_781.default import * as __ns_782 from 'node:url' const pathToFileURL = __ns_782.pathToFileURL const BASE = "https://snapsave.app"; const UA = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Mobile Safari/537.36"; function randomGa() { return `GA1.1.${Math.floor(Math.random() * 2_000_000_000)}.${Math.floor(Date.now() / 1000)}`; } function randomPhpSession() { return crypto.randomBytes(13).toString("hex").slice(0, 26); } function parseSetCookie(headers) { const cookies = []; const raw = headers.get("set-cookie"); if (raw) { cookies.push(...raw.split(/,(?=[^;,]+=)/).map((v) => v.split(";")[0].trim())); } return cookies; } function mergeCookies(...lists) { const map = new Map(); for (const list of lists) { for (const item of list) { const [key] = item.split("="); if (key) map.set(key.trim(), item.trim()); } } return [...map.values()].join("; "); } function defaultCookies() { const now = Math.floor(Date.now() / 1000); return [ `_ga=${randomGa()}`, `PHPSESSID=${randomPhpSession()}`, "__jscuActive=true", `_ga_WNPZGVDWE9=GS2.1.s${now}$o1$g0$t${now}$j60$l0$h0`, ]; } function baseHeaders(cookie = "") { return { "user-agent": UA, "sec-ch-ua": `"Google Chrome";v="147", "Not.A/Brand";v="8", "Chromium";v="147"`, "sec-ch-ua-mobile": "?1", "sec-ch-ua-platform": `"Android"`, "accept-language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7", ...(cookie ? { cookie } : {}), }; } async function initCookie() { const seed = defaultCookies().join("; "); try { const res = await fetch(`${BASE}/id/download-video-instagram`, { method: "GET", headers: { ...baseHeaders(seed), accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", "upgrade-insecure-requests": "1", }, }); const setCookies = parseSetCookie(res.headers); return mergeCookies(defaultCookies(), setCookies); } catch { return seed; } } function convertBase(value, fromBase, toBase) { const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/".split(""); const fromChars = chars.slice(0, fromBase); const toChars = chars.slice(0, toBase); let number = value.split("").reverse().reduce((acc, char, index) => { const pos = fromChars.indexOf(char); if (pos !== -1) return acc + pos * Math.pow(fromBase, index); return acc; }, 0); let out = ""; while (number > 0) { out = toChars[number % toBase] + out; number = (number - (number % toBase)) / toBase; } return out || "0"; } function decodeSnapSaveEval(html) { const match = html.match(/eval\(function\(h,u,n,t,e,r\)[\s\S]*?\}\("([^ "]+)",(\d+),"([^ "]+)",(\d+),(\d+),(\d+)\)\)/); if (!match) return html; const h = match[1], n = match[3], t = Number(match[4]), e = Number(match[5]); const delimiter = n[e]; let result = ""; for (let i = 0; i < h.length; i++) { let chunk = ""; while (i < h.length && h[i] !== delimiter) { chunk += h[i]; i++; } for (let j = 0; j < n.length; j++) chunk = chunk.replaceAll(n[j], String(j)); result += String.fromCharCode(Number(convertBase(chunk, e, 10)) - t); } return decodeURIComponent(escape(result)); } function extractResults(decoded) { const text = decoded.replaceAll('\\"', '"').replaceAll("\\/", "/").replaceAll("&", "&").replaceAll("\\n", "").replaceAll("\\t", "").trim(); const results = []; const hrefRegex = /href="([^ "]+)"/g; let match; while ((match = hrefRegex.exec(text))) { const url = match[1]; if (!url.includes("rapidcdn.app") && !url.includes("cdninstagram.com")) continue; let type = "image"; let filename = ""; try { if (url.includes("token=")) { const token = url.split("token=")[1]?.split("&")[0]; if (token) { const payloadStr = Buffer.from(token.split(".")[1] || "", "base64").toString(); const payload = JSON.parse(payloadStr); filename = payload.filename || ""; } } } catch {} const checkStr = (filename + " " + url).toLowerCase(); if (/\.mp4|video/i.test(checkStr)) { type = "video"; } else { type = "image"; } results.push({ type, url, filename }); } const unique = []; const seen = new Set(); for (const item of results) { if (seen.has(item.url)) continue; seen.add(item.url); unique.push(item); } return unique; } async function snapsave(url) { try { const cookie = await initCookie(); const body = new URLSearchParams(); body.append("url", url); const res = await fetch(`${BASE}/id/action.php?lang=id`, { method: "POST", headers: { ...baseHeaders(cookie), "content-type": "application/x-www-form-urlencoded", accept: "*/*", origin: BASE, referer: `${BASE}/id/download-video-instagram`, }, body: body.toString(), }); const raw = await res.text(); const decoded = decodeSnapSaveEval(raw); const result = extractResults(decoded); return { status: res.ok && result.length > 0, result, }; } catch (e) { return { status: false, error: e.message }; } } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { const __exports = { snapsave: snapsave } const __fns = Object.fromEntries(Object.entries(__exports).filter(([, v]) => typeof v === 'function')) const __names = Object.keys(__fns) if (__names.length === 0) { console.log('Tidak ada fungsi yang bisa di-demo. Import modul ini dari kode lain.') } else { const __search = __names.find((n) => /search|cari/i.test(n)) const __name = __search || __names[0] const __fn = __fns[__name] const __isClass = /^class\s/.test(Function.prototype.toString.call(__fn)) const __defaultArgs = (() => { const n = __name.toLowerCase() if (/emoji/.test(n)) return [{ emoji: '😀😃' }] if (/textpro|generate.*text|effect/.test(n)) return ['neon', 'Hello'] if (/image|wallpaper|search|query|anime|movie|manga|news|lyrics|wiki|quote|stalk|detail/.test(n)) return ['naruto'] if (/download|youtube|video|audio|music|spotify|tiktok|instagram|facebook|twitter|reels|terabox|media/.test(n)) return ['https://www.youtube.com/watch?v=dQw4w9WgXcQ'] if (/dork/.test(n)) return ['example.com', 'documents'] if (/tts|speech|prompt|chat|ask|translate/.test(n)) return ['Hello world'] if (/parse|format|normalize|list|features/.test(n)) return [] return ['test'] })() let __args = __defaultArgs if (process.argv.length > 2) __args = process.argv.slice(2).map((a) => { try { return JSON.parse(a) } catch { return a } }) if (__args.length === 0 && !__search && !__isClass && __fn.length > 0) { console.log('Fungsi: ' + __names.join(', ')) console.log('Contoh: node ' + "snapsave.js" + ' ' + __name + ' ') } else { ;(async () => { try { const r = __isClass ? new __fn(...__args) : await __fn(...__args) console.log(JSON.stringify(r, null, 2)) } catch (e) { console.error('Error: ' + (e && e.message ? e.message : e)) process.exitCode = 1 } })() } } }