-
Notifications
You must be signed in to change notification settings - Fork 1
/
CrowdinIndex.js
111 lines (102 loc) · 3.7 KB
/
CrowdinIndex.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
const request = require('request');
const fs = require('fs');
const VersionIndex = ["1.12", "1.16", "1.17"]
const VersionBrancheID = {
"1.12": "37100",
"1.16": "14694",
"1.17": "33890"
}
let index = 1;
function RunLoop() {
GetVersionIndex(VersionIndex[0]);
setTimeout(function () {
GetVersionIndex(VersionIndex[index]);
index++;
if (index < VersionIndex.length) {
RunLoop();
}
}, 35e4) //每350秒鐘執行一個版本的數據
}
RunLoop();
function GetVersionIndex(Version) {
let GitVersion = `Original-${Version}`;
if (GitVersion === "Original-1.16") {
GitVersion = "Original"
}
let options = {
'method': 'GET',
'url': `https://raw.githubusercontent.com/RPMTW/ResourcePack-Mod-zh_tw/${GitVersion}/${Version}/ModInfo.json`,
};
request(options, function (error, response) {
if (error) console.log(error);
Run(JSON.parse(response.body), VersionBrancheID[Version])
});
function Run(json, branchId) {
let ModIDList = Object.keys(json);
var i = 0;
function CrowdinLoop() {
setTimeout(function () {
CrowdinRequst(ModIDList, branchId, i, json, Version),
i++;
if (i < ModIDList.length) {
CrowdinLoop();
}
}, 800) //每8百毫秒執行一次
}
CrowdinLoop();
}
}
function CrowdinRequst(ModIDList, branchId, i, json, Version) {
let options = {
'method': 'GET',
'url': `https://api.crowdin.com/api/v2/projects/442446/directories/?branchId=${branchId}&recursion&limit=1&filter=${ModIDList[i]}`,
'headers': {
'Authorization': `Bearer ${process.env.CrowdinToken}`,
}
};
request(options, function (error, response) {
if (error) console.log(error);
let CrowdinData = JSON.parse(response.body).data[0].data;
let CrowdinDirName = CrowdinData.name;
if (CrowdinDirName === ModIDList[i]) {
GetModTranslationProgress(CrowdinData.id, ModIDList, i, json, Version);
}
});
}
function GetModTranslationProgress(DirID, ModIDList, i, json, Version) {
let options = {
'method': 'GET',
'url': `https://api.crowdin.com/api/v2/projects/442446/directories/${DirID}/languages/progress/?limit=1`,
'headers': {
'Authorization': `Bearer ${process.env.CrowdinToken}`,
}
};
request(options, function (error, response) {
if (error) console.log(error);
let CrowdinData = JSON.parse(response.body).data[0].data;
let TranslationProgress = (CrowdinData.words.translated / CrowdinData.words.total * 100).toFixed(3) + "%";
/*
json[data] = CurseForge ID
ModIDList[i] = ModID
*/
let index = {};
index[json[ModIDList[i]]['curseForgeID']] = {
"ModID": ModIDList[i],
"Progress": TranslationProgress,
}
let before = {};
if (fs.existsSync(`${process.cwd()}/data/CrowdinIndex-${Version}.json`)) {
before = JSON.parse(fs.readFileSync(`${process.cwd()}/data/CrowdinIndex-${Version}.json`).toString());
} else {
fs.writeFileSync(`${process.cwd()}/data/CrowdinIndex-${Version}.json`, "{}");
}
let NewData = JSON.stringify(Object.assign({}, before, index))
fs.writeFileSync(`${process.cwd()}/data/CrowdinIndex-${Version}.json`, NewData, function (error) {
if (error) {
console.log(`寫入模組翻譯索引檔案時發生未知錯誤\n錯誤原因: ${error}`);
}
})
console.log(`處理 ${ModIDList[i]} 的翻譯索引檔案完成`)
}
);
}