forked from zeokku/genshin-daily-check-in-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (132 loc) · 3.81 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
import axios from "axios";
import { stringify as objToQuery, parse as queryToObj } from "querystring";
import { log, err, warn, ok, accent } from "./log.js";
import fs from "fs";
log("Launching...");
//-------------------
main();
async function main() {
let cookieContainer = null;
if (fs.existsSync("./cookies.json")) {
cookieContainer = JSON.parse(fs.readFileSync("./cookies.json").toString());
}
if (!cookieContainer.cookie_token || !cookieContainer.account_id) {
//get cookie_token and account_id
//apparently this request has a time limit until you can obtain account cookies after log in
//meaning after some time it will not return the needed cookies so you need to store them
//or login again
try {
let { headers, data } = await axios.get(
"https://webapi-os.account.mihoyo.com/Api/cookie_accountinfo_by_loginticket",
{
params: {
t: Date.now(),
login_ticket: cookieContainer.login_ticket,
},
// proxy: {
// host: "localhost",
// port: 8888,
// },
}
);
} catch (e) {
err(e);
return;
}
// let cookieArray = headers["set-cookie"];
// cookieArray.forEach((cookieStr) => {
// let cookie = queryToObj(cookieStr, "; ");
// });
if (data.code === 200) {
ok("Account info cookies obtained!");
let cookieInfo = data.data.cookie_info;
if (cookieInfo === null) {
err(data);
return;
} else {
cookieContainer.cookie_token = cookieInfo.cookie_token;
cookieContainer.account_id = cookieInfo.account_id;
fs.promises.writeFile(
"./cookies.json",
JSON.stringify(cookieContainer, null, 4)
);
}
} else {
err(data);
return;
}
}
//r------------
const api = axios.create({
baseURL: "https://hk4e-api-os.mihoyo.com",
responseType: "json",
headers: {
post: {
"Content-Type": "application/json",
},
Cookie: objToQuery(cookieContainer, "; ", "="),
},
//fiddler proxy
// proxy: {
// host: "localhost",
// port: 8888,
// },
});
//only ltoken & ltuid are needed
//or alternatively login_ticket, account_id & cookie_ticket
//({ headers, data } = await api.get("/event/sol/info", {
let { headers, data } = await api.get("/event/sol/info", {
params: {
lang: "en-us",
act_id: "e202102251931481",
},
});
let nextTry = null;
if (data.retcode === 0) {
({ data } = data);
nextTry = new Date(data.today);
//local UTC offset is already assumed (getHours), so we just sub 8 to get UTC+8 00:00 in local time
nextTry.setHours(nextTry.getHours() - 8, nextTry.getMinutes() + 5);
if (data.is_sign) {
warn("The reward for today has already been claimed");
waitForTheNext();
} else {
signDay();
}
} else {
err(data);
}
function waitForTheNext() {
nextTry.setDate(nextTry.getDate() + 1); //next day
accent(`Next try in ${nextTry}`);
setTimeout(signDay, nextTry.getTime() - Date.now());
}
async function signDay() {
try {
let { data } = await api.post(
"/event/sol/sign",
{
act_id: "e202102251931481", //event id
},
{
params: {
lang: "en-us",
},
}
);
if (data.retcode === 0) {
data = data.data;
if (data.code === "ok") {
ok("Successfully claimed the reward for today");
waitForTheNext();
} else {
err(data);
}
} else {
err(data);
}
} catch (e) {
err(e);
}
}
}