-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
620 lines (530 loc) · 15.8 KB
/
app.js
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
/*native node modules*/
const https = require("https");
const fs = require("fs");
const EventEmitter = require("events");
/*express modules*/
const express = require("express");
const parser = require("body-parser");
/*cheerio modules*/
const cheerio = require("cheerio"); //needs lot of memory 3mb to 3.5mb
/*Custom modules*/
const session = require ('./session.js');
const logger = require('./logger');
/*Environment Variables*/
const PAGE_REQUEST_INTERVAL = process.env.PAGE_REQUEST_INTERVAL || 3000;
const PORT = process.env.PORT || 3000;
const PATH = __dirname;
const SITES_JSON_PATH = PATH + "/sites.json";
const PUBLIC_PATH = PATH + "/public";
const DOWNLOADS_PATH = PATH + "/downloads"; /*ONLY FOR WRITING ON SERVER DISK */
const DEBUG_PATH = PATH + "/debug";
/*DEFAULT SETTINGS*/
const NUM_CHAPTERS = 25;
const PING_INTERVAL = 1 * 60 * 1000; //in milliseconds 2x of client side ping rate
/*JSON Data for default site values*/
const sites = [
{
id: 0,
baseURL: "https://novelbuddy.com",
nextBtn: "#btn-next",
readChapterBtn: "#readchapterbtn",
},
];
/*Constants*/
const siteMap = new Map();
const job = new EventEmitter();
const app = express();
/*Content-Type from client is application/x-www-form-urlencoded*/
app.use(parser.urlencoded({ extended: true }));
/*resource files*/
app.use(express.static(PUBLIC_PATH));
/*GET request on page load, root page*/
app.get("/", function (reqClient, resClient) {
resClient.sendFile(PATH + "/index.html");
});
/*GET request on clicking twitter link*/
app.get("/twitter", function (req, res) {
res.redirect("https://twitter.com/VsSudarshan");
});
function generateToken(){
let token = session.generateToken();
logger.log("\nSession token: " + token);
return token;
}
/*POST request on page load, generate unique session token*/
app.post("/start", (req, res) => {
res.send(generateToken());
});
/*POST request on page unload*/
app.post("/stop", (req, res) => {
if (
req.body.token &&
session.isSession(req.body.token) &&
session.getSession(req.body.token).nextLink
) {
logger.log("\n\n Stop request (@" + reqNovel.body.token + ")");
endProcess(req.body.token);
}
res.send();
});
/*POST request to keep session alive*/
app.post("/ping", (req, res) => {
logger.log("\nPING " + req.body.token);
if (req.body.token && session.isSession(req.body.token)) {
session.getSession(req.body.token).lastActive = Date.now();
res.send("200");
} else {
res.send("406");
}
});
/*on form submit, post request*/
app.post("/novel", function (reqNovel, resNovel) {
if (!reqNovel.body.token || session.isSession(reqNovel.body.token)) {
logger.log("Invalid session. Access Denied. (@" + reqNovel.body.token + ")");
endProcess(reqNovel.body.token);
resNovel.send("Invalid session. Access Denied.");
return;
}
var sessionData = {
token: reqNovel.body.token,
lastActive: Date.now(),
hasEnded: false,
fileName: "",
writeStream:resNovel || null, /* SET TO NULL for server disk writing version */
currentLink: reqNovel.body.nLink || "",
nextLink: "",
title: "",
author: [],
summary: "",
chapter: { num: 0, name: "", content: "" },
numOfChapters: reqNovel.body.nChapter || NUM_CHAPTERS,
errorTick: 0,
};
let siteData = getSiteData(sessionData.currentLink);
if (siteData) {
logger.log(
"\n\n\nRequested URL: (@ " +
sessionData.token +
"): " +
sessionData.currentLink
);
logger.log(
"\nNumber of Chapters: (@ " +
sessionData.token +
"): " +
sessionData.numOfChapters
);
session.addSession(sessionData);
requestPage(sessionData, siteData, 0);
} else {
logger.log("\n\n\nERROR: Invalid URL (@ " + sessionData.token + ")");
resNovel.end("Invalid URL");
}
sessionData.writeStream.once("error", (err) => {
logger.log(
"\n\n\nStream Write Error (@ " + sessionData.token + "): " + err.message
);
sessionData.errorTick = 11;
});
});
function endProcess(token) {
//final clean up and session end
if (!token) return;
let sessionData = session.getSession(token);
sessionData.hasEnded = true;
let wait = 3000;
logger.log(
"Wait " +
wait / 1000 +
" seconds for write processes to end. (" +
sessionData.token +
")"
);
setTimeout(() => {
logger.log("\n\n\nSession Ended: (" + sessionData.token + ")");
sessionData.writeStream.removeAllListeners("error");
sessionData.writeStream.end();
sessionData.writeStream.send(); /*THIS LINE IS ONLY FOR WRITE ON CLIENT DISK */
sessionData = null;
session.endSession(token);
}, wait);
}
/*returns siteData from siteMap`*/
function getSiteData(link) {
return siteMap.get(link.split("/")[2]);
}
/*website crawler logic*/
function requestPage(sessionData, siteData, chapterCount) {
if (
sessionData.hasEnded ||
Date.now() - sessionData.lastActive >= PING_INTERVAL
) {
// inactive threshold
endProcess(sessionData.token);
return;
}
if (sessionData.errorTick > 10) {
logger.log(
"\n\n\nCannot resolve errors. Process stopped. (@ " +
sessionData.token +
")"
);
endProcess(sessionData.token);
return;
}
var htmlData = [];
var $;
var found = false;
logger.log(
"\n\n\nCurrent Link (@ " +
sessionData.token +
"): " +
sessionData.currentLink
);
https
.get(sessionData.currentLink, (res) => {
if (res.statusCode !== 200) {
logger.log(
"\n\n\nResponse code from URL (@ " +
sessionData.token +
"): " +
res.statusCode
);
logger.log("\nMessage (@ " + sessionData.token + "): " + res.statusMessage);
logger.log("\nProcess stopped.");
res.resume();
endProcess(sessionData.token);
return;
}
res
.on("data", (data) => {
htmlData.push(data); //this is better for cheerio instead of concatenation
})
.once("error", (e) => {
logger.log(
"\n\n\nHTTPS Response Stream Error (@ " +
sessionData.token +
"): " +
e.message
);
logger.log("\nProcess stopped. (@ " + sessionData.token + ")");
endProcess(sessionData.token);
});
res.once("end", () => {
if (!res.complete)
logger.log(
"\n\n\nPartial data from URL (@ " +
sessionData.token +
"): " +
sessionData.currentLink +
"\nTrying to continue."
);
$ = cheerio.load(htmlData.join(""));
found = isChapterAndUpdateNext($, sessionData, siteData);
logger.log(
"Next link from chapter (@ " +
sessionData.token +
"): " +
sessionData.nextLink
);
if (found && !sessionData.hasEnded) {
found = getChapterAndWriteToFile($, sessionData, siteData);
} else {
found = novelInfoAndUpdateNext($, sessionData, siteData);
logger.log(
"Next link from main page (@ " +
sessionData.token +
"): " +
sessionData.nextLink
);
}
if (
sessionData.numOfChapters &&
++chapterCount > sessionData.numOfChapters
) {
logger.log("\nProcess complete. (@ " + sessionData.token + ")");
endProcess(sessionData.token);
return;
}
if (!found || sessionData.errorTick > 10 || sessionData.hasEnded) {
logger.log("Aborted. (@ " + sessionData.token + ")");
endProcess(sessionData.token);
return;
}
if (sessionData.nextLink) {
sessionData.currentLink = siteData.baseURL + sessionData.nextLink;
logger.log(
"Next page (@ " +
sessionData.token +
"): " +
sessionData.currentLink
);
setTimeout(function () {
requestPage(sessionData, siteData, chapterCount);
}, PAGE_REQUEST_INTERVAL);
} else {
logger.log("\nNo more links. (@ " + sessionData.token + ")");
endProcess(sessionData.token);
}
});
})
.once("error", (e) => {
logger.log(
"\n\n\nHTTPS Get Error (@ " + sessionData.token + "): " + e.message
);
if (++sessionData.errorTick > 10) {
/*termination after 10 error ticks*/
logger.log("\n\n\nCannot resolve errors. Process stopped.");
endProcess(sessionData.token);
} else {
logger.log(
"\nWill try again in: " +
(PAGE_REQUEST_INTERVAL * 2) / 1000 +
" seconds."
);
setTimeout(function () {
requestPage(sessionData, siteData, chapterCount);
}, PAGE_REQUEST_INTERVAL * 2);
}
});
}
/*check if currentLink is chapter and update the nextLink if possible*/
function isChapterAndUpdateNext($, sessionData, siteData) {
var elements = $(siteData.nextBtn);
logger.log(
"Nav buttons in chapter (@ " + sessionData.token + "): " + elements.length
);
if (elements.length !== 0) {
sessionData.nextLink = elements.attr("href");
return true;
}
return false;
}
/*check if currentLink is main page and update novel info if possible*/
function novelInfoAndUpdateNext($, sessionData, siteData) {
var elements = $(siteData.readChapterBtn);
logger.log(
"Nav buttons in main (@ " + sessionData.token + "): " + elements.length
);
if (elements.length !== 0) {
getMainPage($, sessionData, siteData);
sessionData.nextLink = elements.attr("href");
return true;
}
return false;
}
/*select functions based on website*/
function getChapterAndWriteToFile($, sessionData, siteData) {
if (sessionData.writeStream.socket._writableState.ended) {
sessionData.hasEndednd = true;
return false;
}
var content = null;
switch (siteData.id) {
case 0:
content = novelBuddyChapter($, sessionData);
break;
}
if (content) {
sessionData.errorTick = 0;
return writeToFile(sessionData);
}
sessionData.errorTick++;
return false;
}
function getMainPage($, sessionData, siteData) {
switch (siteData.id) {
case 0:
novelBuddyMainPage($, sessionData);
sessionData.errorTick = 0;
break;
default:
sessionData.errorTick++;
}
}
/*Custom Code for each website */
function novelBuddyChapter($, sessionData) {
var chDetails = sessionData.currentLink.split("/")[4].split("-");
sessionData.chapter.num = chDetails[1];
sessionData.chapter.name = chDetails
.splice(2, chDetails.length - 2)
.join(" ");
//check if class name matches website
sessionData.chapter.content = $(".content-inner")
.children("p")
.map(function () {
return $(this).text();
})
.get()
.join("\n\n");
return sessionData.chapter.content;
}
function novelBuddyMainPage($, sessionData) {
//check if class name matches website
$(".author")
.children("a")
.each(function () {
sessionData.author.push($(this).attr("title"));
});
sessionData.summary = $(".summary").children(".content").text().trim();
}
/* End custom code */
/*write data to file using a write stream */
function writeToFile(sessionData) {
if (!sessionData.chapter.content) return;
var novel = "";
if (!sessionData.fileName) {
sessionData.title = sessionData.currentLink
.split("/")[3]
.split("-")
.join(" ");
novel = "Title: " + sessionData.title + "\nAuthor(s):";
for (let i = 0; i < sessionData.author.length; i++)
novel += "\n" + sessionData.author[i];
novel += "\n\nSummary\n" + sessionData.summary;
sessionData.fileName =
sessionData.title +
"_" +
sessionData.chapter.num +
"@" +
sessionData.token +
".txt";
}
novel +=
"\n\nChapter " +
sessionData.chapter.num +
" " +
sessionData.chapter.name +
"\n\n" +
sessionData.chapter.content;
/*CLIENT DISK VERSION */
return writeToClient(sessionData, novel);
/* SERVER DISK VERSION*/
//writeToServer(sessionData, novel);
}
/*CODE FOR WRITING TO CLIENT DISK*/
async function writeToClient(sessionData, novel) {
try {
if (!sessionData.writeStream.headersSent) {
sessionData.writeStream.writeHead(200, {
"Content-Type": "text/plain",
"Content-Disposition": "attachment; filename=" + sessionData.fileName,
});
}
} catch (err) {
logger.log(
"\n\n\nStream Header write Error (@ " +
sessionData.token +
"): " +
err.message
);
return false;
}
var canWrite = sessionData.writeStream.write(novel, () => {
sessionData.chapter.content = null;
logger.log(
"Session " +
sessionData.token +
": " +
"Novel " +
sessionData.title +
" Chapter " +
sessionData.chapter.num +
" written!"
);
});
if (!canWrite) await drain(sessionData);
else sessionData.writeStream.removeAllListeners("drain");
}
async function drain(sessionData) {
return new Promise((resolve, reject) => {
sessionData.writeStream.once("drain", resolve);
});
}
/*CODE FOR WRITING TO SERVER DISK*/
// async function writeToServer(sessionData, novel){
// var canWrite = true;
//
// if(!sessionData.writeStream){
//
// logger.log("\n\n\nCreate stream : " + sessionData.fileName);
// sessionData.writeStream = fs.createWriteStream(DOWNLOADS_PATH + '/' + sessionData.fileName, {flags: 'a+'});
//
// sessionData.writeStream.once('ready', ()=>{
// logger.log("\nReady to write: " + sessionData.fileName);
// canWrite = writeServerFile(sessionData, novel);
// });
//
// sessionData.writeStream.once('error', (err)=>{
// logger.log("\n\n\nStream Write Error (@ " + sessionData.token + "): " + err.message);
// sessionData.errorTick = 11;
// });
// }
// else {
// canWrite = writeServerFile(sessionData, novel);
// }
//
// // if(!canWrite)
// // await drain();
// else
// sessionData.writeStream.removeAllListeners('drain');
// }
//
// function writeServerFile(sessionData, novel){
//
// return sessionData.writeStream.write(novel, () =>{
// sessionData.chapter.content = null;
// logger.log("Session " + sessionData.token + ": " + "Novel " +
// sessionData.title + " Chapter " + sessionData.chapter.num + ' written!');
// });
// }
/*Run at server start*/
app.listen(PORT, function () {
logger.log("\n\n\nServer started");
logger.log("\n\n\nListening on port: " + PORT);
loadSiteMap();
/*CODE FOR WRITE ON SERVER DISK */
// fs.access(DOWNLOADS_PATH, fs.constants.F_OK, (err) => {
//
// if(err && err.code === 'ENOENT'){ //no folder, then create
// logger.log("\n\n\nFolder Access Error: " + err.message);
// logger.log("\nCreating directory...");
// fs.mkdirSync(DOWNLOADS_PATH);
// }else if(err){
// logger.log("\n\n\nSomething went wrong!\nFolder Access Error: " + err.message); //other errors
// process.exit(1);
// } else {
// logger.log("\n\n\n" + DOWNLOADS_PATH + " already exists!"); //folder exists
// }
//
// });
});
/*load sites from JSON file */
function loadSiteMap() {
logger.log("\n\n\nLoading Site Maps.");
try {
JSON.parse(fs.readFileSync(SITES_JSON_PATH)).forEach((site) => {
loadSite(site);
});
} catch (err) {
logger.log("\n\n\nJSON File Read Error: " + err);
logger.log("\nUsing default sites.\n");
loadDefaultSiteMap();
}
}
function loadDefaultSiteMap() {
sites.forEach((site) => {
loadSite(site);
});
}
function loadSite(site) {
siteMap.set(site.baseURL.split("/")[2], site);
logger.log("Loading site: " + site.baseURL);
}
/*write memory usage every 10 minutes */
setInterval(function () {
if (typeof gc === "function") {
gc();
}
logger.log(process.memoryUsage());
}, 600000);