forked from siddharthvp/SDZeroBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotbase.ts
75 lines (63 loc) · 2.47 KB
/
botbase.ts
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
/** Base file to reduce the amount of boilerplate code in each file */
import * as fs from 'fs';
import * as path from 'path';
import * as child_process from 'child_process';
export {fs, path, child_process};
/** Notify by email on facing unexpected errors, see wikitech.wikimedia.org/wiki/Help:Toolforge/Email */
export function emailOnError(err: Error, taskname: string, isFatal = true) {
logFullError(err, isFatal);
child_process.exec(
`echo "Subject: ${taskname} error\n\n${taskname} task resulted in the error:\n\n${err.stack}\n" | /usr/sbin/exim -odf -i [email protected]`,
() => {} // Emailing failed, must be a non-toolforge environ
);
// exit normally
}
export function logFullError(err: Error, isFatal = true) {
// datetime similar to what mwn log produces, but can't use that directly as mwn may not have loaded
const dateTimeString = new Date().toISOString().slice(0, 19).replace('T', ' ');
console.log(`[${dateTimeString}] ${isFatal ? '[E] Fatal error' : '[E] Error'}`);
console.log(err);
}
// Errors occurring inside async functions are caught by emailOnError(),
// this is only for anything else, such as failing imports
process.on('uncaughtException', function(err) {
if (process.argv[1]) {
var taskname = path.basename(process.argv[1]);
emailOnError(err, taskname);
} else { // else we're probably running in the console
console.log(err);
}
});
import {mwn} from '../mwn';
export {mwn};
/** Colorised and dated console logging. */
export const log = mwn.log;
/** Parsed console arguments */
export const argv = require('minimist')(process.argv.slice(2));
import {updateLoggingConfig} from "../mwn/build/log";
updateLoggingConfig({
printVerbose: !!argv.verbose
});
/** bot account and database access credentials */
const auth = require('./.auth');
export class AuthManager {
static get(account: string) {
return auth[account];
}
}
export const bot = new mwn({
apiUrl: 'https://en.wikipedia.org/w/api.php',
...AuthManager.get('SDZeroBot:bp1'),
OAuthCredentials: AuthManager.get('SDZeroBot:oauth1'),
defaultParams: {
assert: 'bot'
},
maxRetries: 7, // Nov 2020: lag on the roof
userAgent: 'w:en:User:SDZeroBot'
});
bot.initOAuth();
import TextExtractor from "./TextExtractor";
export { TextExtractor };
// Deprecated exports, import from ./db or ./utils directly
export {mysql, db, enwikidb, toolsdb} from './db';
export * as utils from './utils';