UNPKG

snapsave-media-downloader

Version:

Download Instagram, Facebook and TikTok media using snapsave.app downloader

144 lines (140 loc) 6.08 kB
import { load } from 'cheerio'; const facebookRegex = /^https?:\/\/(?:www\.|web\.|m\.)?facebook\.com\/(watch(\?v=|\/\?v=)[0-9]+(?!\/)|reel\/[0-9]+|[a-zA-Z0-9.\-_]+\/(videos|posts)\/[0-9]+|[0-9]+\/(videos|posts)\/[0-9]+|[a-zA-Z0-9]+\/(videos|posts)\/[0-9]+|share\/(v|r)\/[a-zA-Z0-9]+\/?)([^/?#&]+).*$|^https:\/\/fb\.watch\/[a-zA-Z0-9]+$/g; const instagramRegex = /^https?:\/\/(?:www\.)?instagram\.com\/(?:p|reel|reels|tv|stories|share)\/([^/?#&]+).*/g; const tiktokRegex = /^https?:\/\/(?:www\.|m\.|vm\.)?tiktok\.com\/(?:@[^/]+\/(?:video|photo)\/\d+|v\/\d+|t\/[\w]+|[\w]+)\/?/g; const normalizeURL = (url) => { return /^(https?:\/\/)(?!www\.)[a-z0-9]+/i.test(url) ? url.replace(/^(https?:\/\/)([^./]+\.[^./]+)(\/.*)?$/, "$1www.$2$3") : url; }; const fixThumbnail = (url) => { const toReplace = "https://snapinsta.app/photo.php?photo="; return url.includes(toReplace) ? decodeURIComponent(url.replace(toReplace, "")) : url; }; const snapsave = async (url) => { try { let decodeSnapApp = function(args) { let [h, u, n, t, e, r] = args; function decode2(d, e2, f) { const g = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/".split(""); const h2 = g.slice(0, e2); const i = g.slice(0, f); let j = d.split("").reverse().reduce(function(a, b, c) { if (h2.indexOf(b) !== -1) return a += h2.indexOf(b) * Math.pow(e2, c); }, 0); let k = ""; while (j > 0) { k = i[j % f] + k; j = (j - j % f) / f; } return k || "0"; } r = ""; for (let i = 0, len = h.length; i < len; i++) { let s = ""; while (h[i] !== n[Number(e)]) { s += h[i]; i++; } for (let j = 0; j < n.length; j++) s = s.replace(new RegExp(n[j], "g"), j.toString()); r += String.fromCharCode(decode2(s, e, 10) - t); } const fixEncoding = (str) => { const bytes = new Uint8Array(str.split("").map((char) => char.charCodeAt(0))); return new TextDecoder("utf-8").decode(bytes); }; return fixEncoding(r); }, getEncodedSnapApp = function(data2) { return data2.split("decodeURIComponent(escape(r))}(")[1].split("))")[0].split(",").map((v) => v.replace(/"/g, "").trim()); }, getDecodedSnapSave = function(data2) { return data2.split('getElementById("download-section").innerHTML = "')[1].split('"; document.getElementById("inputData").remove(); ')[0].replace(/\\(\\)?/g, ""); }, decryptSnapSave = function(data2) { return getDecodedSnapSave(decodeSnapApp(getEncodedSnapApp(data2))); }; const regexList = [facebookRegex, instagramRegex, tiktokRegex]; if (!regexList.some((regex) => url.match(regex))) return { success: false, message: "Invalid URL" }; const formData = new URLSearchParams(); formData.append("url", normalizeURL(url)); const response = await fetch("https://snapsave.app/action.php?lang=en", { method: "POST", headers: { "accept": "*/*", "content-type": "application/x-www-form-urlencoded", "origin": "https://snapsave.app", "referer": "https://snapsave.app/", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0" }, body: formData }); const html = await response.text(); const decode = decryptSnapSave(html); const $ = load(decode); const data = {}; const media = []; if ($("table.table").length || $("article.media > figure").length) { const description = $("span.video-des").text().trim(); const preview = $("article.media > figure").find("img").attr("src"); data.description = description; data.preview = preview; if ($("table.table").length) { $("tbody > tr").each((_, el) => { const $el = $(el); const $td = $el.find("td"); const resolution = $td.eq(0).text(); let _url = $td.eq(2).find("a").attr("href") || $td.eq(2).find("button").attr("onclick"); const shouldRender = /get_progressApi/ig.test(_url || ""); if (shouldRender) { _url = "https://snapsave.app" + /get_progressApi\('(.*?)'\)/.exec(_url || "")?.[1] || _url; } media.push({ resolution, ...shouldRender ? { shouldRender } : {}, url: _url, type: resolution ? "video" : "image" }); }); } else if ($("div.card").length) { $("div.card").each((_, el) => { const cardBody = $(el).find("div.card-body"); const aText = cardBody.find("a").text().trim(); const url2 = cardBody.find("a").attr("href"); const type = aText === "Download Photo" ? "image" : "video"; media.push({ url: url2, type }); }); } else { const url2 = $("a").attr("href") || $("button").attr("onclick"); const aText = $("a").text().trim(); const type = aText === "Download Photo" ? "image" : "video"; media.push({ url: url2, type }); } } else if ($("div.download-items").length) { $("div.download-items").each((_, el) => { const itemThumbnail = $(el).find("div.download-items__thumb > img").attr("src"); const itemBtn = $(el).find("div.download-items__btn"); const url2 = itemBtn.find("a").attr("href"); const spanText = itemBtn.find("span").text().trim(); const type = spanText === "Download Photo" ? "image" : "video"; media.push({ url: url2, ...type === "video" ? { thumbnail: itemThumbnail ? fixThumbnail(itemThumbnail) : void 0 } : {}, type }); }); } if (!media.length) return { success: false, message: "Blank data" }; return { success: true, data: { ...data, media } }; } catch (e) { return { success: false, message: "Something went wrong" }; } }; export { snapsave };