forked from Cygra/wechat-article-dl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmulti.js
168 lines (140 loc) · 4.39 KB
/
multi.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
const puppeteer = require("puppeteer");
//const SELECTOR = "#js_article > div";
const SELECTOR = "#vuepress-theme-blog__post-layout";
const fs = require('fs')
let InputFile = null
let URLS = []
let argError = false;
let dir='output/'
let usage = `
Usage:
node multi.js [-help] <[-inputfile <file path>]|[-url <[url1][,url2]...[,urln]>]> <[-dir <dir path>]>
-help : Print command help information.
-inputfile <file path> : Specify a filename which contains urls need to be exported.
-url <[url1][,url2]...[,urln]>] : Specify one or more URLs which need to be exported, each URL will be separated by ','.
Examples:
node multi.js -inputfile D:\\urls.txt
node multi.js -url D:\\urls.txt
`
String.prototype.replaceAll = function(s1, s2) {
return this.replace(new RegExp(s1, "gm"), s2);
}
for (let j = 0; j < process.argv.length; j++) {
if (process.argv[j] == '-help') {
argError = true;
break;
}
if (process.argv[j] == '-dir'){
dir=dir+process.argv[j+1]+'/'
}
if (process.argv[j] == '-inputfile' || process.argv[j] == '-url') {
if (j + 1 < process.argv.length) {
if (process.argv[j] == '-inputfile') {
let filename = process.argv[j + 1]
if (fs.existsSync(filename)) {
InputFile = filename;
}
else {
console.error(filename + " not exists.")
argError = true;
break;
}
}
else {
URLS = URLS.concat(process.argv[j + 1].split(','))
}
}
else {
argError = true;
break;
}
}
}
if (argError || (InputFile == null && URLS.length == 0)) {
console.log(usage)
return
}
if (InputFile != null) {
let filecontent = fs.readFileSync(InputFile, { encoding: 'utf-8', flag: 'r' })
URLS = URLS.concat(filecontent.split('\n'))
}
const logProcess = (percent) => {
const completed = Math.min(Math.floor(percent * 40), 40);
process.stdout.write("\r\x1b[K");
process.stdout.write(
`\uD83D\uDEA7[${Array(completed).fill("=").join("")}${Array(40 - completed)
.fill("-")
.join("")}]`
);
};
const autoScroll = async (page) => {
await page.evaluate(async () => {
await new Promise((resolve) => {
const STEP = 200;
const TIME_INTERVAL = 200;
let totalHeight = 0;
const timer = setInterval(() => {
const totalDistance = document.body.scrollHeight - window.innerHeight;
window.scrollBy(0, STEP);
totalHeight += STEP;
console.log("progress", totalHeight / totalDistance);
if (totalHeight >= totalDistance) {
clearInterval(timer);
resolve();
}
}, TIME_INTERVAL);
});
});
};
(async () => {
try {
const browser = await puppeteer.launch();
const total = URLS.length;
console.log("Start to exporting PDF files [" + total + "]")
const page = await browser.newPage();
if (!fs.existsSync(dir)){
fs.mkdir(dir,(err)=>{
if(err){
console.log('创建目录${dir}出错')
}else{
//console.log('未出错')
}
})
}
for (let j = 0; j < URLS.length; j++) {
const url = URLS[j];
if (url === '') {
console.log("[" + (j + 1) + "/" + total + "] 跳过空URL")
continue;
}
console.log("[" + (j + 1) + "/" + total + "] Exporting: " + url)
try {
await page.goto(url);
page.on("console", (consoleObj) => {
const content = consoleObj.text();
if (content.startsWith("progress")) {
logProcess(Number(consoleObj.text().split("progress ")[1]));
}
});
console.log("\u26FDStart to generate!");
await autoScroll(page);
await page.waitForSelector(SELECTOR);
const element = await page.$(SELECTOR);
await element.evaluate((el) => (el.style.padding = "16px"));
const filePath = `${dir}${await (await page.title()).replaceAll('?','').replaceAll('/','、').replaceAll(' ','')}.pdf`;
await page.pdf({
path: filePath,
format: 'A4'
});
//await element.screenshot({ path: `output/zd/${await page.title()}.png` });
console.log(`\n\uD83C\uDF7B${filePath} generated!`);
}catch (error) {
console.error("[" + (j + 1) + "/" + total + "] Exporting:" + url + " failed")
console.error(error)
}
}
await browser.close();
console.log("Exported all pages.")
} catch (error) {
}
})();