-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
73 lines (62 loc) · 1.65 KB
/
context.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
package gospider
import (
"fmt"
"github.com/rs/zerolog"
"github.com/zhshch2002/goreq"
)
// Context 上下文, 包含爬虫, 请求, 相应, 元数据
type Context struct {
s *Spider
Req *goreq.Request
Resp *goreq.Response
Meta map[string]interface{}
abort bool
}
// Abort this context to break the handler chain and stop handling
func (c *Context) Abort() {
c.abort = true
}
// IsAborted return was the context dropped
func (c *Context) IsAborted() bool {
return c.abort
}
// AddTask add a task to new task list. After every handler func return,spider will collect these tasks
// 使用Handler来处理这些请求, Handler可以为多个
func (c *Context) AddTask(req *goreq.Request, h ...Handler) {
if !req.URL.IsAbs() {
req.URL = c.Req.URL.ResolveReference(req.URL)
}
t := c.s.handleOnTask(c, NewTask(req, c.Meta, h...))
if t == nil {
return
}
c.s.addTask(t)
}
// AddItem add an item to new item list. After every handler func return,
// spider will collect these items and call OnItem handler func
func (c *Context) AddItem(i interface{}) {
c.s.addItem(&Item{
Ctx: c,
Data: i,
})
}
func (c *Context) IsDownloaded() bool {
return c.Resp != nil
}
func (c *Context) Println(v ...interface{}) {
log.Print(v...)
}
func (c *Context) Error() *zerolog.Event {
return log.Error()
}
func (c *Context) String() string {
if c.Req == nil {
return "[empty context]"
} else if c.Resp == nil {
return fmt.Sprint("[not downloaded ctx] ", c.Req.URL.String())
} else if c.Resp.Response == nil || c.Resp.Err != nil {
return fmt.Sprint("[err ctx] ", c.Req.URL.String())
} else {
return fmt.Sprint("["+c.Resp.Status+"] ", c.Req.URL)
}
}