Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
207 lines (187 loc) · 5.9 KB
/
Copy pathindex.js
File metadata and controls
207 lines (187 loc) · 5.9 KB
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
// const libgen = require('libgenesis');
const fs = require("fs");
const fp = require("fs/promises");
const https = require("https");
const app = require("express")();
const path = require("path");
const { spawn, execFile } = require("child_process");
const {
parseLibgenSearch,
downloadEPUBbooksdl,
deleteEPUB,
downloadEPUBbookms,
} = require("./helpers");
const PORT = 80;
app.get("/", (req, res, err) => {
console.log("request on /", "from", req.headers["x-forwarded-for"] || req.ip);
res.sendFile(path.join(__dirname, "./index.html"));
});
app.get("/search", async (req, res, next) => {
const search = req.query.s;
if (typeof search !== "string") return next({});
let html;
let startingIndex;
try {
const file = await fp.readFile("./index.html");
html = file.toString();
startingIndex = html.search('<tbody id="tableBody">') + 22;
} catch (err) {
console.error(
"There was an issue reading template html from fs",
err.message
);
}
if (search.length < 1) {
const modifiedHtml =
html.slice(0, startingIndex) +
"<div>Search too short</div>" +
html.slice(startingIndex);
res.contentType("text/html");
res.status(200).send(modifiedHtml);
return;
}
console.log("Search:", search);
fetch(
`https://libgen.is/fiction/?q=${search}&criteria=&language=&format=epub`
)
.then((response) => {
console.log("libgen.is returned status", response.status, "for search");
return response.arrayBuffer();
})
.then((buffer) => {
const libgenHTML = new TextDecoder("utf-8").decode(
new Uint8Array(buffer)
);
// console.log(parseLibgenSearch(html));
const bookData = parseLibgenSearch(libgenHTML);
const bookNodes = bookData.map(
({ authors, series, title, fileSize, md5 }) => {
return `
<tr>
<td>${authors.join("; ")}</td>
<td>${series}</td>
<td>${title}</td>
<td>${fileSize}</td>
<td>
<a href='/download?md5=${md5}' id="downloadButton">GET</a>
</td>
</tr>
`;
}
);
if (bookNodes.length < 1) {
const modifiedHtml =
html.slice(0, startingIndex) +
"<div>Your search returned no results</div>" +
html.slice(startingIndex);
res.contentType("text/html");
res.status(200).send(modifiedHtml);
return;
}
const modifiedHtml =
html.slice(0, startingIndex) +
bookNodes.join("") +
html.slice(startingIndex);
res.contentType("text/html");
res.status(200).send(modifiedHtml);
})
.catch((error) => {
console.error("Error:", error);
next(error);
});
});
app.get("/download", async (req, res, err) => {
const start = Date.now();
const md5 = req.query.md5;
console.log(
"download request from:",
req.headers["x-forwarded-for"] || req.ip
);
if (typeof md5 !== "string" || md5.length !== 32) return next({});
res.setHeader("Content-Disposition", 'attachment; filename=filename.epub"');
res.setHeader("Content-Type", "application/epub");
// console.log('Download:', md5);
if (!fs.existsSync(`./epub/${md5}.kepub.epub`)) {
try {
if (!fs.existsSync(`./epub/${md5}.epub`)) {
try {
await downloadEPUBbooksdl(md5);
} catch (err) {
console.log("falling back to booksms because of error", err.message);
await downloadEPUBbookms(md5);
}
execFile(
"./kepubify",
["-i", "-o", `./epub`, `./epub/${md5}.epub`],
(err, stdout, stderr) => {
if (err) {
console.error("ERROR running kepubify", err.message);
} else {
// res.setHeader('Content-Disposition', 'attachment; filename=filename.epub"');
// res.setHeader('Content-Type', 'application/epub');
res.download(path.join(__dirname, `./epub/${md5}.kepub.epub`));
deleteEPUB(md5);
}
}
);
} else {
const intervalHandle = setInterval(() => {
execFile(
"./kepubify",
["-i", "-o", `./epub`, `./epub/${md5}.epub`],
(err, stdout, stderr) => {
if (err) {
// console.error('INVALID EPUB', err.message);
} else {
clearInterval(intervalHandle);
// res.setHeader('Content-Disposition', 'attachment; filename=filename.epub"');
// res.setHeader('Content-Type', 'application/epub');
res.download(path.join(__dirname, `./epub/${md5}.kepub.epub`));
deleteEPUB(md5);
}
}
);
}, 1000);
}
// res.setHeader('Content-disposition', `attachment; filename=${md5}.kepub.epub`);
} catch (err) {
console.error(`Error: ${err.message}`);
deleteEPUB(md5);
res
.status(500)
.send(
"ERROR downloading book. you can try again but there may be an issue with download servers"
);
}
} else {
console.log("serving book from cache to:");
res.download(path.join(__dirname, `./epub/${md5}.kepub.epub`));
}
console.log("Book download processing took: ", Date.now() - start);
});
app.use((req, res, next) => {
console.log(
"404 request on endpoint",
req.url,
"from ip:",
req.headers["x-forwarded-for"] || req.ip
);
res.status(404).send("page not found");
});
app.use((err, req, res, next) => {
res.status(500).send("Internal Server Error");
});
app.listen(PORT, "0.0.0.0", () => {
console.log("listening on port: " + PORT);
});
// const parsedData = parseHTML(html);
// console.log(parsedData);
// setInterval(() => {
// fetch('http://cdn3.booksdl.org/get.php?md5=2c3809164b7d9727b23ca6a9ac654613')
// .then((res) => {
// console.log(res.status);
// })
// .catch((err) => {
// console.error(err);
// });
// }, 1500);