This repository has been archived by the owner on Oct 15, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mangainn.ts
252 lines (214 loc) · 7.64 KB
/
mangainn.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import qs from "qs";
import cheerio from "cheerio";
import {
MangaExtractorConstructorOptions,
MangaExtractorValidateResults,
MangaExtractorSearchResult,
MangaExtractorModel,
MangaExtractorChapterResult,
MangaExtractorInfoResult,
MangaExtractorChapterPagesResult,
MangaExtractorPageImageResult,
} from "./model";
import { constants, functions } from "../../util";
export const config = {
baseUrl: "https://www.mangainn.net",
searchUrl: "https://www.mangainn.net/service/advanced_search",
mangaRegex: /^https:\/\/www\.mangainn\.net\/.*/,
chapterRegex: /^https:\/\/www\.mangainn\.net\/.*?\/.*/,
defaultHeaders() {
return {
"User-Agent": constants.http.userAgent,
Referer: this.baseUrl,
"x-requested-with": "XMLHttpRequest",
};
},
};
/**
* MangaInn.net Extractor
*/
export default class MangaInn implements MangaExtractorModel {
name = "Mangainn.net";
options: MangaExtractorConstructorOptions;
constructor(options: MangaExtractorConstructorOptions) {
this.options = options;
}
/**
* Validate MangaInn.net URL
* @param url MangaInn.net URL
*/
validateURL(url: string) {
let result: MangaExtractorValidateResults = false;
if (config.mangaRegex.test(url)) result = "manga_url";
else if (config.chapterRegex.test(url)) result = "chapter_url";
return result;
}
/**
* MangaInn.net Search
* @param terms Search term
*/
async search(terms: string) {
try {
this.options.logger?.debug?.(
`(${this.name}) Search terms: ${terms}`
);
const data = await this.options.http.post(
functions.encodeURI(config.searchUrl),
qs.stringify({
type: "all",
"manga-name": terms,
status: "both",
}),
{
headers: Object.assign(config.defaultHeaders(), {
"Content-Type":
"application/x-www-form-urlencoded; charset=UTF-8",
}),
timeout: constants.http.maxTimeout,
}
);
const $ = cheerio.load(data);
this.options.logger?.debug?.(
`(${this.name}) DOM creation successful! (${config.searchUrl})`
);
const results: MangaExtractorSearchResult[] = [];
$(".row").each(function () {
const ele = $(this);
const title = ele.find(".manga-title a");
const url = title.attr("href");
const image = ele.find(".img-responsive").attr("src");
if (url) {
results.push({
title: title.text().trim(),
thumbnail: image || "",
url,
});
}
});
return results;
} catch (err) {
this.options.logger?.error?.(
`(${this.name}) Failed to scrape: ${err?.message}`
);
throw new Error(`Failed to scrape: ${err?.message}`);
}
}
/**
* Get chapter URLs from MangaInn.net URL
* @param url MangaInn.net chapter URL
*/
async getInfo(url: string) {
try {
this.options.logger?.debug?.(
`(${this.name}) Chapter links requested for: ${url}`
);
const data = await this.options.http.get(functions.encodeURI(url), {
headers: config.defaultHeaders(),
timeout: constants.http.maxTimeout,
});
const $ = cheerio.load(data);
this.options.logger?.debug?.(
`(${this.name}) DOM creation successful! (${url})`
);
const chapters: MangaExtractorChapterResult[] = [];
$(".chapter-list li a").each(function () {
const ele = $(this);
const title = ele.find(".val").text().trim();
const url = ele.attr("href");
if (url) {
const [shortTitle, chap] = title.split("-");
chapters.push({
title: shortTitle?.trim() || title,
volume: "-",
chapter: chap?.trim() || "-",
url,
});
}
});
const result: MangaExtractorInfoResult = {
title: $(".content .widget-heading").first().text().trim(),
thumbnail: $(".content img").attr("src") || "",
chapters,
};
return result;
} catch (err) {
this.options.logger?.error?.(
`(${this.name}) Failed to scrape: ${err?.message}`
);
throw new Error(`Failed to scrape: ${err?.message}`);
}
}
/**
* Get page image URLs from MangaInn.net page URL
* @param url MangaInn.net page URL
*/
async getChapterPages(url: string) {
try {
this.options.logger?.debug?.(
`(${this.name}) Chapters pages requested for: ${url}`
);
const data = await this.options.http.get(functions.encodeURI(url), {
headers: config.defaultHeaders(),
timeout: constants.http.maxTimeout,
});
const $ = cheerio.load(data);
const result: MangaExtractorChapterPagesResult = {
type: "page_urls",
entities: [],
};
$(".selectPage.pull-right select")
.first()
.find("option")
.each(function () {
const ele = $(this);
let url = ele.val();
if (typeof url === "string") {
result.entities.push({
page: ele.text().split(" ").pop()?.trim() || "-",
url,
});
}
});
this.options.logger?.debug?.(
`(${this.name}) No. of pages resolved after fetching: ${result.entities.length} (${url})`
);
return result;
} catch (err) {
this.options.logger?.error?.(
`(${this.name}) Failed to scrape: ${err?.message}`
);
throw new Error(`Failed to scrape: ${err?.message}`);
}
}
/**
* Get page image URLs from MangaInn.net page URL
* @param url MangaInn.net page URL
*/
async getPageImage(url: string) {
try {
this.options.logger?.debug?.(
`(${this.name}) Chapters pages requested for: ${url}`
);
const data = await this.options.http.get(functions.encodeURI(url), {
headers: config.defaultHeaders(),
timeout: constants.http.maxTimeout,
});
const page = data
.match(/<option.*?selected=.*?>(.*?)<\/option>/)?.[1]
?.split(" ")
.pop();
const image = data.match(
/<img src="(.*?)".*id="chapter_img">/
)?.[1];
if (!page || !image) throw new Error("No images were found");
return <MangaExtractorPageImageResult>{
image,
};
} catch (err) {
this.options.logger?.error?.(
`(${this.name}) Failed to scrape: ${err?.message}`
);
throw new Error(`Failed to scrape: ${err?.message}`);
}
}
}