-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (181 loc) · 5.78 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
"time"
)
import (
"minispider/internal/config"
"minispider/internal/spider"
"minispider/internal/util"
)
import (
"github.com/rs/zerolog"
)
const (
version = "v1.0"
defaultConfigInputPath = "./conf"
defaultLogOutputDir = "./log/"
helpText = `This is minispider, a distributed program for crawling HTML in specified url format. You can use the following parameters in command:
-h Get help
-v Show version of program
-c Specify configuration file input path, or default to ` + defaultConfigInputPath + `
-l Specify log output dir, or default to ` + defaultLogOutputDir
)
const timeFormat = "2006-01-02 15:04:05"
var (
looger *zerolog.Logger
rfLogger *zerolog.Logger
)
func main() {
allTaskDone := make(chan struct{})
closeLogFiles := make(chan struct{})
cfgPath, logDir := processCMD()
looger, rfLogger = initLoggers(logDir, closeLogFiles)
conf := initConfig(cfgPath)
uniMap := &spider.UniMap{}
taskQue := initTaskQue(conf)
httpClient := &http.Client{
Timeout: time.Duration(conf.CrawlTimeout) * time.Second,
}
for i := 0; i < conf.ThreadCount; i++ {
s := spider.NewSpider(conf, httpClient, taskQue, uniMap)
go crawling(allTaskDone, s)
}
select {
case <-allTaskDone:
// this notices goroutine that it's time to close the log files.
closeLogFiles <- struct{}{}
fmt.Println("minispider Done.")
}
}
// processCMD parses the parameters in CMD and processes it.
func processCMD() (string, string) {
helpFlag := flag.Bool("h", false, "help")
versionFlag := flag.Bool("v", false, "version")
configInputPath := flag.String("c", defaultConfigInputPath, "config input path")
logOutputDir := flag.String("l", defaultLogOutputDir, "log output dir")
flag.Parse()
if *helpFlag {
fmt.Println(helpText)
os.Exit(0)
}
if *versionFlag {
fmt.Println("minispider", version, "implemented by mendge")
os.Exit(0)
}
return *configInputPath, *logOutputDir
}
// initConfig inits a Config based on file on cfgPath.
func initConfig(cfgPath string) *config.Config {
conf, err := config.NewConfigFromFile(cfgPath)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
err = config.ValidateConfig(conf)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return conf
}
// initTaskQue inits a task queue based on configure information.
func initTaskQue(cfg *config.Config) *spider.TaskQueue {
sdq := spider.NewTaskDeque(cfg.ThreadCount)
seeds, err := util.ParseSeeds(cfg.UrlListFile)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
for _, urlStr := range seeds {
sdq.PushTask(spider.NewTask(0, urlStr))
}
return sdq
}
// initLoggers inits two logger redirected to log files in logDer.
// if logDir is not existed, make logDir build.
func initLoggers(logDir string, closeLogFiles chan struct{}) (*zerolog.Logger, *zerolog.Logger) {
err := util.BuildDir(logDir)
if err != nil {
fmt.Println("can not build a new dir as root dir of logs")
os.Exit(-1)
}
// Info level logger
logFile, err := os.OpenFile(logDir+"logs.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println("can not build log.txt in specified log output dir")
os.Exit(-1)
}
logger := zerolog.New(logFile).With().Logger()
// Warn level logger
rfLogFile, err := os.OpenFile(logDir+"logs.wf.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println("can not build log.rf.txt in specified log output dir")
os.Exit(-1)
}
rfLogger := zerolog.New(rfLogFile).With().Logger()
// 另起一个协程用于在程序退出时关闭日志文件
go func(file *os.File, rfFile *os.File) {
select {
case <-closeLogFiles:
file.Close()
rfFile.Close()
}
}(logFile, rfLogFile)
return &logger, &rfLogger
}
// crawling makes a spider container keep working until receives allTaskDone signal.
func crawling(allTaskDone chan struct{}, s *spider.Spider) {
for {
// gets task
var scheduler spider.Scheduler = s
task, noMoreTask := scheduler.FetchTask(s.TaskQ)
if noMoreTask {
allTaskDone <- struct{}{}
return
}
getTaskTime := time.Now().Format(timeFormat)
// downloads page
var downloader spider.Downloader = s
pageBody, err := downloader.DownloadHTML(s.HttpClient, task.DstURL)
if err != nil {
rfLogger.Warn().Str("URL", task.DstURL).Str("time", time.Now().Format(timeFormat)).Msg(fmt.Sprintf("fail to download page: %s", err))
continue
}
downloadPageTime := time.Now().Format(timeFormat)
// parses subURLs and add new tasks
var processor spider.Processor = s
links, err := processor.ParseSubURLs(s.Cfg.TargetUrl, pageBody, s.Cfg.TargetUrlRE)
if err != nil {
rfLogger.Warn().Str("URL", task.DstURL).Str("time", time.Now().Format(timeFormat)).Msg(fmt.Sprintf("fail to parse subURLs in page: %s", err))
}
newTaskCount := 0
for _, urlStr := range links {
visited := s.UniMap.IsKeyVisitedOrVisit(urlStr)
if !visited && task.NowDepth < s.Cfg.MaxDepth {
task := spider.NewTask(task.NowDepth+1, urlStr)
scheduler.AddTask(task, s.TaskQ)
newTaskCount++
}
}
parseSubURLsTime := time.Now().Format(timeFormat)
// saves page content
var persistencer spider.Persistencer = s
err = persistencer.SaveToFile(task.DstURL, s.Cfg.OutputDirectory, pageBody)
if err != nil {
rfLogger.Warn().Str("URL", task.DstURL).Str("time", time.Now().Format(timeFormat)).Msg(fmt.Sprintf("fail to save page to file: %s", err))
continue
}
savePageTime := time.Now().Format(timeFormat)
// if task done normally, record a log.
looger.Info().Str("URL", task.DstURL).Str("get task", getTaskTime).
Str("download Page", downloadPageTime).Str("parse sub URLs", parseSubURLsTime).
Str("save to file", savePageTime).Str("new unprocessed task count", strconv.Itoa(newTaskCount)).Send()
// sets a interval
time.Sleep(time.Duration(s.Cfg.CrawlInterval) * time.Second)
}
}