-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
171 lines (133 loc) · 4.21 KB
/
collector.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
package main
import (
"log"
"github.com/gocolly/colly/v2"
)
type CollectorConfig struct {
AllowedDomains string
CacheDir string
Env string
Route string
Domain string
}
func (cc *CollectorConfig) isDev() bool {
return cc.Env == DEV
}
type SourceCollector struct {
Source ISource
Config *CollectorConfig
Entities []IEntity
}
func (source *SourceCollector) getCollector() *colly.Collector {
options := make([]colly.CollectorOption, 0)
options = append(options, colly.AllowedDomains(source.Config.AllowedDomains))
if source.Config.isDev() {
options = append(options, colly.CacheDir(source.Config.CacheDir))
}
collector := colly.NewCollector(options...)
collector.OnRequest(func(r *colly.Request) {
log.Println("Visiting ", source.Config.Domain, r.URL.String())
})
collector.OnError(func(r *colly.Response, err error) {
log.Println("Something went wrong:", source.Config.Domain, err)
})
return collector
}
func (source *SourceCollector) GetEntities() []IEntity {
return source.Entities
}
type SourceJsonCollector struct {
SourceCollector
Source IJsonSource
}
func MakeSourceJsonCollector(source IJsonSource, collectorConfig *CollectorConfig) *SourceJsonCollector {
collector := &SourceJsonCollector{}
collector.Source = source
collector.Config = collectorConfig
return collector
}
func (source *SourceJsonCollector) Collect() {
collector := source.getCollector()
collector.OnScraped(func(r *colly.Response) {
source.Entities = source.Source.GetOnScrapedHandler(r.Body)
})
switch source.Source.GetRequestMethod() {
case METHOD_POST:
source.applyPostStrategy(collector)
case METHOD_GET:
source.applyGetStrategy(collector)
}
}
func (source *SourceJsonCollector) applyGetStrategy(collector *colly.Collector) {
collector.Visit(source.Config.Route)
}
func (source *SourceJsonCollector) applyPostStrategy(collector *colly.Collector) {
collector.PostRaw(source.Config.Route, source.Source.GetRequestData())
}
type SourceHtmlCollector struct {
SourceCollector
Source IHtmlSource
}
func MakeSourceHtmlCollector(source IHtmlSource, collectorConfig *CollectorConfig) *SourceHtmlCollector {
collector := &SourceHtmlCollector{}
collector.Source = source
collector.Config = collectorConfig
return collector
}
func (source *SourceHtmlCollector) Collect() {
collector := source.getCollector()
collector.OnHTML(source.Source.GetContainer(), func(e *colly.HTMLElement) {
source.Source.GetOnHtmlHandler(e)
})
collector.OnScraped(func(r *colly.Response) {
source.Entities = source.Source.GetOnScrapedHandler(r.Body)
})
switch source.Source.GetRequestMethod() {
case "POST":
source.applyPostStrategy(collector)
case "GET":
source.applyGetStrategy(collector)
}
}
func (source *SourceHtmlCollector) applyGetStrategy(collector *colly.Collector) {
collector.Visit(source.Config.Route)
}
func (source *SourceHtmlCollector) applyPostStrategy(collector *colly.Collector) {
collector.PostRaw(source.Config.Route, source.Source.GetRequestData())
}
type SourcePaginationalHtmlCollector struct {
SourceCollector
Source IHtmlSource
}
func MakeSourcePaginationalHtmlCollector(
source IHtmlSource,
collectorConfig *CollectorConfig) *SourcePaginationalHtmlCollector {
collector := &SourcePaginationalHtmlCollector{}
collector.Source = source
collector.Config = collectorConfig
return collector
}
func (source *SourcePaginationalHtmlCollector) Collect() {
collector := source.getCollector()
collector.OnHTML(".page-links a", func(e *colly.HTMLElement) {
collector.Visit(e.Request.AbsoluteURL(e.Attr("href")))
})
collector.OnHTML(source.Source.GetContainer(), func(e *colly.HTMLElement) {
source.Source.GetOnHtmlHandler(e)
})
collector.OnScraped(func(r *colly.Response) {
source.Entities = source.Source.GetOnScrapedHandler(r.Body)
})
switch source.Source.GetRequestMethod() {
case "POST":
source.applyPostStrategy(collector)
case "GET":
source.applyGetStrategy(collector)
}
}
func (source *SourcePaginationalHtmlCollector) applyGetStrategy(collector *colly.Collector) {
collector.Visit(source.Config.Route)
}
func (source *SourcePaginationalHtmlCollector) applyPostStrategy(collector *colly.Collector) {
collector.PostRaw(source.Config.Route, source.Source.GetRequestData())
}