forked from Velocita/genshin-gacha-export-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
295 lines (280 loc) · 6.39 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// library from cdn
const ExcelJSUrl = "https://cdn.bootcdn.net/ajax/libs/exceljs/4.2.1/exceljs.min.js";
const FileSaverUrl = "https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js";
const GachaTypesUrl = `https://hk4e-api.mihoyo.com/event/gacha_info/api/getConfigList`;
const GachaLogBaseUrl = `https://hk4e-api.mihoyo.com/event/gacha_info/api/getGachaLog`;
let AuthKey = ''
let AuthKeyVer = '1'
let Lang = 'zh-cn'
const mask = document.createElement('div')
mask.style = 'position: fixed;top: 0;bottom: 0;left: 0;right: 0;background: #000e;z-index: 99999;color: #fff;padding: 20px;font-size: 12px;overflow-y: auto;'
document.body.append(mask)
function htmlLog(str) {
console.log(str)
mask.innerHTML = `${str}<br>` + mask.innerHTML
}
function fetch2() {
return fetch(...arguments).then((res) => res.json()).then(ret => {
if (ret.retcode !== 0) {
htmlLog(ret.message || '请求失败!')
return Promise.reject(ret)
}
return ret
}, err => {
htmlLog('网络错误!')
return Promise.reject(err)
})
}
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(s) {
return new Promise(resolve => {
setTimeout(resolve, s * 1000)
})
}
async function getGachaLog(key, page, end_id) {
return fetch2(
GachaLogBaseUrl +
`?authkey=${AuthKey}` +
`&authkey_ver=${AuthKeyVer}` +
`&lang=${Lang}` +
`&gacha_type=${key}` +
`&page=${page}` +
`&size=${20}` +
`&end_id=${end_id}`
)
.then((data) => data);
}
async function getGachaLogs(name, key) {
let page = 1,
data = [],
res = [];
let end_id = "0";
let list = [];
do {
htmlLog(`正在获取${name}第${page}页`);
res = await getGachaLog(key, page, end_id);
await sleep(0.2);
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}`;
}
async function main() {
htmlLog("start load script");
await loadScript(ExcelJSUrl);
htmlLog("load exceljs success");
await loadScript(FileSaverUrl);
htmlLog("load filesaver success");
const uri = new URL(window.location.href)
AuthKey = uri.searchParams.get('authkey')
if (!AuthKey) {
htmlLog('AuthKey 获取失败!')
alert('AuthKey 获取失败!')
return
}
if (AuthKey.includes('/')) {
AuthKey = encodeURIComponent(AuthKey)
}
AuthKeyVer = uri.searchParams.get('authkey_ver') || '1'
Lang = uri.searchParams.get('lang') || 'zh-cn'
const gachaTypes = await fetch2(`${GachaTypesUrl}?authkey=${AuthKey}&authkey_ver=${AuthKeyVer}&lang=${Lang}`)
.then((data) => data.data.gacha_type_list);
htmlLog("获取抽卡活动类型成功");
htmlLog("开始获取抽卡记录");
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;
}
}
// htmlLog(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",
};
});
});
}
htmlLog("获取抽卡记录结束");
htmlLog("正在导出");
const filename = `原神抽卡记录导出_${getTimeString()}.xlsx`
try {
const buffer = await workbook.xlsx.writeBuffer();
saveAs(
new Blob([buffer], { type: "application/octet-stream" }),
filename
);
htmlLog("导出成功: " + filename);
} catch (e) {
htmlLog(`导出失败: ${e}`);
}
htmlLog('<a target="_blank" style="font-size:20px;color:red;" href="https://genshin.lhjmmc.cn">打开分析界面</a>');
}
if (!window.location.host.endsWith('mihoyo.com')) {
htmlLog('请在米哈游通行证页面执行!')
} else {
main()
}