-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crawler.go
177 lines (150 loc) · 3.25 KB
/
Crawler.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
package AppleProductMonitor
import (
"context"
"errors"
"fmt"
"github.com/PuerkitoBio/goquery"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"strings"
"time"
)
type Crawler struct {
DataAccess DataAccess
FetchTarget string
FetchIntervalSec int
}
func NewCrawler(fetchTarget string, fetchIntervalSec int) *Crawler {
return &Crawler{
DataAccess: NewSimpleFileDataAccess(),
FetchTarget: fetchTarget,
FetchIntervalSec: fetchIntervalSec,
}
}
type Product struct {
Group string
Product string
Model string
NCC string
}
type Event struct {
Added []Product `json:"added" yaml:"added"`
Removed []Product `json:"removed" yaml:"removed"`
}
func (c *Crawler) parse(source io.ReadCloser) ([]Product, error) {
doc, err := goquery.NewDocumentFromReader(source)
if err != nil {
return nil, err
}
main := doc.Find("main")
sections := main.Find("div#sections").Children()
log.Debugf("Parsed %d categories...", sections.Length())
products := make([]Product, 0)
for i := 0; i < sections.Length(); i++ {
current := sections.Eq(i)
group := current.Find("h2").Text()
items := current.Find("td")
l := items.Length()
var item, model, ncc string
for j := 0; j < l; j++ {
text := strings.Trim(items.Eq(j).Text(), " \n")
switch j % 3 {
case 0:
item = text
case 1:
model = text
case 2:
ncc = text
products = append(products, Product{
Group: group,
Product: item,
Model: model,
NCC: ncc,
})
}
}
}
log.Debugf("Parsed : %v", products)
return products, nil
}
func (c *Crawler) FetchAndCompare(ctx context.Context) (Event, error) {
resp, err := http.Get(c.FetchTarget)
if err != nil {
log.Warnf("Fail to fetch from %s !", c.FetchTarget)
return Event{}, err
}
if resp.StatusCode != http.StatusOK {
msg := fmt.Sprintf("bad http status for fetching %s : %d !", c.FetchTarget, resp.StatusCode)
log.Warn(msg)
return Event{}, errors.New(msg)
}
defer func() {
_ = resp.Body.Close()
}()
recent, err := c.parse(resp.Body)
if err != nil {
return Event{}, err
}
if len(recent) == 0 {
return Event{}, errors.New("parsed but get 0 elements, something wrong")
}
old, _ := c.DataAccess.LoadData(ctx)
err = c.DataAccess.SaveData(ctx, recent)
if err != nil {
return Event{}, err
}
added := make([]Product, 0)
removed := make([]Product, 0)
for _, r := range recent {
found := false
for _, o := range old {
if r.Model == o.Model {
found = true
break
}
}
if found == false {
added = append(added, r)
}
}
for _, o := range old {
found := false
for _, r := range recent {
if r.Model == o.Model {
found = true
break
}
}
if found == false {
removed = append(removed, o)
}
}
return Event{
Added: added,
Removed: removed,
}, nil
}
func (c *Crawler) Run(ctx context.Context) (e chan Event) {
e = make(chan Event)
go func() {
timer := time.NewTicker(time.Duration(c.FetchIntervalSec) * time.Second)
defer timer.Stop()
for {
select {
case <-ctx.Done():
timer.Stop()
return
case <-timer.C:
event, err := c.FetchAndCompare(ctx)
if err != nil {
log.Warnf("error while FetchAndCompare() : %s", err.Error())
return
}
e <- event
default:
}
}
}()
return e
}