-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
180 lines (141 loc) · 4.44 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
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
const { writeFileSync } = require('fs')
const Crawler = require("crawler");
const url = require('url');
const path = require('path');
const { execSync } = require('child_process')
const toMarkdown = require('to-markdown');
let tracker;
const IGNORE_TABLE = [
/www\.tencent\.com/,
/kf\.qq\.com/,
/github\.com/,
/github\.com/,
/^https:\/\/mp\.weixin\.qq\.com$/,
/developers\.weixin\.qq\.com\/blogdetail/,
/mp\.weixin\.qq\.com\/cgi-bin\/opshowpage/,
/mp\.weixin\.qq\.com\/wxopen\/wawiki/,
/developers\.weixin\.qq\.com\/home/,
/\.zip$/,
];
const MAIN_PAGE = 'https://mp.weixin.qq.com/debug/wxadoc/dev/';
const Git = {
diff () {
const n = execSync('git diff --name-only | wc -l').toString().replace(/\n/g, '');
return n > 0
},
add () {
execSync(`git add --all`);
},
commit (msg) {
execSync(`git commit -am "${msg}" >/dev/null 2>&1`);
},
push () {
execSync('git pull --rebase && git push >/dev/null 2>&1');
}
}
class Tracker {
constructor (crawler) {
this.crawler = crawler;
this.resolveMap = {};
this.count = 0;
this.resolved = 0;
}
queue (uri) {
if (this.resolveMap[uri])
return;
this.resolveMap[uri] = 1;
console.log(`${this.resolved}/${this.count}: ${uri}`);
this.count++;
this.crawler.queue(uri);
}
complete () {
process.chdir(__dirname);
if (!Git.diff()) {
console.log('NO UPDATES');
return;
} else {
console.log('UPDATES FOUND.')
}
let date = new Date();
Git.add();
Git.commit(`[${date.getFullYear()}-${(date.getMonth() + 1)}-${date.getDate()}] UPDATE FOUNDS - @Gcaufy`);
Git.push();
}
}
var c = new Crawler({
maxConnections : 10,
// This will be called for each crawled page
callback : function (error, res, done) {
tracker.resolved++;
if(error){
console.log(error);
}else{
let $ = res.$;
let uri = res.options.uri;
if (/\/$/.test(uri)) {
uri += 'index.html';
}
if (typeof($) !== 'function') {
console.log(uri);
return
}
let body = $('body');
body.find('script').remove();
let html = body.html();
// removed version update
html = html.replace(/\?{0,1}t=20\d{2}\d{2,4}/ig, '');
let md = toMarkdown(html);
let urlObj = url.parse(uri);
let name = urlObj.pathname.replace(/\//g, '_');
if (!/\.html$/.test(name)) {
name = name + '.html';
}
let mdName = name.replace(/\.html$/g, '.md');
writeFileSync(path.join(__dirname, 'html', name), html);
writeFileSync(path.join(__dirname, 'md', mdName), md);
let links = [];
body.find('a').each(function () {
let link = $(this).attr('href');
if (link === undefined)
return;
if (/^javascript:/.test(link)) {
return;
}
if (/^mailto:/.test(link)) {
return;
}
if (/\.md$/.test(link)) {
return;
}
if (link === './')
return;
let ignored = IGNORE_TABLE.some((reg) => {
return reg.test(link);
});
if (ignored)
return;
if (!/^https?:/.test(link)) {
link = url.resolve(uri, link);
}
let linkObj = url.parse(link);
link = linkObj.protocol + '//' + linkObj.host + linkObj.path;
if (link === 'https://mp.weixin.qq.com/')
return;
if (linkObj.host !== 'mp.weixin.qq.com')
return;
if (/\.md$/.test(link)) {
return;
}
if (/^\/debug/.test(linkObj.path))
tracker.queue(link);
});
}
done();
if (tracker.resolved === tracker.count) {
console.log('ALL DONE');
tracker.complete();
}
}
});
tracker = new Tracker(c);
tracker.queue(MAIN_PAGE);