File size: 2,434 Bytes
d145b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ed6900
d145b59
 
3ed6900
d145b59
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* 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