-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.go
188 lines (167 loc) · 4.68 KB
/
search.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
package wxsg
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
const (
WeixinSogouUrl = "https://weixin.sogou.com"
SearchUrlFormat = WeixinSogouUrl + "/weixin?ie=utf8&type=%d&page=%d&query=%s"
)
type AccountInfo struct {
Name string
Url string
Avatar string
QRCode string
WeixinID string
// 可从 /websearch/weixin/pc/anti_account.jsp 可获得月发文数
// Activity string
Introduction string
Identify string
LatestArticleTitle string
LatestArticleUrl string
LatestArticlePubTime time.Time
}
type ArticleInfo struct {
Title string
Url string
Preview string
AccName string
AccUrl string
PubTime time.Time
Image string
}
func (c Client) SearchAccount(query string, page int) (results []AccountInfo, err error) {
if page < 1 {
page = 1
}
query = strings.ReplaceAll(query, " ", "+")
req := c.buildGetReq(SearchUrlFormat, 1, page, query)
resp, err := c.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return
}
resultsSele := doc.Find("#main > div.news-box > ul").Children()
if resultsSele.Length() == 0 {
err = errors.New("no results")
return
}
resultsSele.Each(func(i int, s *goquery.Selection) {
result := AccountInfo{}
s.Children().Each(func(i int, s *goquery.Selection) {
key := s.Find("dt").Text()
switch {
case key == "":
nameSele := s.Find("div.txt-box > p.tit > a")
result.Name = nameSele.Text()
if href, ok := nameSele.Attr("href"); ok {
result.Url = WeixinSogouUrl + href
}
if src, ok := s.Find("div.img-box > a > img").Attr("src"); ok {
result.Avatar = "https:" + src
}
if src, ok := s.Find("div.ew-pop > span > img:nth-child(3)").Attr("src"); ok {
result.QRCode = src
}
result.WeixinID = s.Find("div.txt-box > p.info > label").Text()
case strings.Contains(key, "功能介绍"):
result.Introduction = s.Find("dd").Text()
case strings.Contains(key, "认证"):
result.Identify = strings.TrimSpace(s.Find("dd").Text())
case strings.Contains(key, "最近文章"):
articleSele := s.Find("dd > a")
result.LatestArticleTitle = articleSele.Text()
if href, ok := articleSele.Attr("href"); ok {
result.LatestArticleUrl = WeixinSogouUrl + href
}
if ret := pubTimeScript.FindStringSubmatch(s.Find("dd > span > script").Text()); len(ret) == 2 {
t, _ := strconv.ParseInt(ret[1], 10, 64)
result.LatestArticlePubTime = time.Unix(t, 0)
}
}
})
results = append(results, result)
})
return
}
func (c Client) SearchArticle(query string, page int) (results []ArticleInfo, err error) {
if page < 1 {
page = 1
}
query = strings.ReplaceAll(query, " ", "+") // + "-" 加个无用字符能似乎提升准确率?
req := c.buildGetReq(SearchUrlFormat, 2, page, query)
resp, err := c.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return
}
resultsSele := doc.Find("#main > div.news-box > ul").Children()
if resultsSele.Length() == 0 {
err = errors.New("no results")
return
}
resultsSele.Each(func(i int, s *goquery.Selection) {
titleSele := s.Find("div.txt-box > h3 > a")
accountSele := s.Find("div.txt-box > div > a")
result := ArticleInfo{
Title: titleSele.Text(),
Preview: s.Find("div.txt-box > p").Text(),
AccName: accountSele.Text(),
}
if href, ok := titleSele.Attr("href"); ok {
result.Url = WeixinSogouUrl + href
}
if href, ok := accountSele.Attr("href"); ok {
result.AccUrl = WeixinSogouUrl + href
}
if ret := pubTimeScript.FindStringSubmatch(s.Find("div.txt-box > div > span > script").Text()); len(ret) == 2 {
t, _ := strconv.ParseInt(ret[1], 10, 64)
result.PubTime = time.Unix(t, 0)
}
if src, ok := s.Find("div.img-box > a > img").Attr("src"); ok {
result.Image = "https:" + src
}
results = append(results, result)
})
return
}
// 搜索获得的文章地址往往地址指向了一个中间页面, 需要拼接字符串获得真实地址
func (c Client) GetArticleRealUrl(url string) (url2 string, err error) {
resp, err := c.Do(c.buildGetReq(url))
if err != nil {
return
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if !bytes.Contains(buf, []byte(`url.replace("@", "");`)) {
err = fmt.Errorf("unexpect page: %s", string(buf))
return
}
ret := urlMergeCmdReg.FindAllSubmatch(buf, -1)
for _, result := range ret {
if len(result) != 2 {
err = fmt.Errorf("unexpect page: %s", string(buf))
return
}
url2 += string(result[1])
}
url2 = strings.ReplaceAll(url2, "@", "")
return
}