forked from LNReader/lnreader-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
147 lines (136 loc) · 4.35 KB
/
index.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
require("module-alias/register");
import path from "path";
import dayjs from "dayjs";
import express from "express";
import bodyParser from "body-parser";
import * as pluginApi from "./test_web/api/plugins";
import localizedFormat from "dayjs/plugin/localizedFormat";
const app = express();
const port = Number(process.argv[2]);
if (!port) {
process.exit(0);
}
const host = "localhost";
const dirname = path.join(__dirname, "..");
export const getHeaders = () => {
return pluginApi.fetchHeaders;
};
app.use((req, res, next) => {
const xHeaders = req.headers["x-custom-headers"];
if (xHeaders && typeof xHeaders === "string") {
for (const [headerName, headerValue] of Object.entries(
pluginApi.fetchHeaders
)) {
if (
typeof headerName === "string" &&
typeof headerValue === "string"
)
delete pluginApi.fetchHeaders[headerName];
}
const headers = JSON.parse(xHeaders);
for (const [headerName, headerValue] of Object.entries(headers)) {
if (
typeof headerName === "string" &&
typeof headerValue === "string"
)
pluginApi.fetchHeaders[headerName] = headerValue;
}
}
if (pluginApi.fetchHeaders.userAgent)
console.log("Custom headers: ", pluginApi.fetchHeaders);
next();
});
app.use(bodyParser.json());
app.use("/static", express.static(path.join(dirname, "test_web", "static")));
app.use("/icons", express.static(path.join(dirname, "icons")));
app.get("/all_plugins", (req, res) => {
const allPlugins = pluginApi.all_plugins();
res.json(allPlugins);
});
app.post("/filters", async (req, res) => {
const filters = await pluginApi.getFilter(req.body["pluginRequirePath"]);
res.json(filters || []);
});
app.post("/popularNovels/", async (req, res) => {
const requirePath = req.body["pluginRequirePath"];
const showLatestNovels = req.body["showLatestNovels"] || false;
const defaultFilters = await pluginApi.getFilter(requirePath);
const filters = showLatestNovels
? defaultFilters
: req.body["filters"] || defaultFilters;
const page = parseInt(req.body["page"]);
try {
const novels = await pluginApi.popularNovels(requirePath, page || 1, {
showLatestNovels,
filters,
});
res.json(novels);
} catch (err: unknown) {
res.json({ error: String(err) });
}
});
app.post("/searchNovels/", async (req, res) => {
const page = parseInt(req.body["page"]) || 1;
try {
const novels = await pluginApi.searchNovels(
req.body["pluginRequirePath"],
page,
req.body["searchTerm"]
);
res.json(novels);
} catch (err: unknown) {
res.json({ error: String(err) });
}
});
app.post("/parseNovelAndChapters/", async (req, res) => {
try {
const sourceNovel = await pluginApi.parseNovelAndChapters(
req.body["pluginRequirePath"],
req.body["novelUrl"]
);
res.json(sourceNovel);
} catch (err: unknown) {
res.json({ error: String(err) });
}
});
app.post("/parseChapter/", async (req, res) => {
try {
const chapterText = await pluginApi.parseChapter(
req.body["pluginRequirePath"],
req.body["chapterUrl"]
);
res.send(chapterText);
} catch (err: unknown) {
res.json({ error: String(err) });
}
});
app.post("/fetchImage/", async (req, res) => {
try {
const base64 = await pluginApi.fetchImage(
req.body["pluginRequirePath"],
req.body["url"]
);
res.send(base64);
} catch (err: unknown) {
res.json({ error: String(err) });
}
});
app.get("/", (req, res) => {
res.sendFile(path.join(dirname, "test_web", "index.html"));
});
app.listen(port, host, () => {
console.log(`Testing plugins web listening on http://localhost:${port}`);
});
//Dayjs localization
const language = Intl?.DateTimeFormat()?.resolvedOptions()?.locale || "en";
require("dayjs/locale/ar");
require("dayjs/locale/de");
require("dayjs/locale/es");
require("dayjs/locale/it");
require("dayjs/locale/pt");
require("dayjs/locale/ru");
require("dayjs/locale/tr");
require("dayjs/locale/uk");
require("dayjs/locale/zh");
dayjs.locale(language);
dayjs.extend(localizedFormat);