-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
executable file
·225 lines (211 loc) · 5.4 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/schollz/linkcrawler/lib"
"gopkg.in/urfave/cli.v1"
)
var version string
func main() {
app := cli.NewApp()
app.Name = "linkcrawler"
app.Usage = "crawl a site for links, or download a list of sites"
app.Version = version
app.Compiled = time.Now()
app.Action = func(c *cli.Context) error {
cli.ShowSubcommandHelp(c)
return nil
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "server, s",
Value: "",
Usage: "boltdb server instance [required]",
},
cli.StringFlag{
Name: "useragent,",
Value: "",
Usage: "supply a User-Agent string to be used",
},
cli.StringFlag{
Name: "exclude, e",
Value: "",
Usage: "comma-delimted phrases that must NOT be in URL",
},
cli.StringFlag{
Name: "include, i",
Value: "",
Usage: "comma-delimted phrases that must be in URL",
},
cli.IntFlag{
Name: "workers,w",
Value: 100,
Usage: "Max number of workers",
},
cli.IntFlag{
Name: "conn,c",
Value: 100,
Usage: "Max number of connections in HTTP pool",
},
cli.IntFlag{
Name: "stats",
Value: 1,
Usage: "Print stats every `X` seconds",
},
cli.IntFlag{
Name: "trash-limit",
Value: 5,
Usage: "Exit if trashed URLs accumulates more than `X` / stats check",
},
cli.BoolFlag{
Name: "verbose",
Usage: "turn on logging",
},
cli.StringFlag{
Name: "prefix, p",
Value: "",
Usage: "override file prefix",
},
cli.BoolFlag{
Name: "redo",
Usage: "move doing to todo",
},
cli.BoolFlag{
Name: "tor",
Usage: "use tor proxy",
},
}
app.Commands = []cli.Command{
{
Name: "crawl",
Aliases: []string{"c"},
Usage: "crawl a website and get a list of links",
Action: func(c *cli.Context) error {
url := ""
if c.NArg() > 0 {
url = c.Args().Get(0)
} else {
fmt.Println("Must specify url to crawl")
return nil
}
if c.GlobalString("server") == "" {
fmt.Println("Must specify BoltDB server ")
return nil
}
fmt.Println(url)
// Setup crawler to crawl
fmt.Println("Setting up crawler...")
craw, err := crawler.New(url, c.GlobalString("server"), c.GlobalBool("verbose"))
if err != nil {
return err
}
if c.GlobalString("prefix") != "" {
craw.FilePrefix = c.GlobalString("prefix")
}
craw.MaxNumberConnections = c.GlobalInt("conn")
craw.MaxNumberWorkers = c.GlobalInt("workers")
craw.Verbose = c.GlobalBool("verbose")
craw.TimeIntervalToPrintStats = c.GlobalInt("stats")
craw.UserAgent = c.GlobalString("useragent")
craw.TrashLimit = c.GlobalInt("trash-limit")
craw.UseTor = c.GlobalBool("tor")
if len(c.GlobalString("include")) > 0 {
craw.KeywordsToInclude = strings.Split(strings.ToLower(c.GlobalString("include")), ",")
}
if len(c.GlobalString("exclude")) > 0 {
craw.KeywordsToExclude = strings.Split(strings.ToLower(c.GlobalString("exclude")), ",")
}
if c.GlobalBool("redo") {
craw.ResetDoing()
}
fmt.Printf("Starting crawl using DB %s\n", craw.Name())
err = craw.Crawl()
if err != nil {
return err
}
return craw.Dump()
},
},
{
Name: "download",
Aliases: []string{"d"},
Usage: "download a list of websites",
Action: func(c *cli.Context) error {
fileWithListOfURLS := ""
if c.NArg() > 0 {
fileWithListOfURLS = c.Args().Get(0)
} else {
fmt.Println("Must specify file containing list of URLs")
return nil
}
if c.GlobalString("server") == "" {
fmt.Println("Must specify BoltDB server ")
return nil
}
b, err := ioutil.ReadFile(fileWithListOfURLS)
if err != nil {
return err
}
links := strings.Split(string(b), "\n")
// Setup crawler to download
craw, err := crawler.New(fileWithListOfURLS, c.GlobalString("server"), c.GlobalBool("verbose"))
if err != nil {
return err
}
if c.GlobalString("prefix") != "" {
craw.FilePrefix = c.GlobalString("prefix")
}
craw.MaxNumberConnections = c.GlobalInt("conn")
craw.MaxNumberWorkers = c.GlobalInt("workers")
craw.Verbose = c.GlobalBool("verbose")
craw.TimeIntervalToPrintStats = c.GlobalInt("stats")
craw.UserAgent = c.GlobalString("useragent")
craw.TrashLimit = c.GlobalInt("trash-limit")
craw.UseTor = c.GlobalBool("tor")
if len(c.GlobalString("include")) > 0 {
craw.KeywordsToInclude = strings.Split(strings.ToLower(c.GlobalString("include")), ",")
}
if len(c.GlobalString("exclude")) > 0 {
craw.KeywordsToExclude = strings.Split(strings.ToLower(c.GlobalString("exclude")), ",")
}
if c.GlobalBool("redo") {
craw.ResetDoing()
}
err = craw.Download(links)
if err != nil {
fmt.Printf("Error downloading: %s", err.Error())
return err
}
fmt.Println("Finished downloading")
return nil
},
},
{
Name: "dump",
Usage: "dump a list of links crawled from db",
Action: func(c *cli.Context) error {
url := ""
if c.NArg() > 0 {
url = c.Args().Get(0)
} else {
fmt.Println("Must specify url to dump")
return nil
}
if c.GlobalString("server") == "" {
fmt.Println("Must specify BoltDB server ")
return nil
}
// Setup crawler to crawl
craw, err := crawler.New(url, c.GlobalString("server"), c.GlobalBool("verbose"))
if err != nil {
return err
}
return craw.Dump()
},
},
}
app.Run(os.Args)
}