-
Notifications
You must be signed in to change notification settings - Fork 1
/
parseXlsx.js
74 lines (67 loc) · 2.13 KB
/
parseXlsx.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 xlsx = require("xlsx");
const fs = require("fs");
const https = require("follow-redirects").https;
const concat = require("concat-stream");
const path = require("path");
const getAppDataPath = require("./getAppDataPath");
function download(url, cb) {
const concatStream = concat(cb);
https.get(url, function (response) {
response.pipe(concatStream);
});
}
const sheetsToIgnore = ["Misc", "Minigames", "Episodes"];
module.exports = function downloadAndParseTranslations(cb) {
download(
"https://docs.google.com/spreadsheets/d/16vqUNRQRB5CimIOVHfdyT5Yp6YOPzzVkR_9vtrU1wtI/export?format=xlsx",
function xlsxToJson(buffer) {
const file = xlsx.read(buffer, { type: "buffer" });
const translations = {
...Object.fromEntries(
file.SheetNames.filter(
(name) =>
!sheetsToIgnore.some((nameToIgnore) => name === nameToIgnore)
).map((name) => [
name,
xlsx.utils
.sheet_to_json(file.Sheets[name], {
header: ["name", "en", "jp"],
blankrows: true,
})
.slice(name === "Prologue" ? 22 : 1),
])
),
Episodes: xlsx.utils
.sheet_to_json(file.Sheets["Episodes"], {
header: ["name", "en", "jp", "flag"],
blankrows: true,
})
.slice(1)
.reduce(
(acc, line) =>
line.flag === "x"
? [...acc, [line]]
: [...acc.slice(0, -1), [...acc[acc.length - 1], line]],
[]
),
};
cb(translations);
saveAppData("translation.json", { date: new Date(), ...translations });
}
);
};
function saveAppData(name, content) {
const appDataDirPath = getAppDataPath();
if (!fs.existsSync(appDataDirPath)) {
fs.mkdirSync(appDataDirPath);
}
const appDataFilePath = path.join(appDataDirPath, name);
content = JSON.stringify(content, null, 2);
fs.writeFile(appDataFilePath, content, (err) => {
if (err) {
console.log("There was a problem saving data!");
} else {
console.log("Data saved correctly!");
}
});
}