-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
236 lines (202 loc) · 4.75 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
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"io/ioutil"
"log"
"net"
"net/http"
"runtime"
"strconv"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
// "syscall"
// "io"
"os"
"sync"
)
var wg sync.WaitGroup
var linkList []link
var host, full_host_url string
var file *os.File
var finish bool
var throttle <-chan time.Time
var ignoredFileExtention = []string{".css", ".js", ".png", ".jpg", ".ico"}
var badLinkRetryTimes = 5
var requestTimeOut = 5 * time.Second
var target = "http://www.geekpark.net"
var maxConcurrenceQresuet = 1000
type link struct {
url string
status_code int
duration time.Duration
error_count int
hasRequested int
title string
}
func main() {
link_list_chan := make(chan []link)
throttle = time.Tick(time.Duration(1 * time.Second))
runtime.GOMAXPROCS(runtime.NumCPU())
prepare()
getPageUrls(target)
// os.Exit(1)
for finish == false {
crawl(link_list_chan)
}
log.Println("finish!~")
defer file.Close()
}
func prepare() {
init_file()
set_host()
}
func init_file() {
file, _ = os.OpenFile("log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
}
func set_host() {
last_dot_index := strings.LastIndex(target, ".")
url_without_last_dot := target[:last_dot_index]
last_sec_dot_index := strings.Index(url_without_last_dot, ".")
host = target[last_sec_dot_index+1:]
if host[len(host)-1:] == "/" {
host = host[:len(host)-1]
}
full_host_url = target[:strings.Index(target, host)] + host
}
func crawl(link_list_chan chan []link) {
for {
for k, v := range linkList {
if runtime.NumGoroutine() < maxConcurrenceQresuet {
go request(&linkList[k])
go getPageUrls(v.url)
} else {
log.Println("Max requset,sleep in 2 second")
time.Sleep(2 * time.Second)
continue
}
}
if finish == true {
break
}
checkAllRequested()
<-throttle
}
}
//check the all list has requested
func checkAllRequested() {
var requestCount int
for _, v := range linkList {
if v.hasRequested == 1 {
requestCount++
}
}
if requestCount == len(linkList) {
finish = true
}
}
func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, time.Duration(requestTimeOut))
}
func request(link *link) {
if link.hasRequested == 1 {
return
}
//bad request retry
var breakCounter int
var t0, t1 time.Time
transport := http.Transport{
Dial: dialTimeout,
}
client := http.Client{
Transport: &transport,
}
for {
t0 = time.Now()
resp, err := client.Get(link.url)
t1 = time.Now()
if err != nil {
breakCounter++
if breakCounter == badLinkRetryTimes {
file.WriteString(link.url + " " + strconv.Itoa(link.status_code) + "\n")
break
}
} else {
link.status_code = resp.StatusCode
if link.status_code != 200 {
file.WriteString(link.url + " " + strconv.Itoa(link.status_code) + "\n")
}
spew.Dump(link.url)
get_title(link, resp)
break
}
}
link.duration = t1.Sub(t0)
link.error_count = breakCounter
link.hasRequested = 1
}
func get_title(link *link, resp *http.Response) {
content, _ := ioutil.ReadAll(resp.Body)
contentStr := string(content)
startIndex := strings.Index(contentStr, "<title>")
endIndex := strings.Index(contentStr, "</title>")
if startIndex != -1 && endIndex != -1 {
link.title = contentStr[startIndex+7 : endIndex]
}
}
func getPageUrls(url string) {
res, err := http.Get(url)
if err != nil {
file.WriteString(err.Error())
return
}
content, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
file.WriteString(err.Error())
return
}
contentStr := string(content)
lookingForLink:
for {
startIndex := strings.Index(contentStr, `href="`)
//check if it has looked entire page for href=
if startIndex == -1 {
break
}
newStr := contentStr[startIndex+6:]
newStrEndIndex := strings.Index(newStr, `"`)
if newStrEndIndex <= 6 {
contentStr = newStr[2:]
continue lookingForLink
}
linkStr := newStr[:newStrEndIndex]
//check if linkSts is relative path.if so,change to absolute path.
if string(linkStr[0]) == "/" {
linkStr = full_host_url + linkStr
}
//check the links in pages blog to targe domain
//check http/https://xxx.host.com(len(xxx) = 20),
if index := strings.Index(linkStr, host); index == -1 || index > 20 {
contentStr = newStr[newStrEndIndex:]
continue lookingForLink
}
for _, v := range ignoredFileExtention {
if linkStr[len(linkStr)-4:len(linkStr)] == v {
contentStr = newStr[newStrEndIndex:]
continue lookingForLink
}
}
// spew.Dump(linkStr)
linkNew := &link{linkStr, 0, 0, 0, 0, ""}
linkList = appendIfMissing(linkList, linkNew)
contentStr = newStr[newStrEndIndex:]
}
}
func appendIfMissing(list []link, new_link *link) []link {
for _, v := range list {
if v.url == new_link.url {
return list
}
}
return append(list, *new_link)
}