Spaces:
Paused
Paused
| /* eslint-disable jsdoc/require-jsdoc */ | |
| const FormData = require("form-data") | |
| const axios = require("axios") | |
| const beautify = require("js-beautify") | |
| const cheerio = require("cheerio") | |
| const similarity = require("similarity") | |
| async function getSize(url) { | |
| try { | |
| const res = await axios.head(url) | |
| return res.headers["content-length"] ? parseInt(res.headers["content-length"]) : 0 | |
| } catch { | |
| return 0 | |
| } | |
| } | |
| function bytesToSize(bytes) { | |
| const sizes = ["B", "KB", "MB", "GB", "TB"] | |
| if (bytes == 0 || isNaN(bytes)) return "0 B" | |
| const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))) | |
| return (bytes / Math.pow(1024, i)).toFixed(1) + " " + sizes[i] | |
| } | |
| async function fbdl(url) { | |
| if (!url) return { error: true, message: "URL Required" } | |
| try { | |
| const { hostname } = new URL(url) | |
| const _sim = similarity("facebook.com", hostname) | |
| if (!(_sim >= 0.65 && hostname.includes("facebook.com"))) throw false | |
| } catch { | |
| return { error: true, message: "Invalid URL" } | |
| } | |
| const form = new FormData() | |
| form.append("url", url) | |
| try { | |
| const res = await axios.post("https://snapsave.app/action.php", form, { | |
| headers: { | |
| ...form.getHeaders(), | |
| "User-Agent": "WhatsApp/2.24.6.21", | |
| Referer: "https://snapsave.app/" | |
| } | |
| }) | |
| const script = res.data | |
| let js | |
| try { | |
| js = eval(script.replace("eval", "")) | |
| js = beautify(js).split("\n")[2] | |
| } catch { | |
| throw script | |
| } | |
| try { | |
| const html = eval(js.slice(js.indexOf("<") - 1, -1)) | |
| const $ = cheerio.load(html) | |
| const thumbnail = $(".image > img").attr("src") | |
| const video = [] | |
| for (const i of $("table > tbody > tr")) { | |
| const _$ = $(i).find.bind($(i)) | |
| const quality = _$(".video-quality").text() | |
| let url = _$("a").attr("href") | |
| if (!url) continue | |
| url = new URL(url) | |
| url.searchParams.delete("dl") | |
| url = url.toString() | |
| const size = await getSize(url) | |
| const fSize = bytesToSize(size) | |
| video.push({ resolusi: quality, url, size, fSize }) | |
| } | |
| return { status: 200, title: "", thumbnail, video } | |
| } catch (e) { | |
| if (!(e instanceof SyntaxError)) throw e | |
| return { status: 500, error: true, message: eval(js.split(" = ")[1]) } | |
| } | |
| } catch (error) { | |
| return { status: 500, error: true, message: "Failed to fetch data" } | |
| } | |
| } | |
| module.exports = fbdl |