-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiscr.js
112 lines (100 loc) · 2.56 KB
/
iscr.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
var Xray = require("x-ray");
var down = require("download");
var fs = require("fs");
const ver = "0.1";
/**
* iscr.js
*
* Author: imFS (github.com/imFS)
* License: MIT
*/
var xray = new Xray();
function getLastPart(str, delimiter) {
if (str[str.length - 1] == "/") {
str = str.substring(0, str.length - 1);
}
return str.substring(str.lastIndexOf(delimiter) + 1);
}
function scrapeImgs(url, path) {
path = path.replaceAll("\\", "/");
var name = getLastPart(url, "/");
console.log(`⏳${name}🍳`);
xray(url, "img", [
{
img: "",
src: "@src",
},
])(function (err, result) {
var imgs = result;
var localpaths = [];
var promises = [];
promises.push(
imgs.map(function (img) {
if (name) {
const imgpath = path + name + "/"; //+ getLastPart(img.src, "/");
//console.log(imgpath);
const filename = getLastPart(img.src, "/");
localpaths.push(filename);
if (!fs.existsSync(imgpath + filename)) {
var p = down(img.src, imgpath);
promises.push(p);
}
}
})
);
Promise.all(promises).then(() => {
const indexpath = htmlPageGen(name, localpaths, path);
console.log(`✅ | ${name} | Index page: "${indexpath}"`);
});
});
}
module.exports = {
scrape: scrapeImgs,
};
function htmlPageGen(title, imgs, path) {
var html =
"<!DOCTYPE html>\n\
<head>\n<title>" +
title +
'</title>\n</head>\n\
<body style="word-wrap: break-word;">\n \
<h2>' +
title +
"</h2>\n<h4>Generated by FS Scraper " +
ver +
"</h4>\n";
imgs.map(function (img) {
html +=
'<img src="' +
// path +title + "/" +
"./" +
img +
'" width="728" height="auto;" border="0"></img>\n';
});
// Convert last char to num and +1
let tsp = title.split("-");
let nc = Number(tsp[tsp.length - 1]) + 1;
let nd = title.replace("-" + (nc - 1).toString(), "") + "-";
html += "\n</br><p>[Next chapters] (Experimental)</p>\n";
let i = nc - 1;
if (nc - 2 >= 0) {
i = nc - 2;
}
for (; i < nc + 4; i++) {
if (i == nc - 1) {
html += "<b>";
}
//html += '<a href="' + path + nd + i + '/index.htm">' + i + "</a>";
html += '<a href="' + "../" + nd + i + '/index.htm">' + i + "</a>";
if (i == nc - 1) {
html += "</b>";
}
html += "\n";
}
html += "</body>\n</html>";
var indexpath = path + title + "/index.htm";
fs.writeFile(indexpath, html, function (err) {
if (err) console.log("GenFile write error: ", err);
});
return indexpath;
}