-
Notifications
You must be signed in to change notification settings - Fork 2
/
CNTech.js
320 lines (310 loc) · 9.43 KB
/
CNTech.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
const cheerio = require('cheerio');
const got = require('got');
const WD = require('./wikidot.js');
const EventEmitter = require('events');
const winston = require('winston');
const {branch, branchId, progressAlert} = require('./util');
class CNTech extends EventEmitter {
constructor() {
super();
this.tech = new WD('scp-tech-cn');
this.cn = new WD('scp-wiki-cn');
this.reserves = [];
this.outdates = [];
}
loginAll(WD_NAME, WD_PW) {
let t = this.loginSite("tech", WD_NAME, WD_PW)
let c = this.loginSite("cn", WD_NAME, WD_PW)
Promise.all([t,c]).then(()=>{
this.emit('ready')
winston.info(`Bot is ready.`)
}).catch(e=>{
winston.error(e.message)
})
}
loginSite(site, WD_NAME, WD_PW) {
let temp = this[site].login(WD_NAME, WD_PW)
temp.then(()=>{
winston.info(`Bot logged onto ${this[site].domain}.`)
winston.info(`${this[site].domain} login expires on ${this[site].cookie.exp}.`)
})
if (this[site]._refresh) { clearInterval(this[site]._refresh) }
this[site]._refresh = setInterval(()=>{
try {
if (this[site].cookie.exp - Date.now() <= 2592000000) {
this[site].login(WD_NAME, WD_PW).then(res=>{
winston.info(`Bot logged onto ${this[site].domain}.`)
winston.info(`${this[site].domain} login expires on ${this[site].cookie.exp}.`)
})
}
} catch (e) {
winston.error(e.message)
}
}, 864000000)
return temp;
}
async getInfo(params) {
let info = [];
let cn = this.cn;
let tech = this.tech;
let res = await tech.listPages(Object.assign({
category: "reserve",
created_at: "older than 30 day",
order: "created_at desc desc",
perPage: "500",
separate: "false",
module_body: `[[head]]
[[table class="wiki-content-table"]]
[[/head]]
[[body]]
[[row]]
[[cell]]
%%created_by%%
[[/cell]]
[[cell]]
%%created_by_unix%%
[[/cell]]
[[cell]]
%%created_by_id%%
[[/cell]]
[[cell]]
%%form_raw{branch}%%
[[/cell]]
[[cell]]
%%name%%
[[/cell]]
[[cell]]
%%fullname%%
[[/cell]]
[[cell]]
%%title%%
[[/cell]]
[[cell]]
%%created_at|%Y-%m-%d|hover%%
[[/cell]]
[[cell]]
%%form_raw{page}%%
[[/cell]]
[[/row]]
[[/body]]
[[foot]]
[[/table]]
[[/foot]]`
}, params));
let $ = cheerio.load(res.body);
//console.log(res.body)
let all = $('table').find('tr');
for (let i = 0; i < all.length; i++) {
winston.debug(`Retreiving record ${i+1} of ${all.length}`)
let meta = $(all[i]).children('td')
let rawname = $(meta[5]).text().trim()
let user = {
displayName: $(meta[0]).text().trim(),
unixname: $(meta[0]).text().trim()==="(user deleted)" ? null : $(meta[1]).text().trim(),
id: $(meta[2]).text().trim(),
}
if ($(meta[0]).text().trim()==="(user deleted)") {
let $ = await tech.history(rawname, {});
$ = cheerio.load($.body);
user.id = $("tbody").children("tr").last().find(`span[class="printuser deleted"]`).attr("data-id")
}
let temp = $(meta[3]).text().trim(), temp2 = null;
let cat = parseInt(temp)==0 ? "wanderers:" : "";
let temp3 = rawname.length >= 55 && $(meta[8]).text().trim().length ? $(meta[8]).text().trim() : $(meta[4]).text().trim();
if (parseInt(temp)<0||parseInt(temp)>=15) { temp = null }
else {
temp2=`http://${branch[temp]}.wikidot.com`;
temp = await cn.quick("PageLookupQModule", {s:branchId[temp], q:temp3});
temp = temp.pages.find(v=>v.unix_name === temp3);
}
let page = {
exist: temp2 ? !!temp : null,
originWiki: temp2,
url: `${temp2}/${temp3}`,
name: temp3,
title: temp ? temp.title : $(meta[6]).text().trim(),
}
let created = parseInt($(meta[7]).children('span').attr('class').split(' ')[1].substring(5)+'000')
let trans = {
exist: false,
translator: null,
url: null,
title: null,
created: null,
}
temp = `${cat}${page.name.startsWith("wanderers:") ? page.name.replace("wanderers:","") : page.name}`
let pageinfo = await cn.listPages({
category: '*',
fullname: temp.length>60 ? temp.substring(0,60) : temp,
module_body: `[[table class="exist"]]
[[row]]
[[cell]]
%%created_by%%
[[/cell]]
[[cell]]
%%created_by_unix%%
[[/cell]]
[[cell]]
%%created_by_id%%
[[/cell]]
[[cell]]
%%link%%
[[/cell]]
[[cell]]
%%title%%
[[/cell]]
[[cell]]
%%created_at|%Y-%m-%d|hover%%
[[/cell]]
[[/row]]
[[/table]]`});
meta = cheerio.load(pageinfo.body)('table.exist').find('td');
if (!!meta.length) {
trans = {
exist: true,
translator: {
displayName: $(meta[0]).text().trim(),
unixname: $(meta[1]).text().trim(),
id: $(meta[2]).text().trim(),
},
url: $(meta[3]).text().trim(),
title: $(meta[4]).text().trim(),
created: parseInt($(meta[5]).children('span').attr('class').split(' ')[1].substring(5)+'000'),
}
}
info.push({user:user, page:page, created:created, trans:trans, rawname:rawname});
};
return info;
}
async outdate() {
let now = Date.now()
let info = await this.getInfo({
category: "reserve",
created_at: "older than 30 day"
})
info.forEach(v=>{
if (v.trans.exist && v.created<=v.trans.created || v.created<=now-7776000000) {
this.tech.delete(v.rawname).then(()=>{
winston.verbose(`Deleted "${v.rawname}"`);
}).catch(e=>{
winston.warn(`${e.name} at deleting "${v.rawname}": ${e.message}`);
})
}
else {
this.tech.rename(v.rawname, `outdate:${v.page.name}`).then(()=>{
winston.verbose(`Renamed "${v.rawname}" to "outdate:${v.page.name}"`);
}).catch(e=>{
if (e.name==='page_exists') {
this.tech.delete(`outdate:${v.page.name}`).then(()=>{
winston.verbose(`Deleted "outdate:${v.page.name}"`);
this.tech.rename(v.rawname, `outdate:${v.page.name}`).then(()=>{
winston.verbose(`Renamed "${v.rawname}" to "outdate:${v.page.name}"`);
}).catch(e=>{
winston.warn(`${e.name} at renaming "${v.rawname}": ${e.message}`);
})
}).catch(e=>{
winston.warn(`${e.name} at deleting "${v.rawname}": ${e.message}`);
})
}
else winston.warn(`${e.name} at renaming "${v.rawname}": ${e.message}`);
})
}
})
}
async remove() {
let info = await this.getInfo({
category: "reserve",
created_at: null,
tags: "-无原文",
})
let info2 = await this.getInfo({
category: "outdate",
created_at: "older than 30 day",
tags: "-长网址",
})
info.forEach(v=>{
let tag = v.rawname.length>=55 ? ["长网址"] : [];
if (v.page.exist === false) {
tag.push("无原文");
winston.verbose(`No source article for "${v.rawname}"`);
} else if (v.trans.exist && v.created<=v.trans.created) {
tag.push("已翻译")
}
if (tag.length) {
this.tech.tags(v.rawname, {add: tag}).catch(e=>{
winston.warn(`${e.name} at tagging "${v.rawname}": ${e.message}`);
})
}
})
info2.forEach(v=>{
if (!v.page.exist || v.trans.exist) {
this.tech.delete(v.rawname).then(()=>{
winston.verbose(`Deleted "${v.rawname}"`);
}).catch(e=>{
winston.warn(`${e.name} at deleting "${v.rawname}": ${e.message}`);
})
}
})
}
async untag() {
let info = await this.getInfo({
category: "reserve",
created_at: null,
tags: "+已翻译",
})
info.forEach(v=>{
if (!v.trans.exist) {
this.tech.tags(v.rawname, {remove: "已翻译"}).catch(e=>{
winston.warn(`${e.name} at tagging "${v.rawname}": ${e.message}`);
})
}
})
}
async expire() {
let info = await this.getInfo({
category: "outdate",
created_at: "older than 90 day"
})
info.forEach(v=>{
this.tech.delete(v.rawname).then(()=>{
winston.verbose(`Deleted "${v.rawname}"`);
}).catch(e=>{
winston.warn(`${e.name} at deleting "${v.rawname}": ${e.message}`);
})
})
}
async updateArchive(apiEndpoint, token) {
let info = await this.getInfo({
category: "reserve outdate",
created_at: null,
});
let res = await got.post(apiEndpoint, {
json: {
token,
data: info.map(v=>{
return {
user: v.user.displayName,
userWikidotId: parseInt(v.user.id),
wikipage: v.page.name,
originWiki: v.page.originWiki,
title: v.page.title,
date: v.created,
expired: v.rawname.startsWith("outdate"),
}
})
},
}).json();
if (res.status == "ok") {
winston.verbose(`[Archiver] updated archive.`);
} else {
winston.error(`[Archiver] ${res.error}: ${res.message}`);
}
}
async debug() {
return await this.getInfo({
created_at: null,
category: "outdate",
})
}
}
module.exports = CNTech;