-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcheck_in.js
83 lines (67 loc) · 2.63 KB
/
check_in.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
const request = require('request'),
login_url = "https://web.everphoto.cn/api/auth",
check_in_url = "https://api.everphoto.cn/users/self/checkin/v2",
config = {
'dingtalk': {
'access_token': get_environment_var('DINGTALK_ACCESS_TOKEN'),
'secret': get_environment_var('DINGTALK_SECRET')
},
'ever_photo': {
'mobile': get_environment_var('EVER_PHOTO_MOBILE'),
'password': get_environment_var('EVER_PHOTO_PASSWORD')
}
};
function get_environment_var(name) {
if (process.env.hasOwnProperty(name)) {
return process.env[name];
}
return '';
}
function dingtalk_notice(message) {
if (null === get_environment_var('DINGTALK_ACCESS_TOKEN')) {
return;
}
let crypto = require('crypto');
let timestamp = Date.now();
let sign = crypto.createHmac('sha256', config.dingtalk.secret).update(timestamp + '\n' + config.dingtalk.secret).digest('base64');
let webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=' + config.dingtalk.access_token + '×tamp=' + timestamp + '&sign=' + sign;
if (typeof message === 'object') {
message = JSON.stringify(message);
}
let option = {
'url': webhook_url, 'headers': {'Content-Type': 'application/json;charset=utf-8'}, 'json': {
'msgtype': 'text',
'text': {
'content': message
}
}
}
request.post(option, (error, response) => {
if (error || 0 !== response.body.errcode) throw new Error('Send dingtalk message fail !');
console.log(response.body);
});
}
dingtalk_notice('start checkin');
request.post(
{
'url': login_url,
'headers': {},
formData: {'mobile': config.ever_photo.mobile, 'password': config.ever_photo.password}
},
(error, response) => {
if (error || 0 !== JSON.parse(response.body).code) {
throw new Error('Ever photo login failed !');
}
request.post(
{'url': check_in_url, headers: {'authorization': 'Bearer ' + JSON.parse(response.body).data.token}},
(error, response) => {
if (error || true !== JSON.parse(response.body).data.checkin_result) {
throw new Error('Check in failed !');
}
let reward_info = JSON.parse(response.body).data;
dingtalk_notice('签到完成!已经连续签到' + reward_info.continuity + '天,签到总共获得' + reward_info.total_reward / 1048576 + 'M。');
});
});
process.on('uncaughtException', (e) => {
dingtalk_notice(e.message);
});