-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·113 lines (106 loc) · 2.55 KB
/
index.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
#!/usr/bin/env node
const {
promises: {
writeFile,
},
} = require('fs');
const {resolve} = require('path');
require('dotenv').config({
path: resolve(__dirname, './.env'),
});
let puppeteer;
if (process.env.BROWSER) {
puppeteer = require('puppeteer-core');
} else {
puppeteer = require('puppeteer');
}
const axios = require('axios');
const qs = require('qs');
const config = require('./config');
const {
isNumber,
isEmpty,
reduce,
} = require('lodash');
const {crawl} = require('./lib/traverse');
const {startUrl, domains} = config;
(async () => {
console.log('[SV] start');
if (!startUrl) {
throw new Error('[SV] No start URL');
}
const url = new URL(startUrl);
if (domains) {
domains.push(url.hostname);
} else {
config.domains = [url.hostname];
}
let browser;
try {
const options = {
args: [
'--no-sandbox',
'--disabled-setupid-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage',
],
};
if (process.env.BROWSER) {
options.executablePath = process.env.BROWSER;
}
browser = await puppeteer.launch(options);
const page = await browser.newPage();
const {checked, langNotMatch} = await crawl(page, startUrl);
const {total, broken} = reduce(checked, ({total, broken}, item, url) => {
if (isNumber(item)) {
if (item < 400) {
total.pass += 1;
} else {
total.fail += 1;
broken[url] = item;
}
} else {
total.error += 1;
const {message} = item;
broken[url] = message;
}
return {total, broken};
},
{
total: {
pass: 0,
error: 0,
fail: 0,
},
broken: {},
},
);
console.table(total);
if (!isEmpty(broken)) {
console.table(broken);
}
if (process.env.SERVER_CHAN_KEY) {
const key = process.env.SERVER_CHAN_KEY;
const data = qs.stringify({
title: '[Site Validator] - OR',
desp: JSON.stringify(total),
});
await axios.post(`https://sctapi.ftqq.com/${key}.send`, data);
const date = new Date();
const log = resolve(__dirname, [
date.getFullYear(),
date.getMonth() + 1,
date.getDate(),
].join('-') + '.log');
await writeFile(log, JSON.stringify(broken), 'utf8');
}
if (langNotMatch && langNotMatch.length !== 0) {
console.log('\nLanguage not matched:');
console.table(langNotMatch);
}
} catch (e) {
console.error(e);
}
await browser.close();
console.log('[SV] over');
})();