forked from Velocita/genshin-gacha-export-js
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
236 lines (224 loc) · 8.51 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
(async function () {
//替换指定传入参数的值
//win为某窗体,paramName为参数,paramValue为新值,forceAdding为不存在该参数时是否也指定
function replaceParamVal(win, paramName, paramValue, forceAdding) {
var search = win.location.search + "";
if (!search) {
//没有任何查询参数,则直接附加
return forceAdding
? win.location + "?" + paramName + "=" + paramValue
: win.location + "";
} else {
var q = (win.location + "").split("?")[0];
var list = search.replace("?", "").split("&");
var hasIn = false;
for (var i = 0; i < list.length; i++) {
var listI = list[i];
if (
listI.split("=")[0].toLowerCase() == paramName.toLowerCase()
) {
//指定参数
q = q + (i == 0 ? "?" : "&");
hasIn = true;
if (listI.indexOf("=") == -1) {
//形式:"参数"
q = q + listI + "=" + paramValue;
} else if (!listI.split("=")[1].length) {
//形式:"参数="
q = q + listI + paramValue;
} else {
//形式:"参数=值"
q = q + paramName + "=" + paramValue;
}
} else {
//其它参数
q = q + (i == 0 ? "?" : "&");
q = q + listI;
}
}
if (!hasIn && forceAdding) {
//不存在,但必须要添加时
q = q + "&" + paramName + "=" + paramValue;
}
return q;
}
}
// 强制使用zh-cn
//window.history.pushState('Object', 'Title', replaceParamVal(window.parent,'lang','zh-cn',true)+"#/log");
const zhsearch = replaceParamVal(window.parent, "lang", "zh-cn", true);
// constant
// library from cdn
const ExcelJSUrl = "https://cdnjs.cloudflare.com/ajax/libs/exceljs/4.2.0/exceljs.min.js";
const FileSaverUrl = "https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js";
// mihoyo api
//const GachaTypesUrl = `//hk4e-api.mihoyo.com/event/gacha_info/api/getConfigList${location.search}`;
//const GachaLogBaseUrl = `//hk4e-api.mihoyo.com/event/gacha_info/api/getGachaLog${location.search}`;
const GachaTypesUrl = `//hk4e-api.mihoyo.com/event/gacha_info/api/getConfigList?${
zhsearch.split("?")[1]
}`;
const GachaLogBaseUrl = `//hk4e-api.mihoyo.com/event/gacha_info/api/getGachaLog?${
zhsearch.split("?")[1]
}`;
// function
function loadScript(src) {
return new Promise((resolve, reject) => {
if (document.querySelector(`script[src="${src}"]`)) {
resolve();
}
const s = document.createElement("script");
s.src = src;
s.onload = resolve;
s.onerror = reject;
document.body.append(s);
});
}
function sleep(second) {
return new Promise((r) => setTimeout(() => r(), second * 1000));
}
function getGachaLog(key, page, end_id) {
return fetch(
GachaLogBaseUrl +
`&gacha_type=${key}` +
`&page=${page}` +
`&size=${20}` +
`&end_id=${end_id}`
)
.then((res) => res.json())
.then((data) => data);
}
async function getGachaLogs(name, key) {
let page = 1,
data = [],
res = [];
let end_id = "0";
let list = [];
do {
console.log(`正在获取${name}第${page}页`);
res = await getGachaLog(key, page, end_id);
await sleep(0.5);
end_id = res.data.list.length > 0 ? res.data.list[res.data.list.length - 1].id : 0;
list = res.data.list;
data.push(...list);
page += 1;
} while (list.length > 0);
return data;
}
function pad(num) {
return `${num}`.padStart(2, "0");
}
function getTimeString() {
const d = new Date();
const YYYY = d.getFullYear();
const MM = pad(d.getMonth() + 1);
const DD = pad(d.getDate());
const HH = pad(d.getHours());
const mm = pad(d.getMinutes());
const ss = pad(d.getSeconds());
return `${YYYY}${MM}${DD}_${HH}${mm}${ss}`;
}
// processing
console.log("start load script");
await loadScript(ExcelJSUrl);
console.log("load exceljs success");
await loadScript(FileSaverUrl);
console.log("load filesaver success");
// const data = await fetch(ItemDataUrl).then((res) => res.json());
// console.log("获取物品列表成功");
const gachaTypes = await fetch(GachaTypesUrl)
.then((res) => res.json())
.then((data) => data.data.gacha_type_list);
console.log("获取抽卡活动类型成功");
console.log("开始获取抽卡记录");
const workbook = new ExcelJS.Workbook();
for (const type of gachaTypes) {
const sheet = workbook.addWorksheet(type.name, {
views: [{ state: "frozen", ySplit: 1 }],
});
sheet.columns = [
{ header: "时间", key: "time", width: 24 },
{ header: "名称", key: "name", width: 14 },
{ header: "类别", key: "type", width: 8 },
{ header: "星级", key: "rank", width: 8 },
{ header: "总次数", key: "idx", width: 8 },
{ header: "保底内", key: "pdx", width: 8 },
];
// get gacha logs
const logs = (await getGachaLogs(type.name, type.key)).map((item) => {
// const match = data.find((v) => v.item_id === item.item_id);
return [
item.time,
item.name,
item.item_type,
parseInt(item.rank_type),
];
});
logs.reverse();
idx = 0;
pdx = 0;
for (log of logs) {
idx += 1;
pdx += 1;
log.push(idx, pdx);
if (log[3] === 5) {
pdx = 0;
}
}
// console.log(logs);
sheet.addRows(logs);
// set xlsx hearer style
["A", "B", "C", "D", "E", "F"].forEach((v) => {
sheet.getCell(`${v}1`).border = {
top: { style: "thin", color: { argb: "ffc4c2bf" } },
left: { style: "thin", color: { argb: "ffc4c2bf" } },
bottom: { style: "thin", color: { argb: "ffc4c2bf" } },
right: { style: "thin", color: { argb: "ffc4c2bf" } },
};
sheet.getCell(`${v}1`).fill = {
type: "pattern",
pattern: "solid",
fgColor: { argb: "ffdbd7d3" },
};
sheet.getCell(`${v}1`).font = {
name: "微软雅黑",
color: { argb: "ff757575" },
bold: true,
};
});
// set xlsx cell style
logs.forEach((v, i) => {
["A", "B", "C", "D", "E", "F"].forEach((c) => {
sheet.getCell(`${c}${i + 2}`).border = {
top: { style: "thin", color: { argb: "ffc4c2bf" } },
left: { style: "thin", color: { argb: "ffc4c2bf" } },
bottom: { style: "thin", color: { argb: "ffc4c2bf" } },
right: { style: "thin", color: { argb: "ffc4c2bf" } },
};
sheet.getCell(`${c}${i + 2}`).fill = {
type: "pattern",
pattern: "solid",
fgColor: { argb: "ffebebeb" },
};
// rare rank background color
const rankColor = {
3: "ff8e8e8e",
4: "ffa256e1",
5: "ffbd6932",
};
sheet.getCell(`${c}${i + 2}`).font = {
name: "微软雅黑",
color: { argb: rankColor[v[3]] },
bold: v[3] != "3",
};
});
});
}
console.log("获取抽卡记录结束");
console.log("正在导出");
const buffer = await workbook.xlsx.writeBuffer();
const timestamp = getTimeString();
saveAs(
new Blob([buffer], { type: "application/octet-stream" }),
`原神抽卡记录导出_${timestamp}.xlsx`
);
console.log("导出成功");
})();