-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgluahttpscrape.go
152 lines (134 loc) · 3.8 KB
/
gluahttpscrape.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
package gluahttpscrape
import (
"strings"
"github.com/yhat/scrape"
"github.com/yuin/gopher-lua"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
"layeh.com/gopher-luar"
)
type httpScrapeModule struct{}
func NewHttpScrapeModule() *httpScrapeModule {
return &httpScrapeModule{}
}
func (h *httpScrapeModule) Loader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
"find_attr_by_class": h.findAttrByClass,
"find_attr_by_id": h.findAttrById,
"find_attr_by_tag": h.findAttrByTag,
"find_attrs_by_class": h.findAttrsByClass,
"find_attrs_by_id": h.findAttrsById,
"find_attrs_by_tag": h.findAttrsByTag,
"find_text_by_id": h.findTextById,
"find_text_by_class": h.findTextByClass,
"find_text_by_tag": h.findTextByTag,
})
L.Push(mod)
return 1
}
func getMatcher(selector, value string) scrape.Matcher {
var matcher func(*html.Node) bool
if selector == "class" {
matcher = scrape.ByClass(value)
} else if selector == "id" {
matcher = scrape.ById(value)
} else if selector == "tag" {
matcher = scrape.ByTag(atom.Lookup([]byte(value)))
}
return matcher
}
func (h *httpScrapeModule) findAttr(selector string, L *lua.LState) int {
body := L.ToString(1)
attr := L.ToString(2)
query := L.ToString(3)
L.Pop(3)
root, err := html.Parse(strings.NewReader(body))
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
results := scrape.FindAll(root, getMatcher(selector, query))
attrResults := []string{}
for _, result := range results {
attrResults = append(attrResults, scrape.Attr(result, attr))
}
L.Push(luar.New(L, attrResults))
L.Push(lua.LNil)
return 2
}
func (h *httpScrapeModule) findAttrs(selector string, L *lua.LState) int {
body := L.ToString(1)
attrsCount := L.ToInt(2)
attrs := []string{}
for i := 1; i <= attrsCount; i++ {
attrNow := L.ToString(2 + i)
attrs = append(attrs, attrNow)
}
query := L.ToString(2 + attrsCount + 1)
L.Pop(2 + attrsCount + 1)
root, err := html.Parse(strings.NewReader(body))
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
results := scrape.FindAll(root, getMatcher(selector, query))
attrResults := []map[string]string{}
for _, result := range results {
attrResults = append(attrResults, make(map[string]string))
idx := len(attrResults) - 1
for _, attr := range attrs {
attrResults[idx][attr] = scrape.Attr(result, attr)
}
}
L.Push(luar.New(L, attrResults))
L.Push(lua.LNil)
return 2
}
func (h *httpScrapeModule) findText(selector string, L *lua.LState) int {
body := L.ToString(1)
query := L.ToString(2)
L.Pop(2)
root, err := html.Parse(strings.NewReader(body))
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
results := scrape.FindAll(root, getMatcher(selector, query))
attrResults := []string{}
for _, result := range results {
attrResults = append(attrResults, scrape.Text(result))
}
L.Push(luar.New(L, attrResults))
L.Push(lua.LNil)
return 2
}
func (h *httpScrapeModule) findAttrByClass(L *lua.LState) int {
return h.findAttr("class", L)
}
func (h *httpScrapeModule) findAttrById(L *lua.LState) int {
return h.findAttr("id", L)
}
func (h *httpScrapeModule) findAttrByTag(L *lua.LState) int {
return h.findAttr("tag", L)
}
func (h *httpScrapeModule) findAttrsByClass(L *lua.LState) int {
return h.findAttrs("class", L)
}
func (h *httpScrapeModule) findAttrsById(L *lua.LState) int {
return h.findAttrs("id", L)
}
func (h *httpScrapeModule) findAttrsByTag(L *lua.LState) int {
return h.findAttrs("tag", L)
}
func (h *httpScrapeModule) findTextByClass(L *lua.LState) int {
return h.findText("class", L)
}
func (h *httpScrapeModule) findTextById(L *lua.LState) int {
return h.findText("id", L)
}
func (h *httpScrapeModule) findTextByTag(L *lua.LState) int {
return h.findText("tag", L)
}