-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (95 loc) · 2.16 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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"time"
"strings"
"encoding/json"
"bytes"
"os"
"bufio"
"io"
)
type MessageSt struct {
User string `json:"user"`
Message string `json:"message"`
}
type recevicer struct {
name string
location string
}
var httpclient = http.Client{
CheckRedirect: nil,
Timeout: 60 * time.Second,
}
func getWeather(url string) string {
fmt.Println("start")
tip := ""
request, _ := http.NewRequest("GET", url, nil)
request.Header.Add("User-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36")
resp, err := httpclient.Do(request)
defer resp.Body.Close()
if err == nil {
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
re := regexp.MustCompile(`<metaname="description"content="(.*)">`)
str := strings.Replace(string(body), " ", "", -1)
find := re.FindStringSubmatch(str)
if len(find) > 1 {
return strings.Replace(find[1], "墨迹天气建议您", "建议", -1)
}
}
}
return tip
}
func sendMessage(rer recevicer) {
url := "http://localhost:8888/send"
message := MessageSt{rer.name, getWeather("https://tianqi.moji.com/weather/china/" + rer.location)}
b, err := json.Marshal(message)
resp, err := http.Post(url, "application/json;charset=utf-8", bytes.NewBuffer([]byte(b)))
if err == nil {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
fmt.Printf("receive message:%s\n", body)
}
} else {
return
}
}
func sendMessages(rers []recevicer) {
for _, v := range rers {
sendMessage(v)
}
}
func main() {
var file = "./receivers.conf"
if len(os.Args) == 2 {
file = os.Args[1]
}
f, err := os.Open(file)
if err == nil && f != nil {
var receivers []recevicer
defer f.Close()
rd := bufio.NewReader(f)
for {
line, err := rd.ReadString('\n')
if err != nil || io.EOF == err {
break
}
if len(line) > 0 {
if line[:1] != "#" {
s := strings.Split(line, `|-|`)
if len(s) == 2 {
receivers = append(receivers, recevicer{s[0], s[1]})
}
}
}
}
sendMessages(receivers)
} else {
fmt.Printf("未找到文件:%s\n", file)
}
}