/
checkin.go
186 lines (170 loc) · 5.57 KB
/
checkin.go
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/tidwall/gjson"
)
var (
refresh_Token string
pushplus_token string
updateAccesssTokenURL = "https://auth.aliyundrive.com/v2/account/token"
signinURL = "https://member.aliyundrive.com/v1/activity/sign_in_list"
)
type aliyundrive struct {
refreshToken string
accessToken string
}
func New(refreshToken string) *aliyundrive {
return &aliyundrive{refreshToken: refreshToken}
}
func (a *aliyundrive) getAccessToken() {
body := map[string]string{
"grant_type": "refresh_token",
"refresh_token": refresh_Token,
}
b := bytes.NewBuffer(nil)
json.NewEncoder(b).Encode(body)
rsp, err := http.Post(updateAccesssTokenURL, "application/json", b)
if err != nil {
log.Println(err)
return
}
bytersp, _ := io.ReadAll(rsp.Body)
a.accessToken = gjson.GetBytes(bytersp, "access_token").String()
a.refreshToken = gjson.GetBytes(bytersp, "refresh_token").String()
// log.Printf("%#v\n", string(bytersp))
}
type refreshToken struct {
GrantType string `json:"grant_Type,omitempty"`
RefreshToken string `json:"refresh_Token,omitempty"`
Phone string
}
type Signrsp struct {
Success bool `json:"success,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
TotalCount string `json:"totalCount,omitempty"`
NextToken string `json:"nextToken,omitempty"`
MaxResults string `json:"maxResults,omitempty"`
Result struct {
Subject string `json:"subject,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
IsReward bool `json:"isReward,omitempty"`
Blessing string `json:"blessing,omitempty"`
SignInCount int `json:"signInCount,omitempty"`
SignInCover string `json:"signInCover,omitempty"`
SignInRemindCover string `json:"signInRemindCover,omitempty"`
RewardCover string `json:"rewardCover,omitempty"`
SignInLogs []SignInLogs `json:"signInLogs,omitempty"`
} `json:"result,omitempty"`
Arguments string `json:"arguments,omitempty"`
}
type SignInLogs struct {
Day int `json:"day,omitempty"`
Status string `json:"status,omitempty"`
Icon string `json:"icon,omitempty"`
Notice string `json:"notice,omitempty"`
Type string `json:"type,omitempty"`
Themes string `json:"themes,omitempty"`
CalendarChinese string `json:"calendarChinese,omitempty"`
CalendarDay string `json:"calendarDay,omitempty"`
CalendarMonth string `json:"calendarMonth,omitempty"`
Poster struct {
Name string `json:"name,omitempty"`
Reason string `json:"reason,omitempty"`
Background string `json:"background,omitempty"`
Color string `json:"color,omitempty"`
Action string `json:"action,omitempty"`
} `json:"poster,omitempty"`
Reward struct {
GoodsID int `json:"goodsId,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Background string `json:"background,omitempty"`
Color string `json:"color,omitempty"`
Action string `json:"action,omitempty"`
Notice string `json:"notice,omitempty"`
} `json:"reward,omitempty"`
IsReward bool `json:"isReward,omitempty"`
}
func (a aliyundrive) signIn() error {
req, err := http.NewRequest("POST", signinURL, strings.NewReader("{}"))
if err != nil {
log.Printf("签到失败, 错误信息: %v\n", err)
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.accessToken))
req.Header.Set("Content-Type", "application/json")
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
log.Printf("签到失败, 错误信息: %v\n", err)
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("签到失败, 错误信息: %v\n", err)
return err
}
if gjson.GetBytes(body, "success").Bool() == true {
count := gjson.GetBytes(body, "result.signInCount").Int()
result := gjson.GetBytes(body, fmt.Sprintf("result.signInLogs.%d", count-1))
ltime, _ := time.LoadLocation("Asia/Shanghai")
fmt.Println("东八区时间:", time.Now().Local().In(ltime).Format("2006-01-02 15:04:05"))
log.Printf("签到成功,已签到%d天,本月第%d日\n", count, result.Get("calendarDay").Int())
if result.Get("isReward").Bool() { //奖励
log.Printf("%s\n", result.Get("notice").String())
} else {
log.Println("无奖励")
}
// log.Println(result.String()
} else {
log.Println(gjson.GetBytes(body, "message").String())
return fmt.Errorf("签到失败:%s\n", gjson.GetBytes(body, "message").String())
}
return nil
}
func pushplus(content string) {
v := url.Values{}
v.Add("token", pushplus_token)
v.Add("title", "阿里云盘签到")
v.Add("content", content)
pushplus_url := "http://www.pushplus.plus/send?" + v.Encode()
req, _ := http.NewRequest("GET", pushplus_url, nil)
req.Header.Set("Content-Type", "application/json")
_, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("发送通知失败,%s", err.Error())
}
}
func main() {
rt := os.Getenv("REFRESH_TOKENS")
if rt == "" {
panic("未配置阿里云盘REFRESH_TOKENS")
} else {
refresh_Token = rt
}
pptoken := os.Getenv("PUSHPLUS_TOKEN")
if pptoken == "" {
panic("未配置PUSHPLUS_TOKEN")
} else {
pushplus_token = pptoken
}
a := New(refresh_Token)
a.getAccessToken()
err := a.signIn()
if err != nil {
pushplus(err.Error())
}
}