2
2
package moyucalendar
3
3
4
4
import (
5
- "bufio"
6
- "errors"
7
- "fmt"
8
- "io"
9
- "net/http"
10
- "net/url"
11
- "regexp"
12
- "strings"
13
- "time"
14
-
15
5
control "github.com/FloatTech/zbputils/control"
6
+ "github.com/FloatTech/zbputils/web"
16
7
zero "github.com/wdvxdr1123/ZeroBot"
17
8
"github.com/wdvxdr1123/ZeroBot/message"
18
-
19
- xpath "github.com/antchfx/htmlquery"
20
9
)
21
10
22
- var ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"
23
- var weixin = regexp .MustCompile (`url \+= '(.+)';` )
24
-
25
- var client = & http.Client {}
26
-
27
11
func init () {
28
12
control .Register ("moyucalendar" , & control.Options {
29
13
DisableOnDefault : true ,
@@ -34,159 +18,11 @@ func init() {
34
18
" - 摸鱼人日历" ,
35
19
}).OnFullMatch ("摸鱼人日历" ).SetBlock (true ).
36
20
Handle (func (ctx * zero.Ctx ) {
37
- title := fmt .Sprintf ("摸鱼人日历 %d月%d日" , time .Now ().Month (), time .Now ().Day ())
38
- sg , cookies , err := sougou (title , "摸鱼人日历" , ua )
39
- if err != nil {
40
- ctx .SendChain (message .Text ("ERROR:" , err ))
41
- return
42
- }
43
- wx , err := redirect (sg , cookies , ua )
44
- if err != nil {
45
- ctx .SendChain (message .Text ("ERROR:" , err ))
46
- return
47
- }
48
- image , err := calendar (wx , ua )
21
+ data , err := web .GetData ("https://api.vvhan.com/api/moyu" )
49
22
if err != nil {
50
23
ctx .SendChain (message .Text ("ERROR:" , err ))
51
24
return
52
25
}
53
- ctx .SendChain (message .Image ( image ))
26
+ ctx .SendChain (message .ImageBytes ( data ))
54
27
})
55
28
}
56
-
57
- func sougou (title , publisher , ua string ) (string , []* http.Cookie , error ) {
58
- u , _ := url .Parse ("https://weixin.sogou.com/weixin" )
59
- u .RawQuery = url.Values {
60
- "type" : []string {"2" },
61
- "s_from" : []string {"input" },
62
- "query" : []string {title },
63
- }.Encode ()
64
- req , err := http .NewRequest ("GET" , u .String (), nil )
65
- if err != nil {
66
- return "" , nil , err
67
- }
68
- req .Header .Set ("User-Agent" , ua )
69
- resp , err := client .Do (req )
70
- if err != nil {
71
- return "" , nil , err
72
- }
73
- defer resp .Body .Close ()
74
- if resp .StatusCode != http .StatusOK {
75
- return "" , nil , errors .New ("status not ok" )
76
- }
77
- // 解析XPATH
78
- doc , err := xpath .Parse (resp .Body )
79
- if err != nil {
80
- return "" , nil , err
81
- }
82
- // 取出每个返回的结果
83
- list := xpath .Find (doc , `//*[@class="news-list"]/li/div[@class="txt-box"]` )
84
- if len (list ) == 0 {
85
- return "" , nil , errors .New ("sougou result not found" )
86
- }
87
- var match string
88
- for i := range list {
89
- account := xpath .FindOne (list [i ], `//div[@class="s-p"]/a[@class="account"]` )
90
- if account == nil {
91
- continue
92
- }
93
- if xpath .InnerText (account ) != publisher {
94
- continue
95
- }
96
- target := xpath .FindOne (list [i ], `//h3/a[@target="_blank"]` )
97
- if target == nil {
98
- continue
99
- }
100
- match = xpath .SelectAttr (target , "href" )
101
- break
102
- }
103
- if match == "" {
104
- return "" , nil , errors .New ("sougou result not found" )
105
- }
106
- return "https://weixin.sogou.com" + match , resp .Cookies (), nil
107
- }
108
-
109
- func redirect (link string , cookies []* http.Cookie , ua string ) (string , error ) {
110
- req , err := http .NewRequest ("GET" , link , nil )
111
- if err != nil {
112
- return "" , err
113
- }
114
- req .Header .Set ("User-Agent" , ua )
115
- var c = make ([]string , 0 , 4 )
116
- for _ , cookie := range cookies {
117
- if cookie .Name != "ABTEST" && cookie .Name != "SNUID" &&
118
- cookie .Name != "IPLOC" && cookie .Name != "SUID" {
119
- continue
120
- }
121
- c = append (c , cookie .Name + "=" + cookie .Value )
122
- }
123
- req .Header .Set ("Cookie" , strings .Join (c , "; " ))
124
- resp , err := client .Do (req )
125
- if err != nil {
126
- return "" , err
127
- }
128
- defer resp .Body .Close ()
129
- if resp .StatusCode != http .StatusOK {
130
- return "" , errors .New ("status not ok" )
131
- }
132
- br := bufio .NewReader (resp .Body )
133
- var u = make ([]string , 0 )
134
- for {
135
- b , _ , err := br .ReadLine ()
136
- if err == io .EOF {
137
- break
138
- }
139
- if err != nil {
140
- return "" , err
141
- }
142
- matcha := weixin .FindStringSubmatch (string (b ))
143
- if len (matcha ) < 2 {
144
- continue
145
- }
146
- u = append (u , strings .ReplaceAll (matcha [1 ], "@" , "" ))
147
- }
148
- if len (u ) == 0 {
149
- return "" , errors .New ("weixin url not found" )
150
- }
151
- return strings .Join (u , "" ), nil
152
- }
153
-
154
- func calendar (link , ua string ) (string , error ) {
155
- req , err := http .NewRequest ("GET" , link , nil )
156
- req .Header .Set ("User-Agent" , ua )
157
- if err != nil {
158
- return "" , err
159
- }
160
- resp , err := client .Do (req )
161
- if err != nil {
162
- return "" , err
163
- }
164
- defer resp .Body .Close ()
165
- if resp .StatusCode != http .StatusOK {
166
- return "" , errors .New ("status not ok" )
167
- }
168
- doc , err := xpath .Parse (resp .Body )
169
- if err != nil {
170
- return "" , err
171
- }
172
- html := xpath .OutputHTML (doc , false )
173
- if ! strings .Contains (html , time .Now ().Format ("2006-01-02" )) {
174
- return "" , errors .New ("calendar not today" )
175
- }
176
- images := xpath .Find (doc , `//*[@id="js_content"]/p/img` )
177
- if images == nil {
178
- return "" , errors .New ("calendar not found" )
179
- }
180
- var image string
181
- for i := range images {
182
- if xpath .SelectAttr (images [i ], "data-w" ) != "540" {
183
- continue
184
- }
185
- image = xpath .SelectAttr (images [i ], "data-src" )
186
- break
187
- }
188
- if image == "" {
189
- return "" , errors .New ("image not found" )
190
- }
191
- return image , nil
192
- }
0 commit comments