nhentai.js-api
Version:
nhentai.net API
115 lines (114 loc) • 4.72 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NHSearchResults = exports.NHSort = void 0;
const jsdom_1 = require("jsdom");
var NHSort;
(function (NHSort) {
NHSort["recent"] = "recent";
NHSort["today"] = "popular-today";
NHSort["week"] = "popular-week";
NHSort["all_time"] = "popular";
})(NHSort = exports.NHSort || (exports.NHSort = {}));
class NHSearchResults {
constructor(api, query, sort = NHSort.recent) {
this.hentai = [];
this.total = 0;
this.pages = 0;
this.page = 1;
this.search = query;
this.sort = sort;
this.api = api;
this.url = `https://nhentai.net/search/?q=${encodeURIComponent(query)}&sort=${sort}&page=`;
}
lookup() {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.getPage();
this.total = +doc
.querySelector('#content h1')
.textContent.replace(/\D/g, '');
if (this.total !== 0) {
const lastNav = doc.querySelector('a.last');
if (lastNav) {
this.pages = +lastNav.getAttribute('href').split('page=')[1];
}
else {
this.pages = 1;
}
this.hentai = NHSearchResults.collectHentai(doc, this.api);
}
return this;
});
}
getPage() {
return __awaiter(this, void 0, void 0, function* () {
return (yield jsdom_1.JSDOM.fromURL(this.url + this.page)).window.document;
});
}
static collectHentai(doc, api) {
var _a, _b;
const divs = Array.from(doc.querySelectorAll('div.container div.gallery'));
const collection = [];
for (const div of divs) {
const hentai = {};
const a = div.querySelector('a.cover');
hentai.title = (_a = a.querySelector('div.caption')) === null || _a === void 0 ? void 0 : _a.textContent;
hentai.cleanTitle =
hentai.title.replace(/\([^(]+\)|\[[^\[]+\]/g, '').trim() ||
hentai.title;
hentai.url = a.getAttribute('href');
hentai.id = +hentai.url.replace(/\D/g, '') || 0;
hentai.cover = (_b = a
.querySelector('img.lazyload')) === null || _b === void 0 ? void 0 : _b.getAttribute('data-src');
const dataTags = div.getAttribute('data-tags') || '6346';
hentai.language = dataTags.includes('6346')
? 'japanese'
: dataTags.includes('12227')
? 'english'
: 'chinese'; // 29963;
hentai.fetch = () => __awaiter(this, void 0, void 0, function* () { return api.hentai(hentai.url); });
collection.push(hentai);
}
return collection;
}
next() {
return __awaiter(this, void 0, void 0, function* () {
if (this.total !== 0 && this.page < this.pages) {
this.page++;
const doc = yield this.getPage();
this.hentai = NHSearchResults.collectHentai(doc, this.api);
}
return this;
});
}
goto(page) {
return __awaiter(this, void 0, void 0, function* () {
if (this.total !== 0 && page > 0 && page <= this.pages) {
this.page = page;
const doc = yield this.getPage();
this.hentai = NHSearchResults.collectHentai(doc, this.api);
}
return this;
});
}
previous() {
return __awaiter(this, void 0, void 0, function* () {
if (this.total !== 0 && this.page > 1) {
this.page--;
const doc = yield this.getPage();
this.hentai = NHSearchResults.collectHentai(doc, this.api);
}
return this;
});
}
}
exports.NHSearchResults = NHSearchResults;
exports.default = NHSearchResults;