-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
205 lines (188 loc) · 5.11 KB
/
main.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
/**
* Gocrawler
*
* @author Ritesh Shrivastav
*/
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
// To track crawled URL
var processedLinks = make(map[string]int)
// Will keep reference to the given target URL
var targetUrl *url.URL
// Default file permission
var filePermission = os.FileMode(0777)
// Performs boot check wheather the cli arguments are acceptable, if invalid
// prints help message.
func InitialCheck() bool {
if len(os.Args) < 2 {
fmt.Println("Usage: gocrawl http://example.com")
return false
}
return true
}
// Based on content-type returns path to save the content
func GetSavePath(target string, contentType string) string {
u, _ := url.Parse(target)
newFilePath := "./" + targetUrl.Host + u.Path
// creating dirs, in case if not present already
if strings.Contains(contentType, "text/directory") {
os.MkdirAll(newFilePath, filePermission)
} else if strings.Contains(contentType, "text/html") {
os.MkdirAll(newFilePath, filePermission)
dirSep := "/"
if newFilePath[len(newFilePath)-1:] == "/" {
dirSep = ""
}
return newFilePath + dirSep + "index.html"
} else {
os.MkdirAll((newFilePath[:strings.LastIndex(newFilePath, "/")]), filePermission)
}
return newFilePath
}
// Performs check if given URL is already processed
func IsProcessed(target string) bool {
if processedLinks[target] == 0 {
// not processed
return false
} else {
// processed
return true
}
}
// Saves the processed target Url with response code(in case if we need in
// future)
func SaveProcessed(target string, status int) {
processedLinks[target] = status
}
// Saves given file(content) to the path
func StoreFile(path string, content []byte) {
ioErr := ioutil.WriteFile(path, content, filePermission)
if ioErr != nil {
fmt.Println("Error while writing file " + path + "; " + ioErr.Error())
os.Exit(2)
}
}
// Process all css files from the given document
func ProcessStyle(doc *goquery.Document) {
doc.Find("link").Each(func(i int, s *goquery.Selection) {
val, exist := s.Attr("href")
if exist {
u, _ := url.Parse(val)
if u.Host == targetUrl.Host || u.Host == "" {
newTarget := targetUrl.Scheme + "://" + targetUrl.Host + u.Path
ProcessDoc(newTarget, 0)
}
}
})
}
// Process all the javascript files from given document
func ProcessScript(doc *goquery.Document) {
doc.Find("script").Each(func(i int, s *goquery.Selection) {
val, exist := s.Attr("src")
if exist {
u, _ := url.Parse(val)
if u.Host == targetUrl.Host || u.Host == "" {
newTarget := targetUrl.Scheme + "://" + targetUrl.Host + u.Path
ProcessDoc(newTarget, 0)
}
}
})
}
// Process all image from the given document
func ProcessImg(doc *goquery.Document) {
doc.Find("img").Each(func(i int, s *goquery.Selection) {
val, exist := s.Attr("src")
if exist {
u, _ := url.Parse(val)
if u.Host == targetUrl.Host || u.Host == "" {
newTarget := targetUrl.Scheme + "://" + targetUrl.Host + u.Path
ProcessDoc(newTarget, 0)
}
}
})
}
// Process all the links from the given document
func ProcessLink(doc *goquery.Document, level int) {
doc.Find("a[href]").Each(func(i int, s *goquery.Selection) {
val, exist := s.Attr("href")
if exist {
u, _ := url.Parse(val)
if u.Host == targetUrl.Host || u.Host == "" {
newTarget := targetUrl.Scheme + "://" + targetUrl.Host + u.Path
ProcessDoc(newTarget, level + 1)
}
}
})
}
// Process and save the files, the main method which at the end calls other
// node processing methods. Currently [a, img, link, script] tags are being
// explored.
func ProcessDoc(target string, level int) {
// check if processed already
if IsProcessed(target) || level > 5 {
return
}
// printing to console
fmt.Printf("Fetching level %v from %v\n", level, target)
// fetching target URL
resp, fetchErr := http.Get(target)
if fetchErr != nil {
fmt.Println("Error while fetching " + target + "; " + fetchErr.Error())
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Printf("Got response code %v\n", resp.StatusCode)
return
}
contentType := resp.Header.Get("Content-Type")
// just save the file in case content type is not HTML
if !strings.Contains(contentType, "text/html") {
pathToSave := GetSavePath(target, contentType)
content, readErr := ioutil.ReadAll(resp.Body)
if readErr == nil {
StoreFile(pathToSave, []byte(content))
}
SaveProcessed(target, resp.StatusCode)
} else {
// need to look further
doc, docErr := goquery.NewDocument(target)
if docErr != nil {
SaveProcessed(target, resp.StatusCode)
fmt.Println("Error while fetching " + target)
} else {
SaveProcessed(target, resp.StatusCode)
}
// saving doc
pathToSave := GetSavePath(target, contentType)
htmlStr, _ := doc.Html()
StoreFile(pathToSave, []byte(htmlStr))
// process child nodes
ProcessImg(doc)
ProcessLink(doc, level)
ProcessScript(doc)
ProcessStyle(doc)
}
}
// Gocrawler main thread.
func main() {
if !InitialCheck() {
os.Exit(2)
}
target := os.Args[1]
u, err := url.Parse(target)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
targetUrl = u
ProcessDoc(target, 0)
}