-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (184 loc) · 4.92 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
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
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"math/rand"
"net/http"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/sirupsen/logrus"
)
var feishu string
func sendMsg(msg string) error {
// json
contentType := "application/json"
// data
data := map[string]interface{}{
"msg_type": "text",
"content": map[string]interface{}{
"text": fmt.Sprintf("文体中心羽毛球夜晚场地更新:\n%s", msg),
},
}
bs, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
return err
}
logrus.Infof("send msg: %s", string(bs))
// request
result, err := http.Post(feishu, contentType, bytes.NewReader(bs))
if err != nil {
fmt.Printf("post failed, err:%v\n", err)
return err
}
defer result.Body.Close()
if result.StatusCode/100 != 2 {
fmt.Printf("post failed, status:%v\n", result.StatusCode)
return fmt.Errorf("post failed, status:%v", result.StatusCode)
}
return nil
}
var preMsg = "fake msg"
func sendAlert(alertInfo []string) {
msg := strings.Join(alertInfo, "\n")
if preMsg == msg {
return
}
err := sendMsg(msg)
if err != nil {
logrus.Errorf("send msg: %v", err)
}
preMsg = msg
}
var pageId = 753
var interval = 5
var startTime time.Time
var endTime time.Time
func check() {
alertInfo := make([]string, 0)
dates := make([]string, 0)
currentDateIndex := -1
var preUrl string
var cookie []*http.Cookie
for {
url := fmt.Sprintf("https://xihuwenti.juyancn.cn/wechat/product/details?id=%d", pageId)
if currentDateIndex >= 0 {
url = fmt.Sprintf("%s&time=%s", url, dates[currentDateIndex])
}
logrus.WithField("url", url).Info("fetch page")
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
logrus.WithField("url", url).WithError(err).Error("new request error")
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36")
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
if preUrl != "" {
req.Header.Set("Refer", preUrl)
}
for _, c := range cookie {
req.AddCookie(c)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
logrus.WithField("url", url).WithError(err).Error("get page error")
return
}
defer res.Body.Close()
if res.StatusCode != 200 {
logrus.WithField("url", url).WithField("status", res.Status).Error("status code error")
return
}
if cookie == nil {
cookie = res.Cookies()
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
logrus.WithField("url", url).WithError(err).Errorf("parse document")
return
}
texts := make([]string, 0)
for _, n := range doc.Find(".date-box .cur span").Nodes {
for c := n.FirstChild; c != nil; c = c.NextSibling {
texts = append(texts, c.Data)
}
}
currentDateText := strings.Join(texts, " ")
// Find the available court items
doc.Find(".can-select").Each(func(i int, s *goquery.Selection) {
start, _ := s.Attr("data-start")
end, _ := s.Attr("data-end")
hall, _ := s.Attr("data-hall_name")
startT, err := time.Parse("15:04", start)
if err != nil {
logrus.Errorf("parse start time: %v", err)
return
}
endT, err := time.Parse("15:04", end)
if err != nil {
logrus.Errorf("parse end time: %v", err)
return
}
if startT.Sub(startTime) >= 0 && endT.Sub(endTime) <= 0 {
info := fmt.Sprintf("%s, %s-%s %s", currentDateText, start, end, hall)
alertInfo = append(alertInfo, info)
}
})
// Find the date items
if currentDateIndex == -1 {
doc.Find(".date-box li").Each(func(i int, s *goquery.Selection) {
if s.HasClass("cur") {
return
}
date, ok := s.Attr("data-time")
if ok {
dates = append(dates, date)
} else {
logrus.WithField("url", url).Warn("not founed the date-time attribute")
}
})
}
logrus.Infof("check done %s", currentDateText)
currentDateIndex++
if currentDateIndex >= len(dates) {
break
}
preUrl = url
time.Sleep(time.Duration(interval) * time.Second)
}
sendAlert(alertInfo)
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
start := "18:00"
end := "23:00"
flag.IntVar(&interval, "interval", interval, "check interval")
flag.IntVar(&pageId, "pageID", pageId, "page id, 753:羽毛球")
flag.StringVar(&feishu, "feishu", "", "feishu webhook url")
flag.StringVar(&start, "start", start, "court start time")
flag.StringVar(&end, "end", end, "court end time")
flag.Parse()
var err error
startTime, err = time.Parse("15:04", start)
if err != nil {
logrus.WithError(err).Errorf("parse start time")
return
}
endTime, err = time.Parse("15:04", end)
if err != nil {
logrus.WithError(err).Errorf("parse end time")
return
}
for {
check()
time.Sleep(time.Second * time.Duration(rand.Intn(interval)))
}
}