UNPKG

zyscr

Version:
117 lines (116 loc) 3.37 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) { if (!query) { return { error: true, message: "No query", }; } try { const { data } = await Utils_1.Axios.request({ baseURL: Constant_1.nhentaiBaseUrl, url: "/search", method: "GET", params: { q: query, }, }).catch((e) => e?.response); const _temp = []; const $ = (0, Utils_1.Cheerio)(data); $("div.container > div.gallery").each((i, e) => { const title = $(e).find("a > .caption").text(); const id = Number($(e) .find("a") .attr("href") .replace(/[^0-9]/g, "")); const thumbnail = $(e).find("a > img").attr("src"); _temp.push({ title, id, thumbnail, }); }); if (!_temp.length) { throw new Error(`Empty result for '${query}'`); } return _temp; } catch (e) { return { error: true, message: String(e), }; } } exports.search = search; async function detail(id) { if (!id) { return { error: true, message: "Missing input id", }; } try { const { data } = await Utils_1.Axios.request({ baseURL: Constant_1.nhentaiBaseUrl, url: "/g/" + id, method: "GET", }).catch((e) => e?.response); if (!data) { return { error: true, message: `Probably id '${id}' not exist`, }; } const $ = (0, Utils_1.Cheerio)(data); const title = $("div#info > h1").text().trim(); const images = []; $(".thumb-container > a").each((i, e) => { const _url = $(e) .find("img") .attr("data-src") .replace(/t(\.jpg|\.jpeg|\.png|\.webp|\.gif)$/, "$1"); images.push(_url); }); if (!title || !images.length) { throw new Error(`No title or images found for id ${id}`); } let metadata = {}; try { const _elementRhapsody = $("section#tags"); $(_elementRhapsody) .find(".tag-container.field-name") .each((_i, _e) => { const tags = []; const _key = $(_e).text().trim().split(/\n/)[0].toLowerCase(); $(_e) .find(".tag") .each((i, e) => { const _tag = $(e).find(".name") || $(e).find("time"); tags.push(_tag.text()); }); metadata[_key.replace(/\:.*/g, "")] = tags.join(", "); }); } catch (e) { metadata["error"] = String(e); } return { title, id, metadata, images, }; } catch (e) { return { error: true, message: String(e), }; } } exports.detail = detail;