-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.go
149 lines (134 loc) · 3.31 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"regexp"
"strings"
)
func main() {
client := &http.Client{}
key, err := GetSignKey(client)
if key != "" {
SignIn(client, key)
} else{
fmt.Println("错误:", err)
os.Exit(1)
}
}
func extractSign(input string) (string, error) {
pattern := `var sign = "(.*?)";`
re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(input)
// 判断是否未登录
if len(matches) > 1 {
return matches[1], nil
}else if strings.Contains(input, "请登录") {
return "", fmt.Errorf("未登录!请更新Cookie后重新运行!")
} else {
return "", fmt.Errorf("sign not found")
}
}
// 获取Key
func GetSignKey(client *http.Client) (string, error) {
urlStr := "https://www.hifini.com/"
cookie := os.Getenv("COOKIE")
if cookie == "" {
fmt.Println("COOKIE不存在,请检查是否添加")
return "", nil
}
//提交请求
formData := url.Values{}
req, err := http.NewRequest("POST", urlStr, strings.NewReader(formData.Encode()))
if err != nil {
panic(err)
}
req.Header.Add("Cookie", cookie)
req.Header.Add("x-requested-with", "XMLHttpRequest")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
//处理返回结果
response, err := client.Do(req)
if err != nil {
panic(err)
}
defer response.Body.Close()
buf, _ := ioutil.ReadAll(response.Body)
sign, err := extractSign(string(buf))
if err != nil {
fmt.Println("获取Sign失败:", err)
return sign, err
} else {
fmt.Println("获取Sign成功:", sign)
return sign, nil
}
}
// SignIn 签到
func SignIn(client *http.Client, key string) bool {
//生成要访问的url
urlStr := "https://www.hifini.com/sg_sign.htm"
cookie := os.Getenv("COOKIE")
if cookie == "" {
fmt.Println("COOKIE不存在,请检查是否添加")
return false
}
if key == "" {
fmt.Println("KEY获取失败,请检查Cookie是否过期")
return false
}
//提交请求
formData := url.Values{}
formData.Set("sign", key)
req, err := http.NewRequest("POST", urlStr, strings.NewReader(formData.Encode()))
if err != nil {
panic(err)
}
req.Header.Add("Cookie", cookie)
req.Header.Add("x-requested-with", "XMLHttpRequest")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
//处理返回结果
response, err := client.Do(req)
if err != nil {
panic(err)
}
defer response.Body.Close()
buf, _ := ioutil.ReadAll(response.Body)
fmt.Println("签到结果:")
fmt.Println(string(buf))
hasDing := os.Getenv("DINGDING_WEBHOOK")
if hasDing != "" {
dingding(string(buf))
} else {
fmt.Println("DINGDING_WEBHOOK 环境变量未定义,跳过通知步骤")
}
return strings.Contains(string(buf), "成功")
}
func dingding(result string) {
// 构造要发送的消息
message := struct {
MsgType string `json:"msgtype"`
Text struct {
Content string `json:"content"`
} `json:"text"`
}{
MsgType: "text",
Text: struct {
Content string `json:"content"`
}{
Content: "HiFiNi:\n" + result,
},
}
// 将消息转换为JSON格式
messageJson, _ := json.Marshal(message)
DINGDING_WEBHOOK := os.Getenv("DINGDING_WEBHOOK")
// 发送HTTP POST请求
resp, err := http.Post(DINGDING_WEBHOOK,
"application/json", bytes.NewBuffer(messageJson))
if err != nil {
panic(err)
}
defer resp.Body.Close()
}