forked from Tsuk1ko/nhentai-api-js
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
177 lines (162 loc) · 4.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* @Author: Jindai Kirin
* @Date: 2019-02-18 16:33:33
* @Last Modified by: Jindai Kirin
* @Last Modified time: 2019-02-24 15:31:59
*/
const getHTML = require('./src/get');
const Parse = require('./src/parse');
const nHentaiGallery = require('./src/gallery');
const Qs = require('qs');
async function parseDetailsHTML(url) {
return Parse.details(await getHTML(url).catch(e => {
if (e.response.status == 404) throw new Error('Doujin Not Found');
else throw e;
}));
}
async function parseListHTML(url) {
return Parse.list(await getHTML(url).catch(e => {
if (e.response.status == 404) throw new Error('Parameter Error');
else throw e;
}));
}
/**
* nHentai API
*
* @class nHentaiAPI
*/
class nHentaiAPI {
/**
* Creates an instance of nHentaiAPI.
* @param {string} [baseURL='https://nhentai.net'] nHentai base URL
* @memberof nHentaiAPI
*/
constructor(baseURL = 'https://nhentai.net') {
this.baseURL = baseURL;
}
/**
* Get doujin details
*
* @param {string|number} id Gallery ID
* @returns nHentaiGallery object
* @memberof nHentaiAPI
*/
async g(id) {
return new nHentaiGallery(await parseDetailsHTML(`${this.baseURL}/g/${id}/`));
}
/**
* Search doujin
*
* @param {string} keyword Keyword
* @param {number} [page=1] Page num
* @param {string} [sort='date'] "date" (default) or "popular"
* @returns Doujin list object
* @memberof nHentaiAPI
*/
search(keyword, page = 1, sort = 'date') {
let query = Qs.stringify({
q: keyword,
page,
sort
});
return parseListHTML(`${this.baseURL}/search/?${query}`);
}
/**
* Get doujin list from homepage
*
* @param {number} [page=1] Page num
* @returns Doujin list object
* @memberof nHentaiAPI
*/
homepage(page = 1) {
let query = Qs.stringify({
page
});
return parseListHTML(`${this.baseURL}/?${query}`);
}
/**
* Get a random doujin
*
* @returns Doujin details object
* @memberof nHentaiAPI
*/
random() {
return parseDetailsHTML(`${this.baseURL}/random/`);
}
/**
* Get doujin list by tag
*
* @param {string} name Tag name
* @param {number} [page=1] Page num
* @param {string} [sort='date'] "date" (default) or "popular"
* @returns Doujin list object
* @memberof nHentaiAPI
*/
tag(name, page = 1, sort = 'date') {
let query = Qs.stringify({
page
});
return parseListHTML(`${this.baseURL}/tag/${name.replace(' ','-')}/${sort=='date'?'':sort}?${query}`);
}
/**
* Get doujin list by artist
*
* @param {string} name Artist name
* @param {number} [page=1] Page num
* @param {string} [sort='date'] "date" (default) or "popular"
* @returns Doujin list object
* @memberof nHentaiAPI
*/
artist(name, page = 1, sort = 'date') {
let query = Qs.stringify({
page
});
return parseListHTML(`${this.baseURL}/tag/${name.replace(' ','-')}/${sort=='date'?'':sort}?${query}`);
}
/**
* Get doujin list by character
*
* @param {string} name Character name
* @param {number} [page=1] Page num
* @param {string} [sort='date'] "date" (default) or "popular"
* @returns Doujin list object
* @memberof nHentaiAPI
*/
character(name, page = 1, sort = 'date') {
let query = Qs.stringify({
page
});
return parseListHTML(`${this.baseURL}/tag/${name.replace(' ','-')}/${sort=='date'?'':sort}?${query}`);
}
/**
* Get doujin list by parody
*
* @param {string} name Parody name
* @param {number} [page=1] Page num
* @param {string} [sort='date'] "date" (default) or "popular"
* @returns Doujin list object
* @memberof nHentaiAPI
*/
parody(name, page = 1, sort = 'date') {
let query = Qs.stringify({
page
});
return parseListHTML(`${this.baseURL}/tag/${name.replace(' ','-')}/${sort=='date'?'':sort}?${query}`);
}
/**
* Get doujin list by group
*
* @param {string} name Group name
* @param {number} [page=1] Page num
* @param {string} [sort='date'] "date" (default) or "popular"
* @returns Doujin list object
* @memberof nHentaiAPI
*/
group(name, page = 1, sort = 'date') {
let query = Qs.stringify({
page
});
return parseListHTML(`${this.baseURL}/tag/${name.replace(' ','-')}/${sort=='date'?'':sort}?${query}`);
}
}
module.exports = nHentaiAPI;