-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (142 loc) · 4.26 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
package main
import (
"context"
"flag"
"hellospider/core"
"log"
"net/url"
"regexp"
"strings"
"time"
)
// 初始化布隆过滤器
func initBloomFilter(config Config) core.BloomFilter {
return core.NewRedisBloom(config.Redis.Host, "spider-"+config.Namespace, config.Redis.Auth, "filter:"+config.Namespace)
}
// 初始化抓取器
func initFetcher(config Config) core.Fetcher {
return core.NewDefaultFetcher(config.Accepts, config.UserAgents, config.ResponseHeaders)
}
// 初始化消息队列
func initQueue(config Config) (core.Queue, error) {
return core.NewRbQueue(config.RabbitMq.Url,
config.RabbitMq.Exchange,
"spider-"+config.Namespace,
"spider/"+config.Namespace,
config.Workers*8,
config.RabbitMq.MaxLength,
config.Priority)
}
// 初始化存储器
func initStorage(config Config) (core.Storage, error) {
return core.NewElasticsearchStorage(config.Elasticsearch.Address,
config.Elasticsearch.Username,
config.Elasticsearch.Password,
"spider-"+config.Namespace,
context.Background())
}
// 初始化全部组件
func initAll(config Config) (spider *core.Spider, error error) {
var allows []regexp.Regexp
if len(config.Rules.Allows) != 0 {
allows = make([]regexp.Regexp, 0, len(config.Rules.Allows))
for _, a := range config.Rules.Allows {
r, err := regexp.Compile(a)
if err != nil {
return nil, err
}
log.Printf("[Info] Allow: %s\n", a)
allows = append(allows, *r)
}
}
var forbid []regexp.Regexp
if len(config.Rules.Forbid) != 0 {
allows = make([]regexp.Regexp, 0, len(config.Rules.Forbid))
for _, f := range config.Rules.Forbid {
r, err := regexp.Compile(f)
if err != nil {
return nil, err
}
log.Printf("[Info] Forbid: %s\n", f)
forbid = append(forbid, *r)
}
}
filter := initBloomFilter(config)
fetcher := initFetcher(config)
queue, err := initQueue(config)
if err != nil {
return nil, err
}
storage, err := initStorage(config)
if err != nil {
return nil, err
}
log.Println("[Info] Finish initialize.")
return &core.Spider{
Filter: filter,
Queue: queue,
Storage: storage,
Fetcher: fetcher,
Allows: allows,
Forbid: forbid,
}, nil
}
func main() {
configFile := flag.String("config", "config.json", "File path of configuration.")
seed := flag.String("seed", "", "The seeds URL is comma-separated. Such as: 'https://a.com/, https://b.com/'. And the seeds in the configuration file will be ignored.")
reset := flag.Bool("reset", false, "Reset queue, storage and filter before begin task.")
namespace := flag.String("namespace", "", "Namespace of task.")
priority := flag.String("priority", "", "Priority policy: 0-9 means that the priority is constant, url-len means that the priority is calculated according to the length of the URL (the shorter the priority), path-len means that the priority is calculated according to the length of the URL path (the shorter the priority).")
flag.Parse()
config, err := loadConfig(*configFile)
if err != nil {
log.Fatalf("[Error] Fail to load config!\n%s\n", err)
}
ns := strings.TrimSpace(*namespace)
if config.Namespace == "" || ns != "" {
config.Namespace = ns
}
log.Printf("[Info] Using namespace [%s]\n", config.Namespace)
pty := strings.TrimSpace(*priority)
if config.Priority == "" || pty != "" {
config.Priority = pty
}
log.Printf("[Info] Using priority policy [%s]\n", config.Priority)
if *reset {
for i := 5; i >= 0; i-- {
log.Printf("[Warining] ⚠ Reset flag is true, clear namespace [%s] in %d seconds. ⚠ ", config.Namespace, i)
time.Sleep(time.Second)
}
}
spider, err := initAll(*config)
if err != nil {
log.Fatalf("[Error] Fail to initialize!\n%s\n", err)
}
if *reset {
err = spider.Reset()
if err != nil {
log.Fatalf("[Error] Fail to reset!\n%s", err)
}
log.Println("[Info] Finish reset.")
}
if *seed != "" {
seeds := strings.Split(*seed, ",")
config.Seeds = seeds
}
for _, s := range config.Seeds {
u, err := url.Parse(s)
if err != nil {
log.Printf("[Warning] Fail to push seed: %s\n%s\n", s, err)
continue
}
success, err := spider.Enqueue(u)
if err != nil {
log.Printf("[Warning] Fail to push seed: %s\n%s\n", s, err)
} else if success {
log.Printf("[Info] Push seed: %s \n", s)
} else {
log.Printf("[Warning] Fail to push seed: %s\n", s)
}
}
spider.Run(config.Workers)
}