@xct007/frieren-scraper
Version:
54 lines (53 loc) • 1.92 kB
JavaScript
import https from "https";
import { Axios, Cheerio } from "../Utils.js";
import { PinterestDownloadBaseUrls } from "../Constant.js";
class Pinterest {
static async CreateRequest(baseUrl, config) {
return Axios.request({ url: baseUrl, ...config }).catch((e) => e?.response);
}
static async v1GetToken() {
const { data, headers } = await Pinterest.CreateRequest(PinterestDownloadBaseUrls.v1, { httpsAgent: new https.Agent({ keepAlive: true }) });
const $ = Cheerio(data);
const token = $("#downloadForm > input[name='token']").attr("value") || "";
const cookie = headers["set-cookie"] || "";
if (token && cookie) {
return { token, cookie };
}
else {
return false;
}
}
static async v1(pinUrl) {
try {
const prepare = await Pinterest.v1GetToken();
if (typeof prepare !== "object") {
throw new Error(`Failed to retrieve token from ${PinterestDownloadBaseUrls.v1}`);
}
const data = await Pinterest.CreateRequest(PinterestDownloadBaseUrls.v1 + "/downloader.php", {
method: "POST",
headers: {
["Cookie"]: prepare.cookie,
},
withCredentials: true,
data: new URLSearchParams({ url: pinUrl, token: prepare.token }),
});
const $ = Cheerio(data.data);
const url = $("a.downloadBtn").attr("href");
if (url) {
return { url };
}
else {
throw new Error(`Failed to find imagge/video/gif source, is that valid Pinterest URL ?`);
}
}
catch (e) {
return {
error: true,
message: String(e),
};
}
}
}
export const pinterest = {
v1: Pinterest.v1,
};