UNPKG

@xct007/frieren-scraper

Version:
60 lines (59 loc) 2.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.pinterest = void 0; const https_1 = __importDefault(require("https")); const Utils_1 = require("../Utils"); const Constant_1 = require("../Constant"); class Pinterest { static async CreateRequest(baseUrl, config) { return Utils_1.Axios.request({ url: baseUrl, ...config }).catch((e) => e?.response); } static async v1GetToken() { const { data, headers } = await Pinterest.CreateRequest(Constant_1.PinterestDownloadBaseUrls.v1, { httpsAgent: new https_1.default.Agent({ keepAlive: true }) }); const $ = (0, Utils_1.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 ${Constant_1.PinterestDownloadBaseUrls.v1}`); } const data = await Pinterest.CreateRequest(Constant_1.PinterestDownloadBaseUrls.v1 + "/downloader.php", { method: "POST", headers: { ["Cookie"]: prepare.cookie, }, withCredentials: true, data: new URLSearchParams({ url: pinUrl, token: prepare.token }), }); const $ = (0, Utils_1.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), }; } } } exports.pinterest = { v1: Pinterest.v1, };