-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwikidot.js
301 lines (279 loc) · 10.3 KB
/
wikidot.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
const got = require('got');
const cheerio = require('cheerio');
const winston = require('winston');
const {progressAlert} = require('./util');
class WD {
constructor(base) {
this.wiki(base);
this.cookie = {
auth: '',
sess: '',
expire: null,
exp: null
};
}
wiki(base) {
this.domain = base;
if (!base.startsWith("http")) { base = `http://${base}.wikidot.com` }
this.base = base;
this.ajax = `${base}/ajax-module-connector.php`;
this.quic = `${base}/quickmodule.php`;
winston.debug(`Rebased to ${base}.`)
return this;
}
setCookies(v) { Object.assign(this.cookie, v) }
async req(params) {
const wikidotToken7 = Math.random().toString(36).substring(4);
let ret = await got.post(this.ajax, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
Referer: '05-B, B for Bot',
Cookie: `wikidot_token7=${wikidotToken7}; ${this.cookie.auth}`,
},
form: Object.assign({wikidot_token7: wikidotToken7, callbackIndex: 0}, params)
}).json();
if (ret.status!=="ok") {
let e = new Error(ret.message);
e.name = ret.status;
e.src = ret;
throw e;
} else return ret;
};
async quick(module, params) {
return await got.get(this.quic, {
searchParams: Object.assign({module: module}, params)
}).json();
}
async module(moduleName, params) {
return await this.req(Object.assign({moduleName: moduleName}, params))
}
async action(action, params) {
return await this.req(Object.assign({action: action, moduleName: "Empty"}, params))
}
async login(username, password) {
const wikidotToken7 = Math.random().toString(36).substring(4);
let res = await got.post('https://www.wikidot.com/default--flow/login__LoginPopupScreen', {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
Referer: '05-B, B for Bot',
Cookie: `wikidot_token7=${wikidotToken7};`,
},
form: {
login: username,
password: password,
action: 'Login2Action',
event: 'login',
wikidot_token7: wikidotToken7,
callbackIndex: 0
}
})
if (res.body.includes("The login and password do not match.")) {throw new Error("Wikidot login and password do not match.")}
let tmp = res.headers['set-cookie'][1].split("; ")
this.cookie.sess = tmp[0]
this.cookie.expire = tmp[1].split("=")[1]
this.cookie.exp = new Date(this.cookie.expire.split(",").pop().split("-").join(" "))
this.cookie.auth = `${this.cookie.sess}; wikidot_udsession=1; `
return this;
}
async getPageId(page) {
let pg = await got.get((page.startsWith('http') ? page : `${this.base}/${page}`) + '/norender/true', {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
Referer: '05-B, B for Bot',
Cookie: `${this.cookie.auth}`,
},
followRedirect: false
}).text();
pg = cheerio.load(pg);
let page_id = null;
pg(pg("head").children("script")
.filter((_i,el)=>pg(el).html().includes("WIKIREQUEST"))).html()
.replace(/WIKIREQUEST\.info\.pageId\s*=\s*(\d+)\s*;/g, (_, id)=>{
page_id = id
});
return page_id;
}
async resolvePageId(page_or_id) {
let page_id;
if (typeof page_or_id == "string") {
page_id = await this.getPageId(page_or_id);
} else if (typeof page_or_id == "number") {
page_id = page_or_id;
} else throw new Error(`page_or_id requires a String or Number. Received ${typeof page_or_id}`);
return page_id;
}
async viewSource(page_or_id, params) {
let page_id = await this.resolvePageId(page_or_id);
let options = Object.assign({
page_id: page_id,
raw: true,
}, params);
if (!options.raw) delete options.raw;
return await this.module("viewsource/ViewSourceModule", options);
}
async source(page_or_id, params) {
let page_id = await this.resolvePageId(page_or_id);
return await this.module("edit/TemplateSourceModule", Object.assign({
page_id: page_id,
}, params));
}
async history(page_or_id, params) {
let page_id = await this.resolvePageId(page_or_id);
return await this.module("history/PageRevisionListModule", Object.assign({
page_id: page_id,
page: 1,
perpage: 20,
options: `{"all":true}`
}, params));
}
async edit(wiki_page, params) {
var lock = await this.module('edit/PageEditModule', {
mode: 'page',
wiki_page: wiki_page,
force_lock: true})
return await this.action('WikiPageAction', Object.assign({
event: 'savePage',
wiki_page: wiki_page,
lock_id: lock.lock_id,
lock_secret: lock.lock_secret,
revision_id: lock.page_revision_id||null,
}, params));
}
async tags(page_or_id, params) {
let page_id = await this.resolvePageId(page_or_id);
let tags;
if (typeof params === "string") {
tags = params.split(" ");
} else if (params instanceof Array) {
tags = params;
} else if (params instanceof Object) {
tags = await this.module("pagetags/PageTagsModule", { pageId: page_id });
tags = cheerio.load(tags.body)(`input[id="page-tags-input"]`).attr("value").split(" ");
params.add = params.add instanceof Array ? params.add : (typeof params.add === "string" ? params.add.split(" ") : null);
params.remove = params.remove instanceof Array ? params.remove : (typeof params.remove === "string" ? params.remove.split(" ") : null);
tags = params.add ? tags.concat(params.add.filter(v=>!tags.includes(v))) : tags;
tags = params.remove ? tags.filter(v=>!params.remove.includes(v)) : tags;
}
return await this.action('WikiPageAction', {
event: 'saveTags',
pageId: page_id,
tags: tags.join(" "),
})
}
async delete(page_or_id, params) {
let page_id = await this.resolvePageId(page_or_id);
return await this.action('WikiPageAction', Object.assign({
event: 'deletePage',
page_id: page_id,
}, params));
}
async rename(page_or_id, new_name, params) {
let page_id = await this.resolvePageId(page_or_id);
return await this.action('WikiPageAction', Object.assign({
event: 'renamePage',
page_id: page_id,
new_name: new_name,
}, params));
}
async listPages(params) {
return await this.module('list/ListPagesModule', Object.assign({
category: ".",
order: "created_at desc desc",
perPage: "20",
separate: "true",
module_body: ``
}, params));
}
async countPages(params) {
let count = await this.module('list/CountPagesModule', Object.assign({
category: "*",
module_body: `%%total%%`,
}, params));
return parseInt(cheerio.load(count.body)("p").text());
}
async listBacklinkRaw(page_or_id, params) {
let page_id = await this.resolvePageId(page_or_id);
return await this.module('backlinks/BacklinksModule', Object.assign({
page_id: `${page_id}`,
}, params));
}
async listBacklink(page_or_id) {
let backlink = await this.listBacklinkRaw(page_or_id);
let $ = cheerio.load(backlink.body);
let backlinkList = $('h2:contains("[[include]]")').prev('ul');
let backlinkedPages = [], internalInclude = [], externalInclude = [];
if (backlinkList) {
backlinkList = backlinkList.children('li');
for (let i = 0; i < backlinkList.length; i++) {
let a = $(backlinkList[i]).children('a');
let rellink = a.attr('href').substring(1);
try {
let pageId = await this.getPageId(rellink);
backlinkedPages.push({
url: this.base + '/' + rellink,
pageId,
title: a.text().split(`(${rellink}`)[0].trim(),
});
} catch (e) {
winston.error(`Error while processing page :${this.domain}:${rellink}: ${e.stack}`);
} finally {
if (progressAlert(i)) {
winston.verbose(`[Backlinker] Processing backlinked page info: ${i}/${backlinkList.length}`);
}
}
}
winston.verbose(`[Backlinker] Processing backlinked page info: ${backlinkList.length}/${backlinkList.length}`);
winston.verbose(`[Backlinker] Backlinked page list constructed.`);
}
let includeList = $('h2:contains("[[include]]") + ul');
if (includeList) {
includeList = includeList.children('li');
for (let i = 0; i < includeList.length; i++) {
let a = $(includeList[i]).children('a');
let rellink = a.attr('href').substring(1);
try {
let pageId = await this.getPageId(rellink);
internalInclude.push({
url: this.base + '/' + rellink,
pageId,
title: a.text().split('(/')[0].trim(),
});
} catch (e) {
winston.error(`Error while processing page :${this.domain}:${rellink}: ${e.stack}`);
} finally {
if (progressAlert(i)) {
winston.verbose(`[Backlinker] Processing internal include info: ${i}/${includeList.length}`);
}
}
}
winston.verbose(`[Backlinker] Processing internal include info: ${includeList.length}/${includeList.length}`);
winston.verbose(`[Backlinker] Internal include list constructed.`);
}
includeList = $('h2:contains("[[include]]") + ul + ul');
if (includeList) {
includeList = includeList.children('li');
for (let i = 0; i < includeList.length; i++) {
let a = $(includeList[i]).children('a');
let link = a.attr('href');
try {
let pageId = await this.getPageId(link)
externalInclude.push({
url: link,
pageId,
title: a.text().split('(http')[0].trim(),
});
} catch (e) {
winston.error(`Error while processing page ${link}: ${e.stack}`);
} finally {
if (progressAlert(i)) {
winston.verbose(`[Backlinker] Processing external include info: ${i}/${includeList.length}`);
}
}
}
winston.verbose(`[Backlinker] Processing external include info: ${includeList.length}/${includeList.length}`)
winston.verbose(`[Backlinker] External include list constructed.`)
}
return {backlinkedPages, internalInclude,externalInclude};
}
}
module.exports = WD;