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|
| 3 |
+
|
| 4 |
+
class nHentai {
|
| 5 |
+
hpage = async function (page) {
|
| 6 |
+
try {
|
| 7 |
+
if (!page) {
|
| 8 |
+
throw new Error('Page diperlukan!');
|
| 9 |
+
}
|
| 10 |
+
let hentaiData = {};
|
| 11 |
+
const response = await fetch(`https://nhentai.net/home?page=${page}`);
|
| 12 |
+
const $ = cheerio.load(await response.text());
|
| 13 |
+
|
| 14 |
+
$('.container').each((_, list) => {
|
| 15 |
+
const type = $(list).find('h2').text().trim()
|
| 16 |
+
hentaiData[type] = [];
|
| 17 |
+
|
| 18 |
+
$(list).find('.gallery').each((_, element) => {
|
| 19 |
+
const cover = $(element).find('img').attr('src') || $(element).find('img').attr('data-src')
|
| 20 |
+
const title = $(element).find('.caption').text().trim()
|
| 21 |
+
const url = $(element).find('a.cover').attr('href')
|
| 22 |
+
|
| 23 |
+
hentaiData[type].push({
|
| 24 |
+
title,
|
| 25 |
+
cover,
|
| 26 |
+
url: 'https://nhentai.net' + url
|
| 27 |
+
})
|
| 28 |
+
})
|
| 29 |
+
})
|
| 30 |
+
return hentaiData;
|
| 31 |
+
} catch (error) {
|
| 32 |
+
throw new Error(error.message);
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
search = async function (query, page) {
|
| 37 |
+
try {
|
| 38 |
+
if (!page || !query) {
|
| 39 |
+
throw new Error('Query & Page diperlukan!');
|
| 40 |
+
}
|
| 41 |
+
let hentaiData = [];
|
| 42 |
+
const response = await fetch(`https://nhentai.net/search/?q=${query}&page=${page}`)
|
| 43 |
+
const $ = cheerio.load(await response.text())
|
| 44 |
+
|
| 45 |
+
const galleryElements = $('.gallery');
|
| 46 |
+
for (const element of galleryElements) {
|
| 47 |
+
const title = $(element).find('.caption').text().trim();
|
| 48 |
+
const url = $(element).find('a.cover').attr('href');
|
| 49 |
+
const cover = await this.getThumb('https://nhentai.net' + url);
|
| 50 |
+
hentaiData.push({
|
| 51 |
+
title,
|
| 52 |
+
cover,
|
| 53 |
+
url: 'https://nhentai.net' + url
|
| 54 |
+
});
|
| 55 |
+
}
|
| 56 |
+
return hentaiData;
|
| 57 |
+
} catch (error) {
|
| 58 |
+
throw new Error(error.message);
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
detail = async function (url) {
|
| 63 |
+
try {
|
| 64 |
+
if (!url) {
|
| 65 |
+
throw new Error('Url diperlukan!');
|
| 66 |
+
}
|
| 67 |
+
const response = await fetch(url)
|
| 68 |
+
const $ = cheerio.load(await response.text())
|
| 69 |
+
|
| 70 |
+
const hmm = $('#cover a').attr('href');
|
| 71 |
+
const resp = await fetch(`https://nhentai.net${hmm}`);
|
| 72 |
+
const $$ = cheerio.load(await resp.text());
|
| 73 |
+
|
| 74 |
+
const cover = await this.getThumb(url);
|
| 75 |
+
|
| 76 |
+
const hentaiData = {
|
| 77 |
+
title: {
|
| 78 |
+
main: $('#info h1').text().trim(),
|
| 79 |
+
japanese: $('#info h2').text().trim()
|
| 80 |
+
},
|
| 81 |
+
id: $('#info h3').contents().not('span').text().trim(),
|
| 82 |
+
parody: $('#info a[href*="/parody/"] span.name').map((_, tag) => $(tag).text().trim()).get().join(', '),
|
| 83 |
+
tags: $('#info a[href*="/tag/"] span.name').map((_, tag) => $(tag).text().trim()).get().join(', '),
|
| 84 |
+
artists: $('#info a[href*="/artist/"] span.name').map((_, tag) => $(tag).text().trim()).get().join(', '),
|
| 85 |
+
languages: $('#info a[href*="/language/"] span.name').map((_, tag) => $(tag).text().trim()).get().join(', '),
|
| 86 |
+
categories: $('#info a[href*="/category/"] span.name').map((_, tag) => $(tag).text().trim()).get().join(', '),
|
| 87 |
+
pages: $('#info a[href*="pages"] span.name').text().trim(),
|
| 88 |
+
cover,
|
| 89 |
+
uploadDate: $('#info time').text().trim(),
|
| 90 |
+
url: url
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
return hentaiData;
|
| 94 |
+
} catch (error) {
|
| 95 |
+
throw new Error(error.message);
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
getImage = async function (url) {
|
| 100 |
+
try {
|
| 101 |
+
if (!url) {
|
| 102 |
+
throw new Error('Url diperlukan!');
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
const images = [];
|
| 106 |
+
const response = await fetch(url);
|
| 107 |
+
const $ = cheerio.load(await response.text());
|
| 108 |
+
|
| 109 |
+
const elements = $('.thumb-container').toArray();
|
| 110 |
+
for (const el of elements) {
|
| 111 |
+
const hmm = $(el).find('a.gallerythumb').attr('href');
|
| 112 |
+
if (hmm) {
|
| 113 |
+
const resp = await fetch(`https://nhentai.net${hmm}`);
|
| 114 |
+
const $$ = cheerio.load(await resp.text());
|
| 115 |
+
const image = $$('[id="image-container"] img').attr('src');
|
| 116 |
+
if (image) {
|
| 117 |
+
images.push(image);
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
return images;
|
| 123 |
+
} catch (error) {
|
| 124 |
+
throw new Error(error.message);
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
getThumb = async function (url) {
|
| 129 |
+
try {
|
| 130 |
+
if (!url) {
|
| 131 |
+
throw new Error('Url diperlukan!');
|
| 132 |
+
}
|
| 133 |
+
const response = await fetch(url)
|
| 134 |
+
const $ = cheerio.load(await response.text())
|
| 135 |
+
|
| 136 |
+
const hmm = $('#cover a').attr('href');
|
| 137 |
+
const resp = await fetch(`https://nhentai.net${hmm}`);
|
| 138 |
+
const $$ = cheerio.load(await resp.text());
|
| 139 |
+
|
| 140 |
+
return $$('#image-container img').attr('src');
|
| 141 |
+
} catch (error) {
|
| 142 |
+
throw new Error(error.message);
|
| 143 |
+
}
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
// Usage:
|
| 148 |
+
const nh = new nHentai();
|
| 149 |
+
const resp = await nh.search('loli', '1');
|
| 150 |
+
console.log(resp);
|