-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
275 lines (264 loc) · 7.67 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
const fetch = require('node-fetch');
const sendMail = require('./sendMail');
const [cookie, user, pass, to, isBug] = process.argv.slice(2);
process.env.user = user;
process.env.pass = pass;
const headers = {
'content-type': 'application/json; charset=utf-8',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
'sec-ch-ua': '"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
authority: 'api.juejin.cn',
referer: 'https://juejin.cn/',
accept: '*/*',
cookie
};
const DRAW_CHANCE = 'https://api.juejin.cn/growth_api/v1/lottery_config/get' // 查询今日是否有免费抽奖机会
const DRAW_LAUNCH = 'https://api.juejin.cn/growth_api/v1/lottery/draw' // 执行免费抽奖
const QUERY_IS_CHECKIN = 'https://api.juejin.cn/growth_api/v1/get_today_status' // 查询今日是否已经签到
const CHECKIN_LAUNCH = 'https://api.juejin.cn/growth_api/v1/check_in' // 执行签到
const QUERY_CURRENT_POINT = 'https://api.juejin.cn/growth_api/v1/get_cur_point' // 查询当前积分
const QUERY_LUCK_LIST = 'https://api.juejin.cn/growth_api/v1/lottery_history/global_big' // 查询可粘福气列表
const QUERY_MY_LUCK = 'https://api.juejin.cn/growth_api/v1/lottery_lucky/my_lucky' // 查询我的粘福气
const DIP_LUCK = 'https://api.juejin.cn/growth_api/v1/lottery_lucky/dip_lucky' // 粘福气
// const NOT_COLLECT_LIST = 'https://api.juejin.cn/user_api/v1/bugfix/not_collect' // 未采集的bug列表
// const COLLECT_BUG = 'https://api.juejin.cn/user_api/v1/bugfix/collect' // 采集bug
// compose 组合函数
const compose = function (handles) {
return handles.reduceRight((prev, next) => {
return prev.then(next)
}, Promise.resolve())
}
// 发送邮件
function doSendMail(preResult) {
const { doDrawResult, isSuccess, isCollectBug } = preResult;
let html = "";
if (isCollectBug) {
if (!preResult.bugs) return;
html = `<p>今日采集bug数:${preResult.bugs}</p>`;
} else if (!isSuccess) {
html = `
<h1 style="text-align: center">签到 + 粘福气 失败!!</h1>
<p style="text-indent: 2em">签到执行结果: 失败</p>
<p style="text-indent: 2em">失败原因: ${JSON.stringify(
doDrawResult.errorObj
)}</p>
`;
}
if (!html) return Promise.resolve("");
return sendMail({
from: '掘金',
to,
subject: '掘金定时签到',
html
})
}
// 获取未采集的bug列表
function queryBugList(preResult = {}) {
return fetch(NOT_COLLECT_LIST, {
headers,
method: 'POST',
credentials: 'include',
body: JSON.stringify({})
}).then((res) => res.json()).then(res => {
if (!res.data) {
console.log('获取bug列表错误', res)
throw new Error(res)
}
return {...preResult, bugs: res.data, isSuccess: true, isCollectBug: true}
})
}
// bug 采集
function collectBug(preResult){
function generateAction(bug) {
return fetch(COLLECT_BUG, {
headers,
method: 'POST',
credentials: 'include',
body: JSON.stringify({
bug_time: bug.bug_time,
bug_type: bug.bug_type
})
}).then((res) => res.json()).catch(e => {
console.log('采集bug错误', e)
})
}
if (!preResult.bugs.length) {
return Promise.resolve({
...preResult,
bugs: 0
})
}
const actions = preResult.bugs.map(bug => generateAction(bug))
return Promise.all(actions).then(res => {
return {...preResult, bugs: res.length}
})
}
// 检查今天是否已签到
function isCheckIn() {
return fetch(QUERY_IS_CHECKIN, {
headers,
method: 'GET',
credentials: 'include'
}).then((res) => res.json()).then(res => {
const errorMsg = res.err_no !== 0 ? '签到失败!' : res.data ? '今日已经签到!' : ''
return {
status: !errorMsg,
errorMsg
}
})
}
// 执行今日签到
function doCheckIn(isCheckInResult) {
const doCheckInResult = {
status: isCheckInResult.status,
point: 0,
errorMsg: isCheckInResult.errorMsg
}
if (!isCheckInResult.status) return Promise.resolve({isCheckInResult, doCheckInResult});
return fetch(CHECKIN_LAUNCH, {
headers,
method: 'POST',
credentials: 'include'
}).then((res) => res.json()).then(res => {
if (!res.data) throw new Error(res)
const errorMsg = res.err_no !== 0 ? '签到异常!' : ''
return {
isCheckInResult,
doCheckInResult: {
status: !errorMsg,
point: res.data.sum_point,
errorMsg
}
}
});
}
// 查询当前的积分
function queryCurrentPoint(preResult) {
return fetch(QUERY_CURRENT_POINT, {
headers,
method: 'GET',
credentials: 'include'
}).then((res) => res.json()).then(res => {
if (!res.data) throw new Error(res)
return {...preResult, score: res.data}
});
}
// 查询当前是否有免费抽奖机会
function queryDrawChance(preResult) {
return fetch(DRAW_CHANCE, {
headers,
method: 'GET',
credentials: 'include'
}).then((res) => res.json()).then(res => {
if (!res.data) throw new Error(res)
const errorMsg = res.err_no !== 0 ? '已经签到!免费抽奖失败!' : res.data.free_count === 0 ? '签到成功!今日已经免费抽奖!' : '';
return {
...preResult,
drawChanceResult: {
status: !errorMsg,
errorMsg
},
}
});
}
// 执行免费抽奖
function doDraw(preResult) {
const doDrawResult = {
status: preResult.drawChanceResult.status,
errorMsg: preResult.drawChanceResult.errorMsg
}
if (!preResult.drawChanceResult.status) return Promise.resolve({...preResult, doDrawResult});
return fetch(DRAW_LAUNCH, {
headers,
method: 'POST',
credentials: 'include'
}).then((res) => res.json()).then(res => {
if (!res.data) throw new Error(res)
const errorMsg = res.err_no !== 0 ? '已经签到!免费抽奖异常!' : ''
return {
...preResult,
doDrawResult: {
...res.data,
status: !errorMsg,
errorMsg
}
}
});
}
// 粘福气
function dipLucky(preResult) {
return fetch(DIP_LUCK, {
headers,
method: 'POST',
credentials: 'include',
body: JSON.stringify({
lottery_history_id: preResult.luckList[0]
})
}).then((res) => res.json()).then(res => {
if (!res.data) throw new Error(res)
return { ...preResult, dipLuckyResult: res }
});
}
// 查询我的福气值
function queryMylucky(preResult) {
return fetch(QUERY_MY_LUCK, {
headers,
method: 'POST',
credentials: 'include'
}).then((res) => res.json()).then(res => {
if (!res.data) throw new Error(res)
return { ...preResult, luckvalue: res.data.total_value, isSuccess: true }
})
}
// 查询可粘福气列表
function queryLuckList(preResult) {
return fetch(QUERY_LUCK_LIST, {
headers,
method: 'POST',
credentials: 'include',
body: JSON.stringify({
page_no: 1,
page_size: 5
})
}).then((res) => res.json()).then(res => {
if (!res.data) throw new Error(res)
const lunks = res.data.lotteries.map(lottiem => lottiem.history_id)
return {...preResult, luckList: lunks}
})
}
let tasks = [
doSendMail,
// collectBug,
// queryBugList,
queryMylucky,
dipLucky,
queryLuckList,
queryCurrentPoint,
doDraw,
queryDrawChance,
doCheckIn,
isCheckIn
]
if (isBug) {
tasks = [
doSendMail,
collectBug,
queryBugList,
]
}
compose(tasks)
.then(() => console.log('流水线执行成功'))
.catch(err => {
console.log('执行异常', JSON.stringify(err))
doSendMail({
isSuccess: false,
doDrawResult: {
errorMsg: '流水线执行异常',
score: 0,
errorObj: err
}
})
})