-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
170 lines (143 loc) · 3.95 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"time"
"github.com/demyanovs/robotstxt"
_ "golang.org/x/lint"
"github.com/demyanovs/urlcrawler/queue"
"github.com/demyanovs/urlcrawler/report"
)
const (
fileNameDefault = "result"
)
const (
outputCSV = "csv"
outputJSON = "json"
)
var supportedOutputs = []string{outputCSV, outputJSON}
func main() {
startURL := flag.String("u", "", "Start url (required)")
output := flag.String("output", outputCSV, "Output format (csv, json)")
outputFile := flag.String("output-file", "", "File path to save r")
delay := flag.Int("delay", 1000, "Delay between requests in milliseconds")
depth := flag.Int("depth", 0, "Depth of the crawl (0 - infinite")
limitURLs := flag.Int("limit", 0, "Limit of URLs to crawl (0 - unlimited")
reqTimeout := flag.Int("timeout", 5000, "Request timeout in milliseconds")
bulkSize := flag.Int("bulk-size", 30, "Bulk size for saving to the file")
queueLen := flag.Int("q-len", 50, "Queue length")
quietMode := flag.Bool("q", false, "Quiet mode (no logs")
ignoreRobotsTXT := flag.Bool("ignore-robots", false, "Ignore crawl-delay and disallowed URLs from robots.txt")
flag.Parse()
if *startURL == "" {
log.Fatal("url flag is required")
}
if *output != outputCSV && *output != outputJSON {
log.Fatalf("unsupported output format: %s. Supported formats: %v", *output, supportedOutputs)
}
logger := log.New(log.Writer(), "", log.Ldate|log.Ltime)
r, reportFile := reportByOutput(*output, *outputFile)
q, err := queue.New(
queue.ConfigType{
QueueLen: *queueLen,
LimitURLs: *limitURLs,
ReqTimeout: time.Duration(*reqTimeout) * time.Millisecond,
Delay: time.Duration(*delay) * time.Millisecond,
BulkSize: *bulkSize,
Quiet: *quietMode,
Depth: *depth,
},
*startURL,
r,
logger,
nil,
)
if *ignoreRobotsTXT == true {
if *quietMode == false {
logger.Println("ignoring robots.txt")
}
} else {
if *quietMode == false {
log.Println("parsing robots.txt")
}
robots, err := robotsTXTFromURL(*startURL)
if err != nil {
log.Fatal(err)
}
q.RobotsData = robots
crawlDelay, err := robots.CrawlDelay("*")
if err != nil {
log.Fatal(err)
}
if crawlDelay != nil {
q.Config.Delay = time.Duration(*crawlDelay) * time.Second
if *quietMode == false {
logger.Printf("found crawl-delay in robots.txt: %ds. Ignoring delay from the config\n", *crawlDelay)
}
}
}
if err != nil {
log.Fatal(err)
}
if *quietMode == false {
printConfig(q, *output, reportFile, *ignoreRobotsTXT, logger)
}
q.Start()
}
func robotsTXTFromURL(startURL string) (*robotstxt.RobotsData, error) {
parsedURL, err := url.Parse(startURL)
if err != nil || parsedURL == nil {
return nil, err
}
robotsPath := fmt.Sprintf("%s://%s/robots.txt", parsedURL.Scheme, parsedURL.Host)
resp, err := http.Get(robotsPath)
if err != nil {
return nil, err
}
defer resp.Body.Close()
robots, err := robotstxt.FromResponse(resp)
if err != nil {
return nil, err
}
return robots, nil
}
func reportByOutput(output string, outputFile string) (queue.Reporter, string) {
var r queue.Reporter
if output == outputJSON {
if outputFile == "" {
outputFile = fmt.Sprintf("%s.%s", fileNameDefault, outputJSON)
}
r = report.NewJSONReport(outputFile)
} else {
if outputFile == "" {
outputFile = fmt.Sprintf("%s.%s", fileNameDefault, outputCSV)
}
r = report.NewCSVReport(outputFile)
}
return r, outputFile
}
func printConfig(queue *queue.Queue, output string, outputFile string, ignoreRobotsTXT bool, logger *log.Logger) {
logger.Printf(
"Starting crawling, "+
"delay: %dms, "+
"depth: %d, "+
"limit: %d, "+
"reqTimeout: %dms, "+
"bulk-size: %d, "+
"output: %s, "+
"output-file: %s, "+
"ignore-robots: %t "+
"\n",
queue.Config.Delay/time.Millisecond,
queue.Config.Depth,
queue.Config.LimitURLs,
queue.Config.ReqTimeout/time.Millisecond,
queue.Config.BulkSize,
output,
outputFile,
ignoreRobotsTXT,
)
}