-
Notifications
You must be signed in to change notification settings - Fork 9
/
voice_lines.js
157 lines (148 loc) · 5.05 KB
/
voice_lines.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
148
149
150
151
152
153
154
155
156
157
const fs = require("fs");
const nodefetch = require("node-fetch");
const JSDOM = require("jsdom").JSDOM;
// const AUDIO_REPO_URL = 'https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/'
let SHIPS = require("./ships.internal.json");
let VOICE_LINES = require("./voice_lines.json");
const HEADERS = {
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
cookie: "VEE=wikitext",
};
exports.refreshVoiceLines = async () => {
for (let key in Object.keys(SHIPS)) {
let ship = SHIPS[key];
console.log("Voice Lines of ('" + key + "') " + ship.names.code);
let text = await fetch(
ship.wikiUrl + "/Quotes",
"./web/ships.quotes/" + ship.names.en + ".html"
);
VOICE_LINES[key] = parseVoiceLines(new JSDOM(text).window.document, ship);
fs.writeFileSync("voice_lines.json", JSON.stringify(VOICE_LINES, null, "\t"));
const used = process.memoryUsage().heapUsed / 1024 / 1024;
if (used > 1000) await timeout(1000);
}
console.log("Done!");
};
function parseVoiceLines(doc, ship) {
let tabs = doc.getElementsByClassName("tabbertab");
let languages = {};
for (let tab of tabs) {
console.log("\tLanguage = " + tab.title);
let skins = [];
let i = 0;
while (i < tab.children.length) {
if (
tab.children[i].tagName === "H3" &&
tab.children[i + 1] &&
tab.children[i + 1].tagName === "TABLE"
) {
skins.push({
name: tab.children[i].textContent.trim(),
lines: parseTableLines(tab.children[i + 1]),
});
i++;
}
i++;
}
languages[tab.title] = skins;
}
return compressLanguages(languages, ship);
}
function compressLanguages(languages, ship) {
let skins = {};
for (let server of Object.values(languages)) {
for (let skin of server) {
let skinName = findElSkinName(ship.skins, skin.name);
if (!skins[skinName]) skins[skinName] = {};
for (let line of skin.lines) {
let eventCamel = camelize(line.event);
if (!skins[skinName][eventCamel])
skins[skinName][eventCamel] = {
event: line.event,
};
skins[skinName][eventCamel][line.language] = line.transcription;
if (
skins[skinName][eventCamel].audio &&
line.audio &&
skins[skinName][eventCamel].audio !== line.audio
) {
skins[skinName][eventCamel].audioAlt = skins[skinName][eventCamel].audioAlt || [];
skins[skinName][eventCamel].audioAlt.push(line.audio);
if (fs.existsSync("./web/ships.quotes/" + ship.names.en + ".html"))
fs.unlinkSync("./web/ships.quotes/" + ship.names.en + ".html");
} else skins[skinName][eventCamel].audio = skins[skinName][eventCamel].audio || line.audio;
}
}
}
let newSkins = {};
for (let key of Object.keys(skins)) {
newSkins[key] = Object.values(skins[key]);
}
return newSkins;
}
function parseTableLines(table) {
table = table.firstElementChild;
let lines = [];
for (let i = 1; i < table.children.length; i++) {
let row = table.children[i];
let notes = row.lastElementChild.textContent.trim();
lines.push({
event: row.children[0].textContent.trim(),
audio: row.children[1].firstElementChild ? row.children[1].firstElementChild.href : undefined,
language: row.children[2].lang,
transcription: row.children[2].textContent.trim(),
notes: notes ? notes : undefined,
});
}
return lines;
}
function fetch(url, localPath) {
return new Promise((resolve, reject) => {
if (fs.existsSync(localPath)) resolve(fs.readFileSync(localPath, "utf8"));
else
nodefetch(url, {
headers: HEADERS,
})
.then((res) => res.text())
.then((text) => {
fs.writeFileSync(localPath, text);
resolve(text);
})
.catch(reject);
});
}
function findElSkinName(skins, name) {
name = name.trim();
if (name.includes("Remodel") || name.includes("改") || name.includes("Kai")) return "Retrofit";
for (let skin of skins) {
if (skin.name.trim().includes(name) || name.includes(skin.name.trim())) return skin.name;
if (
skin.info.enClient &&
(skin.info.enClient.trim().includes(name) || name.includes(skin.info.enClient.trim()))
)
return skin.name;
if (
skin.info.cnClient &&
(skin.info.cnClient.trim().includes(name) || name.includes(skin.info.cnClient.trim()))
)
return skin.name;
if (
skin.info.jpClient &&
(skin.info.jpClient.trim().includes(name) || name.includes(skin.info.jpClient.trim()))
)
return skin.name;
}
return name;
}
function camelize(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, (match, i) => {
if (+match === 0) return "";
return i === 0 ? match.toLowerCase() : match.toUpperCase();
});
}
function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}