-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
415 lines (337 loc) · 9.54 KB
/
index.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package main
import (
"encoding/json"
"fmt"
"github.com/PuerkitoBio/goquery"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"bytes"
"math"
)
// Document : Object for each page with an array for each asset
type Document struct {
Location string
Urls []string
Stylesheets []string
Scripts []string
Images []string
}
//Sleeps for amount of time that would meet the crawl delay enforcing politeness
func delaySecond(delay float64) {
fmt.Println("Delaying for:", time.Duration(math.Abs(delay)*1000)*time.Millisecond)
time.Sleep(time.Duration(math.Abs(delay)*1000) * time.Millisecond)
}
func initialiseRobots(newURL string) {
defer timeTrack(time.Now(), "Initialise Robots")
u, err := url.Parse(newURL)
if err != nil {
panic(err)
}
needRefresh := false
constructURL := u.Scheme + "://" + u.Host + "/robots.txt"
// check whether the robots.txt has already been downloaded, if not download it
if _, err := os.Stat(u.Host); os.IsNotExist(err) {
downloadAndCreateRobots(u, constructURL)
}
// fetch the info regarding robot.txt ie modified time
// check whether the robots.txt needs a refresh depending on if the time since last download is greater than 1 hour
info, err := os.Stat(u.Host)
currentTime := time.Now()
robotsModified := info.ModTime()
elapsed := currentTime.Sub(robotsModified).Hours()
if elapsed > 1 {
needRefresh = true
}
if needRefresh {
downloadAndCreateRobots(u, constructURL)
}
}
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s", name, elapsed)
}
func checkRobots(newURL string, disallowedPages []string) bool {
// This function checks for politeness regarding user agents, allow/deny and crawler rate
URL, err := url.Parse(newURL)
if err != nil {
fmt.Print(err)
}
for _, disallowPath := range disallowedPages {
if disallowPath == URL.Path {
return false
}
}
return true
}
//reads the robot.txt then processes it so that a URL can be checked against it quickly
func processRobots(newURL string) (bool, []string, []string, int) {
defer timeTrack(time.Now(), "Process Robots")
userAgent := false
crawlDelay := 1
disallowedPages := []string{}
allowedPages := []string{}
var buff bytes.Buffer
URL, err := url.Parse(newURL)
if err != nil {
panic(err)
}
robotsTxt, err := ioutil.ReadFile(URL.Host)
if err != nil {
fmt.Print(err)
}
buff.WriteString(string(robotsTxt))
buff.WriteString("\n")
fileAsString := buff.String()
robotLines := strings.Split(fileAsString, "\n")
for _, lineValue := range robotLines {
if lineValue == "" {
continue
}
values := strings.Split(lineValue, ":")
field := strings.TrimSpace(values[0])
value := strings.TrimSpace(values[1])
switch strings.ToLower(field) {
case "user-agent":
if value == "*" {
userAgent = true
}
break
case "disallow":
disallowedPages = append(disallowedPages, value)
break
case "allow":
allowedPages = append(allowedPages, value)
break
case "crawl-delay":
crawlDelay, _ := strconv.Atoi(value)
_ = crawlDelay
break
default:
break
}
}
return userAgent, disallowedPages, allowedPages, crawlDelay
}
func downloadAndCreateRobots(u *url.URL, constructURL string) {
defer timeTrack(time.Now(), "Robots downloading and creating")
output, err := os.Create(u.Host)
if err != nil {
fmt.Println("Error while creating", u.Host, "-", err)
}
defer output.Close()
response, err := http.Get(constructURL)
if err != nil {
panic(err)
}
defer response.Body.Close()
n, err := io.Copy(output, response.Body)
if err != nil {
fmt.Println("Error while downloading", u.Host, "-", err)
}
_ = n
}
func addToJSONMap(newURL string, jsonData []byte) string {
defer timeTrack(time.Now(), "Add JSON to file")
URL, err := url.Parse(newURL)
if err != nil {
panic(err)
}
var fileName = URL.Host + ".json"
err = ioutil.WriteFile(fileName, jsonData, 0644)
if err != nil {
panic(err)
} else {
return fileName
}
}
func removeDuplicates(elements []string) []string {
// takes inspiration from https://www.dotnetperls.com/duplicates-go but rather than int it removes duplicate strings
// Use map to record duplicates as we find them.
encountered := map[string]bool{}
result := []string{}
for k, v := range elements {
if encountered[elements[k]] == true || v == "" {
// Do not add duplicate.
} else {
// Record this element as an encountered element.
encountered[elements[k]] = true
// Append to result slice.
result = append(result, elements[k])
}
}
// Return the new slice.
return result
}
func normalise(newURL string) string {
lowercaseURL := strings.ToLower(newURL)
if !strings.HasSuffix(lowercaseURL, "/") {
return newURL + "/"
}
return lowercaseURL
}
func fetchURL(baseURL string, c chan Document) {
defer timeTrack(time.Now(), "Fetching URL, HTML parsing and search for assets")
//normalise url
Location := normalise(baseURL)
doc, err := goquery.NewDocument(Location)
if err != nil {
var Urls []string
var Stylesheets []string
var Scripts []string
var Images []string
c <- Document{Location, Urls, Stylesheets, Scripts, Images}
return
}
// Find all the anchor tags and get the href attribute value
Urls := doc.Find("a[href]").Map(func(_ int, s *goquery.Selection) string {
val, _ := s.Attr("href")
// Parse the base url
baseParse, err := url.Parse(Location)
if err != nil {
panic(err)
}
// Parse the context url
contextParse, err := url.Parse(val)
if err != nil {
return ""
}
// remove encoded query values of context
contextParse.RawQuery = ""
// remove fragments of context
contextParse.Fragment = ""
// only return link if host of the base url and context url are the same
if baseParse.Host == contextParse.Host {
return contextParse.String()
}
return ""
})
// Find all the img tags and get the src attribute value
Images := doc.Find("img[src]").Map(func(_ int, s *goquery.Selection) string {
val, _ := s.Attr("src")
if strings.HasPrefix(val, "//") {
return strings.Replace(val, "//", "http://", -1)
}
return val
})
// Find all the script tags and get the src attribute value and remove // if present and replace with http
Scripts := doc.Find("script[src]").Map(func(_ int, s *goquery.Selection) string {
val, _ := s.Attr("src")
if strings.HasPrefix(val, "//") {
return strings.Replace(val, "//", "http://", -1)
}
return val
})
// Find all the link tags and get the href attribute value but only if the rel equals stylesheet
Stylesheets := doc.Find("link[rel=stylesheet]").Map(func(_ int, s *goquery.Selection) string {
val, _ := s.Attr("href")
return val
})
Urls = removeDuplicates(Urls)
Images = removeDuplicates(Images)
Stylesheets = removeDuplicates(Stylesheets)
Scripts = removeDuplicates(Scripts)
newDocument := Document{Location, Urls, Stylesheets, Scripts, Images}
c <- newDocument
}
func remove(slice []string, s int) []string {
return append(slice[:s], slice[s+1:]...)
}
func processCrawler(
documentMap []Document,
channelDocument chan Document,
queue []string,
markedPages map[string]bool,
disallowedPages []string,
allowedPages []string,
crawlDelay int,
timeCalled time.Time,
) []Document {
if len(queue) == 0 {
return documentMap
}
currentTime := time.Now()
diff := timeCalled.Sub(currentTime)
transformedDiff := math.Abs(float64(diff.Seconds()))
if transformedDiff < float64(crawlDelay) {
delaySecond(float64(crawlDelay) + diff.Seconds())
} else {
log.Println("No delay needed")
}
timeCalled = time.Now()
for _, URL := range queue {
allowed := checkRobots(URL, disallowedPages)
if allowed {
go fetchURL(URL, channelDocument)
queue = remove(queue, 0)
details := <-channelDocument
documentMap = append(documentMap, details)
for _, URL := range details.Urls {
// check the URL is actually a URL and if not continue to the next item in the array
_, err := url.ParseRequestURI(URL)
if err != nil {
continue
}
//ensure value isn't empty or already in the system
if !markedPages[URL] && URL != "" {
markedPages[URL] = true
queue = append(queue, URL)
} else {
}
}
log.Println("Queue length:", len(queue))
log.Println("Documents length:", len(documentMap))
return processCrawler(documentMap,
channelDocument,
queue,
markedPages,
disallowedPages,
allowedPages,
crawlDelay,
timeCalled,
)
}
fmt.Print("This page is not allowed to be crawled")
continue
}
return documentMap
}
func startCrawler() {
passedURL := os.Getenv("WEBSITE")
_, err := url.ParseRequestURI(passedURL)
if err != nil {
panic(err)
}
channelDocument := make(chan Document) // channel for document
newURL := normalise(passedURL)
initialiseRobots(newURL)
userAgent, disallowedPages, allowedPages, crawlDelay := processRobots(newURL)
if userAgent {
markedPages := make(map[string]bool) // URLs already in the system whether processed or in queue.
var queue []string // URLs to be fetched, using a very simple array FIFO
var documentMap []Document // map of all the processed pages
markedPages[newURL] = true
queue = append(queue, newURL)
timeCalled := time.Now()
results := processCrawler(documentMap, channelDocument, queue, markedPages, disallowedPages, allowedPages, crawlDelay, timeCalled)
b, err := json.MarshalIndent(results, "", " ")
fileName := addToJSONMap(newURL, b)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Sitemap: ", fileName)
} else {
fmt.Println("You're not allowed to crawl this site")
return
}
}
func main() {
startCrawler()
}