-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (60 loc) · 1.7 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
const { argv } = require("yargs");
const MWBot = require("mwbot");
const { fetchData, log, readInputFile, wikify } = require("./helpers");
const credentials = {
username: argv.user || "Polona Upload",
password: argv.password
};
const wiki = new MWBot({
apiUrl: "https://commons.wikimedia.org/w/api.php"
});
async function processInput(fileName = "input.txt") {
log(`[i] Reading file '${fileName}'`);
await readInputFile(fileName).then(async values => {
const { csrftoken } = await wiki.getEditToken();
// eslint-disable-next-line
for (const id of values) {
// eslint-disable-next-line
const data = await fetchData(id);
const { academica_id, name, file_url } = data;
const wikicode = wikify(data);
if (argv.test) {
log(data);
log(wikicode);
}
if (!argv.test) {
log(`Uploading ${academica_id}...`);
log(`[i] File:${name}`);
// eslint-disable-next-line
await wiki
.request({
action: "upload",
filename: name,
url: file_url,
text: wikicode,
token: csrftoken
})
.catch(err => log(`[E] ${err.message}`));
}
}
});
}
async function login() {
await wiki.loginGetEditToken(credentials);
}
async function main() {
if (!argv.input) {
log(`[W] No input file specified, assuming 'input.txt'.
Add --input=<filename> to change it`);
}
if (!argv.test) {
if (!argv.password) {
throw new Error(`Missing password!
Add --password=<password>`);
}
await login();
}
await processInput(argv.input);
log("[i] Done");
}
main().catch(err => log(`[E] ${err.message}`));