UNPKG

zyscr

Version:
183 lines (182 loc) 5.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.detail = exports.search = void 0; const Utils_1 = require("../Utils"); const Constant_1 = require("../Constant"); async function search(query) { try { const { data } = await Utils_1.Axios.request({ baseURL: Constant_1.hentaiHavenBaseUrl, method: "GET", url: "/", params: { s: query, post_type: "wp-manga", }, }).catch((e) => e?.response); if (!data) { throw new Error(`Failed to fetch data from ${Constant_1.hentaiHavenBaseUrl}`); } const $ = (0, Utils_1.Cheerio)(data); const _temp = []; $("div[role='tabpanel'] > div").each((i, e) => { const title = $(e).find("a").attr("title"); const url = $(e).find("a").attr("href"); const thumbnail = $(e).find("a > img").attr("src"); const _metadatas = []; const _metadata = $(e).find(".post-content > div"); $(_metadata).each((_i, _e) => { const _sum_title = $(_e) .find(".summary-heading") .text() .trim() .toLowerCase(); const _sum_content = $(_e) .find(".summary-content") .text() .trim(); _metadatas.push({ [_sum_title]: _sum_content, }); }); const metadata = Object.assign({}, ..._metadatas); _temp.push({ title, thumbnail, url, metadata, }); }); if (!_temp.length) { throw new Error(`Empty results "${query}"`); } return _temp; } catch (e) { return { error: true, message: String(e), }; } } exports.search = search; async function findHLSources(url, cookie) { try { const { data: _data } = await Utils_1.Axios.request({ method: "GET", url, }).catch((e) => e?.response); const $ = (0, Utils_1.Cheerio)(_data); const scripts = $("body > script[type='text/javascript']").text(); if (!scripts) { throw new Error("failed to find sources"); } const en = scripts.match(/var en = \"(.*)\";/)?.[1]; const iv = scripts.match(/var iv = \"(.*)\";/)?.[1]; const form = new FormData(); form.append("action", "zarat_get_data_player_ajax"); form.append("a", String(en)); form.append("b", String(iv)); const { data } = await Utils_1.Axios.request({ baseURL: Constant_1.hentaiHavenBaseUrl, url: "/wp-content/plugins/player-logic/api.php", method: "POST", headers: { cookie, }, data: form, }).catch((e) => e?.response); if (!data || !data.status || !(data.data && data.data.sources)) { throw new Error("failed to find HLS video sources"); } return { type: data["data"]["sources"][0]["type"], url: data["data"]["sources"][0]["src"], }; } catch (e) { return { error: String(e), }; } } async function detail(url) { try { const { data, headers } = await Utils_1.Axios.request({ method: "GET", url, }).catch((e) => e?.response); if (!data) { throw new Error(`Failed to get data from ${url}`); } const $ = (0, Utils_1.Cheerio)(data); const title = $(".post-title > h1").text().trim() || $("#chapter-heading.h4").text().trim(); if (!title) { throw new Error(`Cannot find title sources`); } const thumbnail = $("a > img").attr("src"); if (url.includes("episode-")) { const _iframe_source_url = $(".player_logic_item > iframe").attr("src"); const synopsis = $(".description-summary") .find("p") .text() .replace(/synopsis:/i, "") .trim(); const source = await findHLSources(_iframe_source_url, headers["set-cookie"]); if (source.error) { throw new Error(source.error); } return { isEpisode: true, title, thumbnail, synopsis, source, }; } const _metadatas = []; const episodes = []; $(".post-content > div").each((i, e) => { const _sum_title = $(e) .find(".summary-heading") .text() .trim() .toLowerCase() .replace(/[^\w+]/g, ""); const _sum_content = $(e).find(".summary-content").text().trim(); if (_sum_title && _sum_content) { _metadatas.push({ [_sum_title]: _sum_content, }); } }); $(".listing-chapters_wrap > ul > li").each((i, e) => { const episode = $(e).find("a").text().trim(); const release_date = $(e) .find(".chapter-release-date > i") .text(); const url = $(e).find("a").attr("href"); episodes.push({ episode, release_date, url, }); }); const metadata = Object.assign({}, ..._metadatas); return { isEpisode: false, title, thumbnail, metadata, episodes, }; } catch (e) { return { error: true, message: String(e), }; } } exports.detail = detail;