-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider.go
117 lines (88 loc) · 2.19 KB
/
spider.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
package gluaspider
import (
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
"github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
)
// Spider type
type Spider struct {
restyClient *resty.Client
}
// NewSpider NewSpider
func NewSpider() *Spider {
return &Spider{
restyClient: resty.New(),
}
}
// GetRestyClient Get RestyClient
func (s *Spider) GetRestyClient() *resty.Client {
return s.restyClient
}
func (s *Spider) SetProxy(url string) {
s.restyClient.SetProxy(url)
}
// Get Simple Get Url
func (s *Spider) Get(l *lua.LState) int {
resp, err := s.restyClient.R().Get(l.CheckString(1))
if err != nil {
l.Push(lua.LNil)
l.Push(lua.LString(err.Error()))
return 2
}
return s.newDocumentFromString(l, resp.String())
}
// RestyClient Get RestyClient
func (s *Spider) RestyClient(l *lua.LState) int {
l.Push(luar.New(l, s.restyClient))
return 1
}
// newDocumentFromString New Goquery Document From String
func (s *Spider) newDocumentFromString(l *lua.LState, html string) int {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
l.Push(lua.LNil)
l.Push(lua.LString(err.Error()))
return 2
}
l.Push(luar.New(l, doc))
l.Push(lua.LNil)
return 2
}
// NewDocumentFromString New Goquery Document From String
func (s *Spider) NewDocumentFromString(l *lua.LState) int {
return s.newDocumentFromString(l, l.CheckString(1))
}
// Regexp
func (s *Spider) Regexp(l *lua.LState) int {
reg, err := regexp.Compile(l.CheckString(1))
if err != nil {
l.Push(lua.LBool(false))
return 1
}
l.Push(lua.LBool(reg.MatchString(l.CheckString(2))))
return 1
}
// ParseJson
func (s *Spider) ParseJson(l *lua.LState) int {
r := gjson.Parse(l.CheckString(1))
l.Push(luar.New(l, r))
return 1
}
// Loader Loader
func (s *Spider) Loader(l *lua.LState) int {
// register functions to the table
mod := l.SetFuncs(l.NewTable(), map[string]lua.LGFunction{
"RestyClient": s.RestyClient,
"NewDocumentFromString": s.NewDocumentFromString,
"Get": s.Get,
"Regexp": s.Regexp,
"ParseJson": s.ParseJson,
})
// returns the module
l.Push(mod)
return 1
}