forked from 15921483570/wechat_spider
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocessor.go
312 lines (281 loc) · 7.48 KB
/
processor.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package wechat_spider
import (
"bytes"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/elazarl/goproxy"
"github.com/palantir/stacktrace"
)
type Processor interface {
ProcessList(resp *http.Response, ctx *goproxy.ProxyCtx) ([]byte, error)
ProcessDetail(resp *http.Response, ctx *goproxy.ProxyCtx) ([]byte, error)
ProcessMetrics(resp *http.Response, ctx *goproxy.ProxyCtx) ([]byte, error)
ProcessPages() error
Output()
}
type BaseProcessor struct {
req *http.Request
lastId string
data []byte
urlResults []*UrlResult
detailResult *DetailResult
historyUrl string
biz string
// The index of urls for detail page
currentIndex int
checked bool
Type string
}
type (
UrlResult struct {
Mid string
// url
Url string
_URL *url.URL
}
DetailResult struct {
Id string
Url string
Data []byte
Appmsgstat *MsgStat `json:"appmsgstat"`
}
MsgStat struct {
ReadNum int `json:"read_num"`
LikeNum int `json:"like_num"`
RealReadNum int `json:"real_read_num"`
}
)
var (
replacer = strings.NewReplacer(
"\t", "", " ", "",
""", `"`, " ", "",
`\\`, "", "&", "&",
"&", "&", `\`, "",
)
urlRegex = regexp.MustCompile(`http://mp.weixin.qq.com/s?[^#"',]*`)
idRegex = regexp.MustCompile(`"id":(\d+)`)
MsgNotFound = errors.New("MsgLists not found")
TypeList = "list"
TypeDetail = "detail"
TypeMetric = "metric"
)
func NewBaseProcessor() *BaseProcessor {
return &BaseProcessor{}
}
func (p *BaseProcessor) init(req *http.Request, data []byte) (err error) {
p.req = req
p.data = data
p.currentIndex = -1
p.biz = req.URL.Query().Get("__biz")
p.logf("Running a new wechat processor, please wait...")
return nil
}
func (p *BaseProcessor) ProcessList(resp *http.Response, ctx *goproxy.ProxyCtx) (data []byte, err error) {
p.Type = TypeList
var buf bytes.Buffer
if _, err = buf.ReadFrom(resp.Body); err != nil {
return
}
if err = resp.Body.Close(); err != nil {
return
}
data = buf.Bytes()
if err = p.init(ctx.Req, data); err != nil {
return
}
if err = p.processMain(); err != nil {
return
}
return
}
func (p *BaseProcessor) ProcessDetail(resp *http.Response, ctx *goproxy.ProxyCtx) (data []byte, err error) {
p.Type = TypeDetail
p.req = ctx.Req
p.currentIndex++
var buf bytes.Buffer
if _, err = buf.ReadFrom(resp.Body); err != nil {
return
}
if err = resp.Body.Close(); err != nil {
return
}
data = buf.Bytes()
p.detailResult = &DetailResult{Id: genId(p.req.URL.String()), Url: p.req.URL.String(), Data: data}
return
}
func (p *BaseProcessor) ProcessMetrics(resp *http.Response, ctx *goproxy.ProxyCtx) (data []byte, err error) {
p.Type = TypeMetric
p.req = ctx.Req
var buf bytes.Buffer
if _, err = buf.ReadFrom(resp.Body); err != nil {
return
}
if err = resp.Body.Close(); err != nil {
return
}
data = buf.Bytes()
detailResult := &DetailResult{}
e := json.Unmarshal(data, detailResult)
if e != nil {
p.logf("error in parsing json %s\n", string(data))
}
detailResult.Url = p.req.Referer()
detailResult.Id = genId(detailResult.Url)
p.detailResult = detailResult
return
}
func (p *BaseProcessor) Sleep() {
ti := rand.Intn(rootConfig.SleepSecond) + 1
time.Sleep(time.Duration(ti) * time.Second)
}
func (p *BaseProcessor) UrlResults() []*UrlResult {
return p.urlResults
}
func (p *BaseProcessor) DetailResult() *DetailResult {
return p.detailResult
}
func (p *BaseProcessor) GetRequest() *http.Request {
return p.req
}
func (p *BaseProcessor) Output() {
urls := []string{}
fmt.Println("result => [")
for _, r := range p.urlResults {
urls = append(urls, r.Url)
}
fmt.Println(strings.Join(urls, ","))
fmt.Println("]")
}
//Parse the html
func (p *BaseProcessor) processMain() error {
p.urlResults = make([]*UrlResult, 0, 100)
buffer := bytes.NewBuffer(p.data)
var msgs string
str, err := buffer.ReadString('\n')
for err == nil {
if strings.Contains(str, "msgList = ") {
msgs = str
break
}
str, err = buffer.ReadString('\n')
}
if msgs == "" {
return stacktrace.Propagate(MsgNotFound, "Failed parse main")
}
msgs = replacer.Replace(msgs)
urls := urlRegex.FindAllString(msgs, -1)
if len(urls) < 1 {
return stacktrace.Propagate(MsgNotFound, "Failed find url in main")
}
p.urlResults = make([]*UrlResult, len(urls))
for i, u := range urls {
p.urlResults[i] = &UrlResult{Url: u}
}
idMatcher := idRegex.FindAllStringSubmatch(msgs, -1)
if len(idMatcher) < 1 {
return stacktrace.Propagate(MsgNotFound, "Failed find id in main")
}
p.lastId = idMatcher[len(idMatcher)-1][1]
return nil
}
func (p *BaseProcessor) ProcessPages() (err error) {
if err = p.sendCheckurl(); err != nil {
return
}
var pageUrl = p.genPageUrl()
p.logf("process pages....")
req, err := http.NewRequest("GET", pageUrl, nil)
if err != nil {
return stacktrace.Propagate(err, "Failed new page request")
}
for k, _ := range p.req.Header {
req.Header.Set(k, p.req.Header.Get(k))
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return stacktrace.Propagate(err, "Failed get page response")
}
bs, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
str := replacer.Replace(string(bs))
result := urlRegex.FindAllString(str, -1)
if len(result) < 1 {
return stacktrace.Propagate(err, "Failed get page url")
}
idMatcher := idRegex.FindAllStringSubmatch(str, -1)
if len(idMatcher) < 1 {
return stacktrace.Propagate(err, "Failed get page id")
}
lastId := idMatcher[len(idMatcher)-1][1]
for _, u := range result {
p.urlResults = append(p.urlResults, &UrlResult{Url: u})
}
if lastId != "" {
if p.lastId == lastId {
i, _ := strconv.Atoi(lastId)
p.lastId = fmt.Sprintf("%d", i-10)
} else {
p.lastId = lastId
}
p.Sleep()
return p.ProcessPages()
}
return nil
}
func (p *BaseProcessor) genPageUrl() string {
p.logf("loading pages, urls size now is %d", len(p.UrlResults()))
otherQuery := strings.Replace(p.req.URL.RawQuery, "action=home", "", -1)
return fmt.Sprintf("https://mp.weixin.qq.com/mp/profile_ext?%s&frommsgid=%s&f=json&count=10&is_ok=1&action=getmsg&f=json&wxtoken=&x5=1&uin=777&key=777", otherQuery, p.lastId)
}
func (p *BaseProcessor) sendCheckurl() (err error) {
if p.checked {
return nil
}
p.checked = true
values := url.Values{}
query := p.req.URL.Query()
values.Add("__biz", query.Get("__biz"))
values.Add("scene", query.Get("scene"))
values.Add("url_list", "")
urlStr := fmt.Sprintf("http://mp.weixin.qq.com/mp/profile_ext?action=urlcheck&uin=%s&key=%s&pass_ticket=%s", query.Get("uin"), query.Get("key"), query.Get("pass_ticket"))
req, err := http.NewRequest("POST", urlStr, strings.NewReader(values.Encode()))
if err != nil {
return stacktrace.Propagate(err, "Failed check request")
}
for k, _ := range p.req.Header {
req.Header.Set(k, p.req.Header.Get(k))
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
func genId(urlStr string) string {
uri, _ := url.ParseRequestURI(urlStr)
return hashKey(uri.Query().Get("__biz") + "_" + uri.Query().Get("mid") + "_" + uri.Query().Get("idx"))
}
func hashKey(key string) string {
h := md5.New()
io.WriteString(h, key)
return fmt.Sprintf("%x", h.Sum(nil))
}
func (P *BaseProcessor) logf(format string, msg ...interface{}) {
if rootConfig.Verbose {
Logger.Printf(format, msg...)
}
}