UNPKG

@yeci226/nhentai-ts

Version:

A scraper for NHentai with types

278 lines (277 loc) 11.2 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __accessCheck = (obj, member, msg) => { if (!member.has(obj)) throw TypeError("Cannot " + msg); }; var __privateGet = (obj, member, getter) => { __accessCheck(obj, member, "read from private field"); return getter ? getter.call(obj) : member.get(obj); }; var __privateAdd = (obj, member, value) => { if (member.has(obj)) throw TypeError("Cannot add the same private member more than once"); member instanceof WeakSet ? member.add(obj) : member.set(obj, value); }; var __privateSet = (obj, member, value, setter) => { __accessCheck(obj, member, "write to private field"); setter ? setter.call(obj, value) : member.set(obj, value); return value; }; var NHentai_exports = {}; __export(NHentai_exports, { NHentai: () => NHentai }); module.exports = __toCommonJS(NHentai_exports); var import_axios = __toESM(require("axios")); var import_cheerio = require("cheerio"); var import_tough_cookie = require("tough-cookie"); var import_http = require("http-cookie-agent/http"); var import_Parser = require("../../Parser"); var import_constants = require("../constants"); var import_util = require("../util"); var _axios; class NHentai { /** * Constructs an instance of the NHentai class * @param _options Options of the NHentai class */ constructor(_options = { site: "https://nhentai.to" }) { this._options = _options; __privateAdd(this, _axios, void 0); /** * Gets a random doujin * @returns Info of the random doujin */ this.getRandom = async () => await __privateGet(this, _axios).get("".concat(this._options.site, "/random")).then( async ({ data }) => (0, import_Parser.parseDoujinInfo)( (0, import_cheerio.load)(data), this.getSiteName(), this._options.site.includes("net") ? await (0, import_util.getAPIGalleryPages)(__privateGet(this, _axios), data) : void 0 ) ).catch((err) => { throw new Error(err.message); }); /** * Explores the list of doujin * @param page Page number of the list * @returns The doujin list */ this.explore = async (page = 1) => { if (isNaN(page) || page < 1) page = 1; return await __privateGet(this, _axios).get("".concat(this._options.site, "?page=").concat(page)).then(({ data }) => (0, import_Parser.parseDoujinList)((0, import_cheerio.load)(data), this.getSiteName())).catch((err) => { throw new Error(err.message); }); }; /** * Searches for a doujin by a query * @param query Query of the doujin to search * @param options Options for searching * @returns The result of the search */ this.search = async (query, options) => { if (!query) throw new Error("The 'query' parameter shouldn't be undefined"); let page = 1; if ((options == null ? void 0 : options.page) && options.page > 0) page = options.page; return await __privateGet(this, _axios).get("".concat(this._options.site, "/search?q=").concat(query, "&page=").concat(page)).then((res) => { const results = (0, import_Parser.parseDoujinList)( (0, import_cheerio.load)(res.data), this.getSiteName() ); if (!results.data.length) throw new Error("No search results found"); return results; }); }; /** * Searches Tag for a doujin by a query * @param query Tag of the doujin to search * @param options Options for searching * @returns The result of the search */ this.searchWithTag = async (query, options) => { if (!query) throw new Error("The 'query' parameter shouldn't be undefined"); let page = 1; if ((options == null ? void 0 : options.page) && options.page > 0) page = options.page; return await __privateGet(this, _axios).get( "".concat(this._options.site, "/tag/").concat(query).concat(page > 1 ? "?page=".concat(page) : "") ).then((res) => { const results = (0, import_Parser.parseDoujinList)( (0, import_cheerio.load)(res.data), this.getSiteName() ); if (!results.data.length) throw new Error("No tags results found"); return results; }); }; /** * Searches artist for a doujin by a query * @param query Artist of the doujin to search * @param options Options for searching * @returns The result of the search */ this.searchWithArtist = async (query, options) => { if (!query) throw new Error("The 'query' parameter shouldn't be undefined"); let page = 1; if ((options == null ? void 0 : options.page) && options.page > 0) page = options.page; return await __privateGet(this, _axios).get( "".concat(this._options.site, "/artist/").concat(query).concat(page > 1 ? "?page=".concat(page) : "") ).then((res) => { const results = (0, import_Parser.parseDoujinList)( (0, import_cheerio.load)(res.data), this.getSiteName() ); if (!results.data.length) throw new Error("No artists results found"); return results; }); }; /** * Searches parody for a doujin by a query * @param query Parody of the doujin to search * @param options Options for searching * @returns The result of the search */ this.searchWithParody = async (query, options) => { if (!query) throw new Error("The 'query' parameter shouldn't be undefined"); let page = 1; if ((options == null ? void 0 : options.page) && options.page > 0) page = options.page; return await __privateGet(this, _axios).get( "".concat(this._options.site, "/parody/").concat(query).concat(page > 1 ? "?page=".concat(page) : "") ).then((res) => { const results = (0, import_Parser.parseDoujinList)( (0, import_cheerio.load)(res.data), this.getSiteName() ); if (!results.data.length) throw new Error("No parodies results found"); return results; }); }; /** * Searches character for a doujin by a query * @param query Character of the doujin to search * @param options Options for searching * @returns The result of the search */ this.searchWithCharacter = async (query, options) => { if (!query) throw new Error("The 'query' parameter shouldn't be undefined"); let page = 1; if ((options == null ? void 0 : options.page) && options.page > 0) page = options.page; return await __privateGet(this, _axios).get( "".concat(this._options.site, "/character/").concat(query).concat(page > 1 ? "?page=".concat(page) : "") ).then((res) => { const results = (0, import_Parser.parseDoujinList)( (0, import_cheerio.load)(res.data), this.getSiteName() ); if (!results.data.length) throw new Error("No characters results found"); return results; }); }; /** * Gets the info of a doujin by its ID * @param id ID of the doujin * @returns Info of the doujin */ this.getDoujin = async (id) => { if (!id) throw new Error("The 'id' parameter shouldn't be undefined"); const valid = await this.validate(id); if (!valid) throw new Error("Invalid doujin ID"); return await __privateGet(this, _axios).get("".concat(this._options.site, "/g/").concat(id)).then( async (res) => (0, import_Parser.parseDoujinInfo)( (0, import_cheerio.load)(res.data), this.getSiteName(), this._options.site.includes("net") ? await (0, import_util.getAPIGalleryPages)(__privateGet(this, _axios), res.data) : void 0 ) ).catch((err) => { throw new Error(err.message); }); }; /** * Validates the ID of a doujin * @param id ID of the doujin to check */ this.validate = (id) => __privateGet(this, _axios).get("".concat(this._options.site, "/g/").concat(id)).then(() => true).catch(() => false); __privateSet(this, _axios, import_axios.default); const siteName = this._options.site.replace("https://", "").replace("http://", "").replace(/\/$/, ""); if (!import_constants.sites.includes(siteName)) { this._options.site = "https://nhentai.to"; } if (!this._options.site.startsWith("https://")) this._options.site = "https://".concat(this._options.site); if (this._options.site.includes("nhentai.net") && (!this._options.cookie_value || !this._options.user_agent)) throw new Error( "Assign the ".concat(!this._options.cookie_value ? "'cookie_value'" : "'user_agent'", " in the instance of the class to use this site.") ); if (this._options.cookie_value) { const jar = new import_tough_cookie.CookieJar(); jar.setCookie(this._options.cookie_value, this._options.site); const httpsAgent = new import_http.HttpsCookieAgent({ cookies: { jar } }); __privateSet(this, _axios, import_axios.default.create({ httpsAgent })); } if (this._options.user_agent) __privateGet(this, _axios).defaults.headers.common["User-Agent"] = this._options.user_agent; } /** * Helper method to extract site name from URL */ getSiteName() { const siteName = this._options.site.replace("https://", "").replace("http://", "").replace(/\/$/, ""); if (siteName.includes("nhentai.to")) return "to"; if (siteName.includes("nhentai.net")) return "net"; if (siteName.includes("nhentai.website")) return "website"; return "to"; } } _axios = new WeakMap(); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { NHentai });