This repository was archived by the owner on Jun 18, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathnovel.js
More file actions
774 lines (683 loc) · 26.5 KB
/
Copy pathnovel.js
File metadata and controls
774 lines (683 loc) · 26.5 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
const express = require("express");
const axios = require("axios");
const cheerio = require("cheerio");
const path = require("path");
const fs = require("fs");
const { URL } = require("url");
const WebSocket = require("ws");
const { exit } = require("process");
const epub = require("epub-gen");
const { create } = require("domain");
const app = express();
const port = 3000;
app.use(express.json());
app.use(express.static("public"));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
const headers = {
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
};
// making the project locks better:
function createLock(directory, sendUpdate) {
const lockFilePath = path.join(directory, "fetch.lock");
if (fs.existsSync(lockFilePath)) {
sendUpdate(
`Another process is already running for ${directory}. Cannot start a new one.`
);
return false;
}
try {
fs.writeFileSync(lockFilePath, "locked");
sendUpdate(`Lock file created for ${directory}. Starting process...`);
return true;
} catch (err) {
// create directory if it doesn't exist
fs.mkdirSync(lockFilePath, { recursive: true });
sendUpdate(`Lock file created for ${directory}. Starting process...`);
return true;
}
}
function removeLock(directory, sendUpdate) {
const lockFilePath = path.join(directory, "fetch.lock");
if (fs.existsSync(lockFilePath)) {
fs.unlinkSync(lockFilePath);
sendUpdate(`Lock file removed for ${directory}.`);
}
}
async function fetchChapter(url, sendUpdate) {
if (url !== "javascript:;") {
let attempts = 0; // Track the number of attempts
const maxAttempts = 3; // Maximum attempts to fetch the chapter
while (attempts < maxAttempts) {
try {
sendUpdate(`Fetching chapter from URL: ${url}`); // Log the URL being fetched
const response = await axios.get(url, { headers });
const $ = cheerio.load(response.data);
// Extract chapter content based on the site structure
if (url.includes("royalroad")) {
const chapterTitle = $(
"body > div.page-container > div > div > div > div > div.row.fic-header.margin-bottom-40 > div > div.col-md-5.col-lg-6.col-md-offset-1.text-center.md-text-left > h1"
)
.text()
.trim();
const chapterContainer = $(".chapter-content");
const nextChapterLink = $(
"div.portlet-body > div.row.nav-buttons > div.col-xs-6.col-md-4.col-md-offset-4.col-lg-3.col-lg-offset-6 > a"
).attr("href");
// Log the chapter title and next chapter link
sendUpdate(
`Fetched title: "${chapterTitle}", Looking for next chapter link...`
);
if (nextChapterLink) {
const nextChapterUrl = new URL(nextChapterLink, url).toString();
const chapterText = `<h1>${chapterTitle}</h1>\n${chapterContainer.html()}`;
sendUpdate(
`Successfully fetched chapter "${chapterTitle}" and next: "${nextChapterUrl}"`
);
return [chapterText, nextChapterUrl];
} else {
const chapterText = `<h1>${chapterTitle}</h1>\n${chapterContainer.html()}`;
sendUpdate(
`Successfully fetched chapter: "${chapterTitle}" without a next link.`
);
return [chapterText, null];
}
} else if (url.includes("findnovel")) {
$(".box-notification").remove();
const chapterTitle =
$(".chapter-title").text().trim() || "Untitled Chapter";
const chapterContainer = $("#content");
const nextChapterLink = $('a[rel="next"]').attr("href");
if (!nextChapterLink) {
sendUpdate(`No next chapter found for: "${chapterTitle}"`);
return [null, null];
}
const nextChapterUrl = nextChapterLink.length
? new URL(nextChapterLink, url).toString()
: null;
const chapterText = `<h1>${chapterTitle}</h1>\n${chapterContainer.html()}`;
sendUpdate(
`Successfully fetched chapter: "${chapterTitle}" and next: "${nextChapterUrl}"`
);
return [chapterText, nextChapterUrl];
} else {
const chapterTitle =
$(".chapter-title").text().trim() || "Untitled Chapter";
const chapterContainer = $("#chapter-container");
const nextChapterLink = $('a[rel="next"]').attr("href");
if (!nextChapterLink) {
sendUpdate(`No next chapter found for: "${chapterTitle}"`);
return [null, null];
}
const nextChapterUrl = nextChapterLink.length
? new URL(nextChapterLink, url).toString()
: null;
const chapterText = `<h1>${chapterTitle}</h1>\n${chapterContainer.html()}`;
sendUpdate(
`Successfully fetched chapter: "${chapterTitle}" and next: "${nextChapterUrl}"`
);
return [chapterText, nextChapterUrl];
}
} catch (error) {
if (error.response && error.response.status === 429) {
attempts++;
sendUpdate(
`Received 429 error! Attempt ${attempts} of ${maxAttempts}. Retrying in 15 seconds...`
);
await new Promise((resolve) => setTimeout(resolve, 15000)); // Wait for 15 seconds before retrying
} else {
sendUpdate(
`Failed to fetch chapter content from ${url}: ${error.message}`
); // Log error message for other errors
return [null, null];
}
}
}
sendUpdate(`Max attempts reached for: ${url}. Exiting...`);
return [null, null]; // Return null if max attempts are exceeded
} else {
sendUpdate("Downloading Chapters Completed...");
return [null, null];
}
}
function saveChapter(content, chapterNumber, directory, sendUpdate) {
const filename = path.join(
"public",
directory,
`chapter_${chapterNumber}.html`
);
try {
// Ensure the directory exists.
if (!fs.existsSync(path.join("public", directory))) {
fs.mkdirSync(path.join("public", directory), { recursive: true });
}
fs.writeFileSync(filename, content, "utf-8");
sendUpdate(`Chapter ${chapterNumber} saved successfully in ${directory}.`);
} catch (error) {
sendUpdate(`Failed to save chapter ${chapterNumber}: ${error.message}`);
return;
}
}
async function downloadCoverImage(coverUrl, directory, sendUpdate) {
if (!coverUrl) {
sendUpdate("No cover image URL provided. Skipping cover image download.");
return;
}
try {
sendUpdate(`Downloading cover image from ${coverUrl}`);
const response = await axios.get(coverUrl, {
responseType: "arraybuffer",
headers: {
"Content-Type": "image/jpeg",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
},
});
const coverPath = path.join("public/" + directory, "cover.jpg");
fs.writeFileSync(coverPath, response.data);
sendUpdate("Cover image downloaded successfully.");
} catch (error) {
sendUpdate(`Failed to download cover image: ${error.message}`);
}
}
async function createEpub(title, author, directory, sendUpdate) {
// Step 1: Read chapter files from directory
const dirPath = path.join("public", directory);
const files = fs
.readdirSync(dirPath)
.filter((file) => file.startsWith("chapter_") && file.endsWith(".html"));
if (!files.length) {
sendUpdate("No chapter files found.");
return;
}
// Step 2: Sort chapter files
const sortedFiles = files.sort(
(a, b) => parseInt(a.split("_")[1]) - parseInt(b.split("_")[1])
);
// Step 3: Extract content and titles from chapter files
const chapters = sortedFiles.map((file, index) => {
const filePath = path.join(dirPath, file);
// content needs to remove the first h1 tag
const content = fs.readFileSync(filePath, "utf-8");
const $ = cheerio.load(content);
const chapterTitle = $("h1").first().text() || `Chapter ${index + 1}`;
// we need to remove the h1 tag and it's contents from the content
return {
title: chapterTitle,
data: content.replace(/<h1.*?>(.*?)<\/h1>/, ""),
};
});
// Step 4: Create options object
const options = {
title: title,
author: author,
output: path.join(dirPath, `${title.replace(/\s+/g, "_")}.epub`),
cover: path.join(dirPath, "cover.jpg"),
content: chapters, // Ensure this is the chapters array
};
try {
// Step 5: Generate EPUB and handle the resulting buffer
new epub(options).promise.then(() => console.log("Done"));
// remove lock
// delete /public/directory/fetch.lock after generating epub
if (fs.existsSync(path.join("public", directory, "fetch.lock"))) {
fs.unlinkSync(path.join("public", directory, "fetch.lock"));
}
sendUpdate(
`ePub created successfully: <a href="/${title}/${title}.epub" download>Download Here</a>`
);
} catch (err) {
// Handle and log errors
console.error("Failed to generate Ebook because of ", err);
sendUpdate(`Failed to create ePub: ${err.message}`);
}
}
// Utility to extract chapter number from chapter text
function extractChapterNumber(chapterText, directory) {
const files = fs.readdirSync(path.join("public", directory));
const existingChapters = files.filter(
(file) => file.startsWith("chapter_") && file.endsWith(".html")
).length;
return existingChapters + 1;
}
async function downloadChapters(
title,
author,
startUrl,
coverUrl,
sendUpdate,
skipFirstChapter = false
) {
let url = startUrl;
if (!sendUpdate) {
sendUpdate = (message) => {
console.log(message);
};
}
const directory = title.replace(/\s+/g, "_");
if (!fs.existsSync(path.join("public", directory))) {
fs.mkdirSync(path.join("public", directory));
}
// Create a lock to prevent simultaneous execution for this project
if (!createLock(path.join("public", directory), sendUpdate)) {
return; // Exit if another process is already running for this project
}
const dirPath = path.join("public", directory);
let chapterNumber = 1; // Default to start at Chapter 1
// Retrieve existing chapters sorted by chapter number
const existingChapters = fs
.readdirSync(dirPath)
.filter((file) => file.startsWith("chapter_") && file.endsWith(".html"))
.sort((a, b) => parseInt(a.split("_")[1]) - parseInt(b.split("_")[1]));
if (existingChapters.length > 0) {
const lastChapterFile = existingChapters[existingChapters.length - 1];
chapterNumber = parseInt(lastChapterFile.split("_")[1]) + 1; // Start with the next chapter number
// Read the content of the last downloaded chapter
const lastChapterContent = fs.readFileSync(
path.join(dirPath, lastChapterFile),
"utf-8"
);
const $last = cheerio.load(lastChapterContent);
const lastChapterTitle = $last("h1").first().text().trim();
sendUpdate(`Last downloaded chapter title: "${lastChapterTitle}"`);
// Fetch just the title/text from the starting URL without saving it
const [currentChapterText, nextUrl] = await fetchChapter(url, sendUpdate);
const $current = cheerio.load(currentChapterText);
const currentChapterTitle = $current("h1").first().text().trim();
// If the last chapter and current startUrl chapter titles match, skip the first chapter
if (lastChapterTitle === currentChapterTitle) {
sendUpdate(
`The chapter from startUrl "${url}" matches the last downloaded chapter. Skipping this chapter.`
);
url = nextUrl; // Move to the next chapter URL
} else {
sendUpdate(
`The chapter from startUrl "${url}" differs from the last downloaded chapter. Restarting from this chapter.`
);
// Do not change the url, proceed to download this chapter again.
chapterNumber--; // Adjust the chapter number to overwrite or continue correctly
}
}
try {
while (url) {
const booksPath = path.join(__dirname, "books.json");
const books = JSON.parse(fs.readFileSync(booksPath, "utf8"));
const book = books.find((book) => book.title === title);
if (book && url !== "javascript:;") {
book.ch1 = url;
fs.writeFileSync(booksPath, JSON.stringify(books, null, 2));
}
const [chapterText, nextUrl] = await fetchChapter(url, sendUpdate);
if (!chapterText) {
sendUpdate("Failed to fetch chapter content. Exiting...");
break;
}
const currentChapterNumber = chapterNumber;
saveChapter(chapterText, currentChapterNumber, directory, sendUpdate);
if (!nextUrl) {
sendUpdate("No next chapter found. Exiting...");
break;
}
url = nextUrl; // Proceed to the next chapter's URL
chapterNumber += 1; // Increment the chapter number correctly
}
} catch (err) {
// Handle and log errors
console.error("Failed to download chapters because of ", err);
sendUpdate(`Failed to download chapters: ${err.message}`);
} finally {
// Remove the lock
removeLock(path.join("public", directory), sendUpdate);
}
await downloadCoverImage(coverUrl, directory, sendUpdate);
await createEpub(title, author, directory, sendUpdate);
}
async function getTitlePage(url, sendUpdate) {
if (url.includes("royalroad")) {
try {
const response = await axios.get(url, { headers });
const $ = cheerio.load(response.data);
const title = $(
"div.page-content-inner > div > div.row.fic-header > div.col-md-5.col-lg-6.text-center.md-text-left.fic-title > div > h1"
)
.text()
.trim();
const author = $(
"div.page-content-inner > div > div.row.fic-header > div.col-md-5.col-lg-6.text-center.md-text-left.fic-title > div > h4 > span:nth-child(2)"
)
.text()
.trim();
sendUpdate(`Successfully fetched title page: ${title}, ${author}`);
const rootUrl = new URL(url).origin;
const status = $(
"div.page-content-inner > div > div.fiction.row > div > div.fiction-info > div.portlet.light.row > div.col-md-8 > div.margin-bottom-10 > span:nth-child(2)"
)
.text()
.trim();
if (status == "STUB") {
sendUpdate(
"This book seems to have chapters missing due to the author removing them...this could mean that they removed them in adherance to 3rd party requirements."
);
return [null, null, null, null, null, null, null];
}
const startUrl = new URL(
$(
"div.page-content-inner > div > div.row.fic-header > div.col-md-4.col-lg-3.fic-buttons.text-center.md-text-left > a"
).attr("href"),
rootUrl
).toString();
const coverUrl = $(
"body > div.page-container > div > div > div > div.page-content-inner > div > div.row.fic-header > div.col-md-3.text-center.cover-col > div > img"
).attr("src");
const chapterCount = $(
"div.page-content-inner > div > div.fiction.row > div > div.fiction-info > div:nth-child(5) > div.portlet-title > div.actions > span"
)
.text()
.replace(" Chapters", "")
.trim();
//sendUpdate(`title: ${title}, author: ${author}, url: ${url}, coverUrl: ${coverUrl}, chapterCount: ${chapterCount}, status: ${status}, startUrl: ${startUrl}`);
return [title, author, startUrl, coverUrl, chapterCount, status, url];
} catch (error) {
sendUpdate(`Failed to fetch title page: ${error}`);
return [null, null, null];
}
} else if (url.includes("novelworm")) {
try {
const response = await axios.get(url, { headers });
const $ = cheerio.load(response.data);
const title = $("h1.novel-title.text2row").text().trim();
const author = $("div.author")
.text()
.replace("Author:", "")
.replace(/\n+/g, "")
.trim();
const rootUrl = new URL(url).origin;
// startUrl is coming from: #novel > header > div.header-body.container > div.novel-info > nav > a:nth-child(1)
const startUrl = new URL(
$("div.novel-info > nav > a:nth-child(1)").attr("href"),
rootUrl
).toString();
console.log(startUrl);
const coverUrl = $("figure.cover img").attr("data-src");
const chapterCount = $("div.header-stats span strong")
.text()
.trim()
.split(" ")[0];
const status = $("div.header-stats span:last-child")
.text()
.replace("Status", "")
.trim();
//sendUpdate(`Successfully fetched title page: ${title}, ${author}, ${startUrl}, ${coverUrl}, ${chapterCount}, ${status}`);
return [title, author, startUrl, coverUrl, chapterCount, status, url];
} catch (error) {
sendUpdate(`Failed to fetch title page: ${error}`);
return [null, null, null, null, null, null, null];
}
} else {
try {
const response = await axios.get(url, { headers });
const $ = cheerio.load(response.data);
const title = $("h1.novel-title.text2row").text().trim();
const author = $("div.author")
.text()
.replace("Author:", "")
.replace(/\n+/g, "")
.trim();
const rootUrl = new URL(url).origin;
const startUrl = new URL(
$("a#readchapterbtn").attr("href"),
rootUrl
).toString();
console.log(startUrl);
const coverUrl = $("figure.cover img").attr("data-src");
const chapterCount = $("div.header-stats span strong")
.text()
.trim()
.split(" ")[0];
const status = $("div.header-stats span:last-child")
.text()
.replace("Status", "")
.trim();
//sendUpdate(`Successfully fetched title page: ${title}, ${author}, ${startUrl}, ${coverUrl}, ${chapterCount}, ${status}`);
return [title, author, startUrl, coverUrl, chapterCount, status, url];
} catch (error) {
sendUpdate(`Failed to fetch title page: ${error}`);
return [null, null, null, null, null, null];
}
}
}
app.get("/", (req, res) => {
res.render("index");
});
app.get("/books", (req, res) => {
// get books from books.json
const booksPath = path.join(__dirname, "books.json");
fs.readFile(booksPath, "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
// sort books by updated date
const books = JSON.parse(data);
books.sort((a, b) => new Date(b.updated) - new Date(a.updated));
res.status(200).json(books);
});
});
app.use("/public", express.static("public"));
app.get("/cron", (req, res) => {
const sendUpdate = (message) => {
console.log(message);
};
// This is to check books.json for any status of "ongoing" for any new chapters by looking at the ch1 url, then run downloadChapters function
// loading from a different path, make sure to update the file path
const booksPath = path.join(__dirname, "books.json");
fs.readFile(booksPath, "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
const books = JSON.parse(data);
books.forEach((book) => {
// book.status could be Ongoing or ONGOING, so let's lowercase the string.
if (book.status.toLowerCase() == "ongoing") {
console.log(`Checking ${book.title} for new chapters...`);
// count number of files in the directory (replace spaces with underscores), and if the count is less than the total chapters, then run downloadChapters
const bookTitle = book.title.replace(/\s+/g, "_");
const dirPath = path.join("public", bookTitle);
// check if directory exists
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
const files = fs
.readdirSync(dirPath)
.filter(
(file) => file.startsWith("chapter_") && file.endsWith(".html")
);
// update totalChapters in books.json
try {
// get new totalChapters from getTitlePage
getTitlePage(book.url, sendUpdate).then(
([
title,
author,
startUrl,
coverUrl,
totalChapters,
status,
url,
]) => {
// update books.json with new chapter count, just in case it does change
if (book.totalChapters != totalChapters) {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const updatedDate = `${year}-${month}-${day}`;
book.totalChapters = totalChapters;
book.updated = updatedDate;
fs.writeFileSync(booksPath, JSON.stringify(books, null, 2));
}
console.log(
`${book.title}: \n Found ${files.length} files.\n Total chapters: ${book.totalChapters}\n`
);
if (files.length < book.totalChapters) {
downloadChapters(
book.title,
book.author,
book.ch1,
book.coverUrl,
null,
true
);
} else {
console.log(`Skipping ${book.title} because it is up to date.`);
}
}
);
} catch (error) {
console.error(error);
} finally {
// remove the lock
removeLock(bookTitle, sendUpdate);
}
//downloadChapters(book.title, book.author, book.ch1, null, book.coverUrl, null);
} else {
console.log(`Skipping ${book.title} because it is not ongoing.`);
}
});
});
res.status(200).json({ message: "Cron job done" });
});
app.get("/compile/:folder", (req, res) => {
const sendUpdate = (message) => {
console.log(message);
};
const folder = req.params.folder;
// get author from books.json using folder.replace('__', ' ') as the 'title' key
const booksPath = path.join(__dirname, "books.json");
const books = JSON.parse(fs.readFileSync(booksPath, "utf8"));
const book = books.find((b) => b.title === folder.replace("_", " "));
if (!book) {
sendUpdate("Book not found in books.json.");
return;
}
const title = book.title;
const author = book.author;
createEpub(title, author, folder, sendUpdate);
res.status(200).json({ message: "Compilation started" });
});
app.post("/download", (req, res) => {
const { startUrl } = req.body;
const clientId = req.body.clientId;
const client = clients.get(clientId);
if (client) {
const sendUpdate = (message) => {
client.send(JSON.stringify({ message }));
};
sendUpdate("Grabbing title page...");
getTitlePage(startUrl, sendUpdate).then(
([title, author, ch1, coverUrl, totalChapters, status, url]) => {
if (title) {
const directory = title.replace(/\s+/g, "_"); // Replace spaces with underscores
const booksPath = path.join(__dirname, "books.json");
try {
// get new totalChapters from getTitlePage
if (!fs.existsSync(path.join("public", directory))) {
fs.mkdirSync(path.join("public", directory));
}
if (!fs.existsSync(booksPath)) {
fs.writeFileSync(booksPath, "[]");
}
const books = JSON.parse(fs.readFileSync(booksPath, "utf8"));
let existingBook = books.find((book) => book.title === title);
if (!existingBook) {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const updated = `${year}-${month}-${day}`;
books.push({
title,
author,
coverUrl,
totalChapters,
ch1,
status,
url,
updated,
});
fs.writeFileSync(booksPath, JSON.stringify(books, null, 2));
} else {
// Update ch1 if it has changed
if (existingBook.ch1 !== ch1) {
ch1 = existingBook.ch1;
}
}
const existingFiles = fs.readdirSync(
path.join("public", directory)
);
const existingChapters = existingFiles.filter(
(file) => file.startsWith("chapter_") && file.endsWith(".html")
).length;
if (existingChapters >= totalChapters) {
// Check to see if epub exists, if not compile it
if (
!fs.existsSync(path.join("public", directory, `${title}.epub`))
) {
// Confirm if cover exists, then compile epub
if (
!fs.existsSync(path.join("public", directory, "cover.jpg"))
) {
downloadCoverImage(coverUrl, directory, sendUpdate)
.then(() =>
createEpub(title, author, directory, sendUpdate)
)
.then(() => {
sendUpdate(
`Up to date. Download here: <a href="/public/${directory}/${directory}.epub">${title}</a>`
);
});
} else {
createEpub(title, author, directory, sendUpdate).then(() => {
sendUpdate(
`Up to date. Download here: <a href="/public/${directory}/${directory}.epub">${title}</a>`
);
});
}
} else {
sendUpdate(
`Up to date. Download here: <a href="/public/${directory}/${directory}.epub">${title}</a>`
);
}
return;
}
// Now proceed with downloading chapters
downloadChapters(title, author, ch1, coverUrl, sendUpdate).then(
() => {
sendUpdate("Process Complete");
}
);
} finally {
// Always remove the lock afterward, whether success or failure
removeLock(directory, sendUpdate);
}
}
}
);
}
res.status(200).json({ message: "Download started" });
});
const server = app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
const wss = new WebSocket.Server({ server });
const clients = new Map();
wss.on("connection", (ws) => {
const clientId = Date.now().toString();
clients.set(clientId, ws);
ws.send(JSON.stringify({ clientId }));
ws.on("close", () => {
clients.delete(clientId);
});
});