-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpcr-accepter.js
executable file
·147 lines (137 loc) · 3.65 KB
/
pcr-accepter.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
#!/usr/bin/env node
// Dependencies.
const argv = require('minimist')(process.argv.slice(2));
const MWBot = require('mwbot');
const mysql = require('mysql');
const util = require('util');
const credentials = require('./PCR_credentials'); // Load credentials from config.
const apiUrl = 'https://en.wikipedia.org/w/api.php';
const database = 'enwiki_p';
const acceptSummary = 'Task 65: Automatically accept, no net change';
/**
* Log a message to stdout prepended with a timestamp.
* @param {String} message
*/
function log(message) {
const datestamp = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
console.log(`${datestamp}: ${message}`);
}
/**
* Retrieve bot instance
* @returns MWBot
*/
async function getBot() {
// Login to the bot.
log(`Logging in to bot account`);
const bot = new MWBot({apiUrl});
await bot.loginGetEditToken({
apiUrl,
username: credentials.username,
password: credentials.password
});
return bot;
}
/**
* Accept the null changes
* @param {int} revid
* @param {MWBot} bot
* @returns {Promise<void>}
*/
async function acceptNoChange( revid, bot ) {
log(`Accepting revision ${revid}`);
await bot.request( {
action: 'review',
revid: revid,
comment: acceptSummary,
token: bot.editToken
} ).then( response => {
console.log( response );
} ).catch(err => {
const error = err.response && err.response.error ? err.response.error.code : 'Unknown';
log(`Failed to accept edits: ${error}`);
});
}
/**
* Get all pending changes
* @param {MWBot} bot
* @returns {Promise<Array|false>} pending changes
*/
function getPending( bot ) {
log(`Querying pending changes via api`);
return new Promise((resolve) => {
bot.request( {
action: 'query',
list: 'oldreviewedpages',
ormaxsize: 0,
ornamespace: '*',
orlimit: 'max',
formatversion: 2,
} ).then( response => {
console.log( response );
resolve( response );
} ).catch(err => {
const error = err.response && err.response.error ? err.response.error.code : 'Unknown';
log(`Failed to get pending changes: ${error}`);
resolve( false )
});
});
}
/**
* Check pending changes to a page
* @param {MWBot} bot
* @param {int} oldid
* @param {int} newid
* @param {bool} dry
*/
async function checkPage( bot, oldid, newid, dry ) {
const old_content = await getPage( bot, oldid );
const new_content = await getPage( bot, newid );
if ( old_content === new_content ) {
log( `No net change between ${oldid} and ${newid}` );
if ( dry ) {
console.log( `Would accept ${newid}` );
} else {
await acceptNoChange( newid, bot );
}
}
}
/**
* Get the wikitext of a page
* @param {MWBot} bot
* @param {int} revid
* @returns {Promise<String|false>}
*/
function getPage( bot, revid ) {
return new Promise((resolve) => {
bot.request( {
action: 'parse',
oldid: revid,
prop: 'wikitext'
} ).then( response => {
//console.log( response );
resolve( response.parse.wikitext['*'] );
} ).catch(err => {
const error = err.response && err.response.error ? err.response.error.code : 'Unknown';
log(`Failed to accept edits: ${error}`);
resolve( false )
});
});
}
/**
* Entry point for the bot task.
* @returns {Promise<void>}
*/
async function main() {
const bot = await getBot();
const allPending = await getPending( bot );
console.log( 'allPending', allPending );
const pending = allPending.query.oldreviewedpages;
console.log( 'pending', pending );
for ( var iii = 0; iii < pending.length; iii++ ) {
console.log( pending[iii].stable_revid, pending[iii].revid );
await checkPage( bot, pending[iii].stable_revid, pending[iii].revid, argv.dry );
}
log('Task complete!');
process.exit();
}
main().catch(console.error);