-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.go
223 lines (191 loc) · 4.39 KB
/
scraper.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
package scraper
import (
"context"
"fmt"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/css"
"github.com/chromedp/chromedp"
"log"
"strconv"
"strings"
"time"
)
const (
textJS = `(function(a) {
var s = '';
for (var i = 0; i < a.length; i++) {
if (a[i].offsetParent !== null) {
s += a[i].textContent;
}
}
return s;
})($x('%s/node()'))`
dollarSelector = "//*[contains(text(), '$')]"
)
type NodeInfo struct {
node *cdp.Node
text *string
cssProperties *[]*css.ComputedProperty
}
func FindStyles(nodes *[]*cdp.Node, cssAttributes *[]*[]*css.ComputedProperty) chromedp.Action {
return chromedp.ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
for _, node := range *nodes {
attributes, err := css.GetComputedStyleForNode(node.NodeID).Do(ctxt, h)
if err != nil {
return err
}
*cssAttributes = append(*cssAttributes, &attributes)
}
return nil
})
}
func FindTexts(nodes *[]*cdp.Node, strings *[]*string) chromedp.Action {
return chromedp.ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
for _, node := range *nodes {
var s *string
err := chromedp.EvaluateAsDevTools(fmt.Sprintf(textJS, node.FullXPath()), &s).Do(ctxt, h)
if err != nil {
return err
}
*strings = append(*strings, s)
}
return nil
})
}
func findMaxFont(infos *[]NodeInfo) string {
var maxfont string
for _, node := range *infos {
attr := node.cssProperties
for _, field := range *attr {
if field.Name == "font-size" {
if strings.Compare(field.Value, maxfont) > 0 {
maxfont = field.Value
}
}
}
}
return maxfont
}
func parseColor(color string) (int, int, int) {
c := strings.TrimPrefix(color, "rgb(")
c = strings.TrimSuffix(c, ")")
colors := strings.Split(c, ",")
colors[0] = strings.TrimSpace(colors[0])
colors[1] = strings.TrimSpace(colors[1])
colors[2] = strings.TrimSpace(colors[2])
r, _ := strconv.Atoi(colors[0])
g, _ := strconv.Atoi(colors[1])
b, _ := strconv.Atoi(colors[2])
return r, g, b
}
func red(r int, g int, b int) bool {
if (g+100 < r) && (100+b < r) {
return true
}
return false
}
func isRed(node NodeInfo) bool {
attrs := *node.cssProperties
for _, attr := range attrs {
if attr.Name == "color" {
r, g, b := parseColor(attr.Value)
if red(r, g, b) {
return true
}
}
}
return false
}
func isCrossed(node NodeInfo) bool {
attrs := *node.cssProperties
for _, attr := range attrs {
if attr.Name == "text-decoration-line" {
if attr.Value == "line-through" {
return true
}
}
}
return false
}
func gatherNodeInfos(ctxt context.Context, selector string, url string) ([]NodeInfo, error) {
ctxt, cancel := context.WithCancel(ctxt)
defer cancel()
// create chrome instance
c, err := chromedp.New(ctxt) //, chromedp.WithLog(log.Printf))
if err != nil {
return nil, err
}
// collect node info
var nodes []*cdp.Node
var texts []*string
var cssAttributes []*[]*css.ComputedProperty
err = c.Run(ctxt, chromedp.Tasks{
chromedp.Navigate(url),
chromedp.Sleep(2 * time.Second),
chromedp.Nodes(selector, &nodes),
FindTexts(&nodes, &texts),
FindStyles(&nodes, &cssAttributes),
})
if err != nil {
return nil, fmt.Errorf("Error opening %s: %v", url, err)
}
// shutdown chrome
err = c.Shutdown(ctxt)
if err != nil {
return nil, err
}
// package node info
nodeInfos := make([]NodeInfo, len(nodes))
for i := range nodes {
nodeInfos[i].node = nodes[i]
nodeInfos[i].text = texts[i]
nodeInfos[i].cssProperties = cssAttributes[i]
}
// wait for chrome to finish
err = c.Wait()
if err != nil {
return nil, err
}
return nodeInfos, nil
}
func findPrice(infos *[]NodeInfo) string {
var nodes []NodeInfo
var price string
maxfont := findMaxFont(infos)
for _, node := range *infos {
attr := *(node.cssProperties)
for j, _ := range attr {
if attr[j].Name == "font-size" {
if attr[j].Value == maxfont {
nodes = append(nodes, node)
}
}
}
}
for _, node := range nodes {
if isCrossed(node) {
log.Println("Crossed")
continue
}
if isRed(node) {
price = *node.text
break
}
price = *node.text
}
return price
}
func ParseUrl(url string) (string, error) {
var err error
var price string
// create context
ctxt := context.Background()
infos, err := gatherNodeInfos(ctxt, dollarSelector, url)
if err != nil {
log.Print(err)
return price, err
}
price = findPrice(&infos)
log.Print(price)
return price, nil
}