forked from DataUSA/datausa-wikimedia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
98 lines (86 loc) · 2.42 KB
/
upload.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
const Bot = require("nodemw"),
client = new Bot({
protocol: "https",
server: "commons.wikimedia.org",
path: "/w",
debug: false,
username: process.env.WIKI_USERNAME,
password: process.env.WIKI_PASSWORD,
userAgent: "OEC Uploader (https://oec.world; [email protected])"
}),
fs = require("fs");
let total;
function upload(files) {
const fileName = files.pop();
process.stdout.write(`\nUploading ${total - files.length} of ${total}`);
const {title} = require(fileName.replace("svg", "js"));
function deleteFiles(fileName) {
fs.unlink(fileName, () => {});
fs.unlink(fileName.replace("svg", "js"), () => {});
fs.unlink(fileName.replace("svg", "txt"), () => {});
}
function next() {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(title);
if (files.length) {
setTimeout(() => upload(files), 7500);
}
else {
console.log("\n\ndone!");
process.exit(0);
}
}
fs.readFile(fileName, "utf8", (err, svg) => {
if (err) {
console.log("\nSVG readFile:");
console.error(err);
next(title);
}
else {
fs.readFile(fileName.replace("svg", "txt"), "utf8", (err, txt) => {
if (err) {
console.log("\nTXT readFile:");
console.error(err);
next(title);
}
else {
client.upload(title, svg, "updates SVG", err => {
if (err && err.message.includes("duplicate")) {
deleteFiles(fileName);
next(title);
}
else if (err) {
console.log("\nSVG upload:");
console.error(err);
next(title);
}
else {
client.edit(`File:${title}.svg`, txt, "updates description", err => {
if (err) {
console.log("\nTXT edit:");
console.error(err);
}
else {
deleteFiles(fileName);
}
next(title);
});
}
});
}
});
}
});
}
client.logIn(err => {
if (err) throw err;
fs.readdir("./exports", (err, files) => {
if (err) throw err;
files = files.filter(file => file.includes(".svg"))
.sort((a, b) => b.localeCompare(a))
.map(file => `./exports/${file}`);
total = files.length;
upload(files);
});
});