-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.go
165 lines (124 loc) · 4.01 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"strings"
"sync"
"github.com/gocolly/colly"
"github.com/gocolly/colly/extensions"
)
var FORBIDDEN_TEXT_CHARACTERS = [2]string{"\n", "\t"}
type RouteDocument struct {
ElementTag string `json:"elementTag"`
InnerText string `json:"innerText"`
}
type Scraper struct {
Protocol string
Domain string
BaseURL string
OutputFolderPath string
ScrapableTags string
Collector *colly.Collector
ContentRouteMap map[string][]*RouteDocument
VisitedURLS map[string]bool
Mutex *sync.Mutex
}
func CreateRouteDocument(el *colly.HTMLElement) *RouteDocument {
finalInnerText := strings.Join(strings.Fields(strings.ToLower(el.DOM.Text())), " ")
for _, FORBIDDEN_CHAR := range FORBIDDEN_TEXT_CHARACTERS {
finalInnerText = strings.ReplaceAll(finalInnerText, FORBIDDEN_CHAR, " ")
}
return &RouteDocument{
ElementTag: el.Name,
InnerText: finalInnerText,
}
}
func NormalizeURL(rawURL string) string {
return strings.TrimSpace(strings.ToLower(rawURL))
}
func (scraper *Scraper) CanVisitURL(url string) bool {
parsedURL := NormalizeURL(url)
_, hasAlreadyVisitedParsedURL := scraper.VisitedURLS[parsedURL]
if !strings.Contains(parsedURL, scraper.Domain) || hasAlreadyVisitedParsedURL {
return false
}
return true
}
func (scraper *Scraper) SaveScrapedDataToJSON(fileName string) {
defer scraper.Mutex.Unlock()
scraper.Mutex.Lock()
data, marshalErr := json.Marshal(scraper.ContentRouteMap)
if marshalErr != nil {
panic(marshalErr)
}
dirCreateErr := os.MkdirAll(OUTPUT_DIRECTORY_NAME, os.ModePerm)
if dirCreateErr != nil {
panic(dirCreateErr)
}
filePath := strings.TrimRight(scraper.OutputFolderPath, "/") + "/" + fileName
writeErr := ioutil.WriteFile(filePath, data, 0644)
if writeErr != nil {
panic(writeErr)
}
log.Printf("Saved data for the domain: %s to the relative file path: %s", scraper.Domain, filePath)
}
func (scraper *Scraper) AggregateData() {
scraper.Collector.OnHTML(scraper.ScrapableTags, func(el *colly.HTMLElement) {
requestURL := NormalizeURL(el.Request.URL.String())
defer scraper.Mutex.Unlock()
scraper.Mutex.Lock()
_, ok := scraper.ContentRouteMap[requestURL]
if !ok {
scraper.ContentRouteMap[requestURL] = []*RouteDocument{}
}
scraper.ContentRouteMap[requestURL] = append(scraper.ContentRouteMap[requestURL], CreateRouteDocument(el))
})
scraper.Collector.OnHTML("a[href]", func(el *colly.HTMLElement) {
relativeURLPath := el.Attr("href")
absoluteURLPath := el.Request.AbsoluteURL(relativeURLPath)
el.Request.Visit(absoluteURLPath)
})
scraper.Collector.OnRequest(func(request *colly.Request) {
defer scraper.Mutex.Unlock()
scraper.Mutex.Lock()
requestURL := NormalizeURL(request.URL.String())
canVisitURL := scraper.CanVisitURL(requestURL)
if !canVisitURL {
request.Abort()
return
}
log.Printf("Visiting: %s", requestURL)
})
scraper.Collector.OnScraped(func(response *colly.Response) {
defer scraper.Mutex.Unlock()
scraper.Mutex.Lock()
requestURL := NormalizeURL(response.Request.URL.String())
scraper.VisitedURLS[requestURL] = true
log.Printf("Finished scraping content for: %s", requestURL)
})
scraper.Collector.Visit(scraper.BaseURL)
scraper.Collector.Wait()
}
func NewScraper(domain string, protocol string, maxExplorationDepth uint, scrapableTags string, outputFolderPath string) *Scraper {
collector := colly.NewCollector()
collector.AllowedDomains = []string{domain}
collector.AllowURLRevisit = false
collector.MaxDepth = int(maxExplorationDepth)
collector.Async = true
extensions.RandomUserAgent(collector)
extensions.Referer(collector)
baseURL := protocol + "://" + domain + "/"
return &Scraper{
Protocol: protocol,
Domain: domain,
BaseURL: baseURL,
ScrapableTags: scrapableTags,
OutputFolderPath: outputFolderPath,
Collector: collector,
ContentRouteMap: make(map[string][]*RouteDocument),
VisitedURLS: make(map[string]bool),
Mutex: &sync.Mutex{},
}
}