This repository has been archived by the owner on Oct 15, 2021. It is now read-only.
/
fanfox.ts
263 lines (219 loc) · 8.19 KB
/
fanfox.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
253
254
255
256
257
258
259
260
261
262
263
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://fanfox.net",
searchUrl: (search: string) => `https://fanfox.net/search?title=${search}`,
mangaRegex: /^https:\/\/fanfox\.net\/manga.*/,
chapterRegex: /^https:\/\/fanfox\.net\/manga.*?\/\d+\.html$/,
defaultHeaders() {
return {
"User-Agent": constants.http.userAgent,
Referer: this.baseUrl,
Cookie: "isAdult=1;",
};
},
};
/**
* FanFox.net Extractor
*/
export default class FanFox implements MangaExtractorModel {
name = "FanFox.net";
options: MangaExtractorConstructorOptions;
constructor(options: MangaExtractorConstructorOptions) {
this.options = options;
}
/**
* Validate FanFox.net URL
* @param url FanFox.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;
}
/**
* FanFox.net Search
* @param terms Search term
*/
async search(terms: string) {
try {
this.options.logger?.debug?.(
`(${this.name}) Search terms: ${terms}`
);
const url = config.searchUrl(terms);
this.options.logger?.debug?.(`(${this.name}) Search URL: ${url}`);
const data = await this.options.http.get(functions.encodeURI(url), {
credentials: true,
headers: config.defaultHeaders(),
timeout: constants.http.maxTimeout,
});
const $ = cheerio.load(data);
this.options.logger?.debug?.(
`(${this.name}) DOM creation successful! (${url})`
);
const results: MangaExtractorSearchResult[] = [];
$(".line-list li").each(function () {
const ele = $(this);
const title = ele.find(".manga-list-4-item-title a");
const url = title.attr("href");
const image = ele.find("img").attr("src");
const latestChap = ele
.find(".manga-list-4-item-tip:contains('Latest Chapter:')")
.text()
.trim();
if (url) {
let append = "";
if (latestChap)
append += ` (Latest Chapter: ${latestChap.replace(
/^Latest Chapter\:/,
""
)})`;
results.push({
title: `${title.text().trim()}${append}`,
url: `${config.baseUrl}${url.trim()}`,
thumbnail: image || "",
});
}
});
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 FanFox.net URL
* @param url FanFox.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[] = [];
$("#chapterlist li a").each(function () {
const ele = $(this);
const title = ele.find(".title3").text().trim();
const url = ele.attr("href");
if (url) {
const shortTitle = title.match(/-(.*)/)?.[1];
const vol = title.match(/Vol.(\d+)/)?.[1];
const chap = title.match(/Ch.([\d.]+)/)?.[1];
chapters.push({
title: shortTitle?.trim() || title,
volume: vol?.trim() || "-",
chapter: chap?.trim() || "-",
url: `${config.baseUrl}${url.trim()}`,
});
}
});
const result: MangaExtractorInfoResult = {
title: $(".detail-info-right-title-font").text().trim(),
thumbnail: $("img.detail-info-cover-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 FanFox.net page URL
* @param url FanFox.net page URL
*/
async getChapterPages(url: string) {
try {
url = url.replace(/https?:\/\/fanfox/, "https://m.fanfox");
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: [],
};
$("select.mangaread-page")
.first()
.find("option")
.each(function () {
const ele = $(this);
let url = ele.val();
if (typeof url === "string") {
if (!url.startsWith("http")) url = `https:${url}`;
result.entities.push({
page: ele.text().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 FanFox.net page URL
* @param url FanFox.net page URL
*/
async getPageImage(url: string) {
try {
url = url.replace(/https?:\/\/fanfox/, "https://m.fanfox");
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]
?.trim();
const image = data
.match(/<img src="(.*?)".*id="image".*>/)?.[1]
?.trim();
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}`);
}
}
}