-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
109 lines (99 loc) · 2.54 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
package main
import (
"encoding/json"
"fmt"
"github.com/PuerkitoBio/goquery"
"gopkg.in/gomail.v2"
"io"
"log"
"net/http"
"os"
"regexp"
"strings"
)
var (
fundCodeStr, watchWeekDay, watchMonthDay, emailName, emailPassword string
)
var (
fundCodeSlice []string
)
func init() {
fundCodeStr = os.Getenv("WATCH_FUND_CODE")
//fundCodeStr = "005827,012414,003095,161005"
fundCodeSlice = strings.Split(fundCodeStr, ",")
watchWeekDay = os.Getenv("WATCH_WEEK_DAY")
watchMonthDay = os.Getenv("WATCH_MONTH_DAY")
emailName = os.Getenv("EMAIL_NAME")
emailPassword = os.Getenv("EMAIL_PASSWORD")
}
func fetchFund(codes []string) []map[string]string {
var fundResult []map[string]string
var weeklyChange string
var oneMonthChange string
fmt.Printf("codes: %#v\n", codes)
for _, code := range codes {
fundJsUrl := FundJsUrl + code + ".js"
resp1, err := http.Get(fundJsUrl)
if err != nil {
panic(err)
}
defer resp1.Body.Close()
fundHTMLUrl := FundHTMLUrl + code + ".html"
fmt.Printf("fundHTMLUrl: %s\n", string(fundHTMLUrl))
resp2, err := http.Get(fundHTMLUrl)
if err != nil {
panic(err)
}
defer resp2.Body.Close()
re, _ := regexp.Compile("jsonpgz\\((.*)\\);")
data, err := io.ReadAll(resp1.Body)
if err != nil {
panic(err)
}
ret := re.FindSubmatch(data)
fundData := ret[1]
fmt.Printf("fundData: %s\n", string(fundData))
doc, err2 := goquery.NewDocumentFromReader(resp2.Body)
if err != nil {
log.Fatal(err2)
}
doc.Find("#increaseAmount_stage > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2)").Each(func(i int, s *goquery.Selection) {
s.Find("td > div").Each(func(j int, k *goquery.Selection) {
change := k.Text()
switch j {
case 1:
weeklyChange = change
case 2:
oneMonthChange = change
}
})
})
var fundDataMap map[string]string
if err := json.Unmarshal(fundData, &fundDataMap); err == nil {
fundDataMap["weeklyChange"] = weeklyChange
fundDataMap["oneMonthChange"] = oneMonthChange
fundResult = append(fundResult, fundDataMap)
}
}
fmt.Printf("fundResult: %#v\n", fundResult)
return fundResult
}
func sendEmail(content string) {
if content == "" {
return
}
m := gomail.NewMessage()
m.SetHeader("From", emailName)
m.SetHeader("To", emailName)
m.SetHeader("Subject", "基金涨跌监控")
m.SetBody("text/html", content)
d := gomail.NewDialer("smtp.qq.com", 587, emailName, emailPassword)
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
func main() {
fundResult := fetchFund(fundCodeSlice)
content := renderHTML(fundResult)
sendEmail(content)
}