File size: 2,606 Bytes
9184ced 26dfb17 9184ced |
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 |
import got from "got";
import * as cheerio from "cheerio";
import FormData from "form-data";
import { SnapSaveArgsSchema, SnapSaveSchema } from "../types/snapsave-v1.js";
import { DEFAULT_HEADERS } from "../constant.js";
import { decryptSnapSave } from "./util.js";
export async function snapsave(url) {
SnapSaveArgsSchema.parse(arguments);
const form = new FormData();
form.append("url", url);
// Fetch video data
const code = await got
.post("https://snapsave.app/action.php?lang=en", {
headers: {
...DEFAULT_HEADERS,
...form.getHeaders(),
origin: "https://snapsave.app",
referer: "https://snapsave.app/en",
},
body: form.getBuffer(),
})
.text();
const decode = decryptSnapSave(code);
const $ = cheerio.load(decode);
const results = [];
const title = $(".content > p > strong").text() || null;
const description = $("span.video-des").text() || null;
if ($("div.download-items").length) {
// Download items available
$("div.download-items").each((_, el) => {
const $el = $(el);
const url = $el.find(".download-items__btn > a").attr("href") || null;
const thumbUrl = new URL($el.find(".download-items__thumb > img").attr("src") || "");
const thumb = thumbUrl.searchParams.get("photo") || thumbUrl.toString();
if (url) {
results.push({ thumbnail: thumb, url });
}
});
} else if ($("table.table").length) {
const thumbnail = $("figure > .image > img").attr("src") || null;
$("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/gi.test(_url || "");
if (shouldRender) {
_url = /get_progressApi\('(.*?)'\)/.exec(_url || "")?.[1] || _url;
}
if (_url) {
results.push({ resolution, thumbnail, url: _url, shouldRender });
}
});
} else {
const thumbnail = $("figure > .image > img").attr("src") || null;
const url = $("div.column > a").attr("href") || null;
if (url) {
results.push({ thumbnail, url });
}
}
const result = { title, description, results };
return SnapSaveSchema.parse(result);
} |