Skip to content
This repository was archived by the owner on Mar 23, 2025. It is now read-only.

Commit a119f52

Browse files
authored
Merge pull request #2 from TheBritishAccent/master
Add nhentai plugin
2 parents b1f4416 + 9fa43a1 commit a119f52

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

plugins/nhentai/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# nhentai Plugin
2+
3+
This is the Mango plugin for [nhentai](https://nhentai.net/).
4+
5+
Note that
6+
7+
- All downloaded galleries will be placed in the `nhentai` folder in your library
8+
9+
Maintained by [@TheBritishAccent](https://github.com/TheBritishAccent).

plugins/nhentai/index.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
var pageURL;
2+
var pageCount = 0;
3+
var ended = false;
4+
var digits = 0;
5+
var imageType;
6+
var totalPages;
7+
8+
const NHENTAI_BASE_URL = 'https://nhentai.net/g/';
9+
const NHENTAI_IMAGE_URL = "https://i.nhentai.net/galleries/";
10+
11+
12+
function listChapters(url) {
13+
var html = mango.get(url).body;
14+
15+
var urlMatch = /\/g\/([a-zA-Z0-9]+)\//.exec(url);
16+
var chapterID = urlMatch[1];
17+
18+
var chapterTitleNode = mango.css(html, '.title')[0];
19+
20+
if (!chapterTitleNode)
21+
mango.raise("Failed to get gallery title");
22+
23+
var chapterTitle = mango.text(chapterTitleNode);
24+
25+
var chapters = [{
26+
id: chapterID,
27+
title: chapterTitle
28+
}];
29+
return JSON.stringify({
30+
chapters: chapters,
31+
title: 'nhentai'
32+
});
33+
}
34+
35+
function selectChapter(id) {
36+
var url = NHENTAI_BASE_URL + id + '/';
37+
var html = mango.get(url).body;
38+
39+
var chapterTitleNode = mango.css(html, '.title')[0];
40+
41+
if (!chapterTitleNode) {
42+
mango.raise("Failed to get gallery title");
43+
}
44+
45+
var chapterTitle = mango.text(chapterTitleNode);
46+
47+
var pages = 0;
48+
try {
49+
var pagesSpan = mango.css(html, '#tags div:nth-last-child(2) span.name')[0];
50+
if (!pagesSpan) {
51+
throw new Error();
52+
}
53+
var lengthText = mango.text(pagesSpan);
54+
pages = parseInt(lengthText);
55+
} catch (e) {
56+
mango.raise("Failed to get page count");
57+
}
58+
59+
var firstPageATag = mango.css(html, 'div#thumbnail-container img.lazyload')[0];
60+
if (!firstPageATag) {
61+
mango.raise("Failed to get URL to the first page");
62+
}
63+
64+
uglyPageURL = mango.attribute(firstPageATag, 'data-src');
65+
var pageURLMatch = /\/galleries\/([0-9]+)\/.*?\.(\w*)/.exec(uglyPageURL);
66+
67+
pageURL = NHENTAI_IMAGE_URL + pageURLMatch[1] + "/";
68+
imageType = pageURLMatch[2];
69+
70+
ended = false;
71+
pageCount = 0;
72+
totalPages = pages;
73+
digits = Math.floor(Math.log10(pages)) + 1;
74+
75+
return JSON.stringify({
76+
title: chapterTitle,
77+
pages: pages
78+
});
79+
}
80+
81+
function nextPage() {
82+
if (ended) {
83+
return JSON.stringify({});
84+
}
85+
86+
pageCount += 1;
87+
88+
if (pageCount === totalPages) {
89+
ended = true;
90+
}
91+
92+
var url = pageURL + pageCount + "." + imageType;
93+
94+
// Not all galleries have uniform file types
95+
// Post should return 405 if path exists, 404 otherwise.
96+
if (mango.post(url, "").status_code == 404) {
97+
url = pageURL + pageCount + "." + (imageType == "png" ? "jpg" : "png");
98+
}
99+
100+
var filename = pad(pageCount, digits) + '.' + imageType;
101+
102+
return JSON.stringify({
103+
url: url,
104+
filename: filename
105+
});
106+
}
107+
108+
// https://stackoverflow.com/a/10073788
109+
function pad(n, width, z) {
110+
z = z || '0';
111+
n = n + '';
112+
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
113+
}

plugins/nhentai/info.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "nhentai",
3+
"title": "nhentai",
4+
"author": "TheBritishAccent - [email protected]",
5+
"version": "v1.0",
6+
"placeholder": "nhentai Gallery URL",
7+
"wait_seconds": 1
8+
}

0 commit comments

Comments
 (0)