-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawl.go
More file actions
232 lines (216 loc) · 5.82 KB
/
Copy pathcrawl.go
File metadata and controls
232 lines (216 loc) · 5.82 KB
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
package main
import (
"fmt"
"time"
"log"
"net/url"
)
type DownloadResult struct {
url string
body []byte
err error
}
type ExtractResult struct {
url, title string
words, urls []string
imgs map[string]string
}
func (idx *IIndex) addImgs(url_id int, ex *ExtractResult) {
var wcount int
for img, alt := range ex.imgs {
url, err := url.Parse(img)
if err != nil {
log.Fatalf("Could not parse %v: %v\n", img, err)
}
url.RawQuery = ""
img = url.String()
_, err = idx.dbase.Exec(`INSERT OR IGNORE INTO imgs(src, alt_txt) VALUES(?, ?);`, img, alt)
if err != nil {
log.Fatalf("Could not insert %v, %v into imgs table: %v\n", img, alt, err)
}
words := stringMod(alt)
for _, word := range words {
if !isInStopWords(word, stopwords) {
wcount++
_, err := idx.dbase.Exec(`INSERT OR IGNORE INTO alt_terms(name) VALUES(?);`, word)
if err != nil {
log.Fatalf("Could not insert %v into alt_terms table: %v\n", word, err)
}
img_id := idx.getImgId(img)
term_id := idx.getAltWordId(word)
//log.Printf("img_id is: %v term_id is: %v\n", img_id, term_id)
_, err = idx.dbase.Exec(`INSERT OR IGNORE INTO alt_hits(term_id, url_id, img_id, freq) VALUES (?,?,?,?) ON CONFLICT(term_id, img_id) DO UPDATE SET freq = freq + 1;`,
int(term_id), url_id, int(img_id), 1)
if err != nil {
log.Fatalf("Could not insert alt_hit for %v: %v\n", word, err)
}
}
}
}
_, err := idx.dbase.Exec(`UPDATE urls SET altw_count = ? WHERE id = ?`, wcount, url_id)
if err != nil {
log.Fatalf("Could not update altw_count: %v\n", err)
}
}
func (idx *IIndex) dadd(ex *ExtractResult) {
wcount := 0 //DB
idx.mutex.Lock()
defer idx.mutex.Unlock()
url_id := idx.getUrlId(ex.url)
go idx.addImgs(url_id, ex)
_, err := idx.dbase.Exec(`UPDATE urls SET title = ? WHERE id = ?`, ex.title, url_id)
if err != nil {
log.Fatalf("Could not insert title to urls table: %v\n", err)
}
insertWords := "INSERT OR IGNORE INTO terms(name) VALUES "
words := []interface{}{}
insertHits := "INSERT OR IGNORE INTO hits(term_id, url_id, freq) VALUES "
hits := []interface{}{}
insertBiGram := "INSERT OR IGNORE INTO bigrams(term_id1, term_id2, url_id, freq) VALUES "
bigrams := []interface{}{}
for _, word := range ex.words {
if !isInStopWords(word, stopwords) { // if word is not in stopwords
wcount++
insertWords += "(?),"
words = append(words, word)
}
}
// for words
if wcount == 0 {
return
}
// removes last comma
insertWords = insertWords[0:len(insertWords) - 1]
stmt, err := idx.dbase.Prepare(insertWords)
if err != nil {
log.Fatalf("Could not prepare words stmt: %v\n", err)
}
_, err = stmt.Exec(words...)
if err != nil {
log.Fatalf("Could not execute words: %v\n", err)
}
idx.addWordCount(ex.url, wcount)
for ix, word := range words {
// for single-word hit table
term_id1 := idx.getWordId(word.(string))
insertHits += "(?,?,?),"
hits = append(hits, term_id1, url_id, 1)
// checks if there is a next word for bigrams table
if ix + 1 < len(words) {
term_id2 := idx.getWordId(words[ix+1].(string))
insertBiGram += "(?,?,?,?),"
bigrams = append(bigrams, term_id1, term_id2, url_id, 1)
}
}
// removes last comma
insertHits = insertHits[0:len(insertHits) - 1]
insertHits += "ON CONFLICT(term_id, url_id) DO UPDATE SET freq = freq + 1"
stmt, err = idx.dbase.Prepare(insertHits)
if err != nil {
log.Fatalf("Could not prepare hits stmt: %v\n", err)
}
_, err = stmt.Exec(hits...)
if err != nil {
log.Fatalf("Could not execute hits: %v\n", err)
}
// removes last comma
insertBiGram = insertBiGram[0:len(insertBiGram) - 1]
insertBiGram += "ON CONFLICT(term_id1, term_id2, url_id) DO UPDATE SET freq = freq + 1"
stmt, err = idx.dbase.Prepare(insertBiGram)
if err != nil {
log.Fatalf("Could not prepare bigram hits stmt: %v\n", err)
}
_, err = stmt.Exec(bigrams...)
if err != nil {
log.Fatalf("Could not execute bigram hits: %v\n", err)
}
fmt.Println("done")
}
func dcrawl(url string, idx *IIndex) {
// pass url to crawl robots.txt
err, agents := robots(url)
if err == nil {
idx.rules = agents
}
idx.Open()
// channel initialization
dlInC := make(chan string, 100000)
dlOutC := make(chan DownloadResult, 100000)
exOutC := make(chan ExtractResult, 100000)
quitC := make(chan bool, 1)
// to check if crawled
urls := make(map[string]struct{})
// initializing tables
dbInIt(idx.dbase)
// queue of urls to crawl
s := []string{}
// for href crawling
/*s = append(s, url)
idx.insertUrl(url)
urls[url] = struct{}{}
dlInC <- url*/
// for sitemap crawling
sites := readSitemap()
idx.insertUrl(sites[0])
dlInC <- sites[0]
for idx, site := range sites {
if idx < 5000 {
s = append(s, site)
} else {
break
}
}
s = s[1:]
go func() {
for {
prevSize, _ := idx.getSize()
time.Sleep(10 * time.Second)
currSize, _ := idx.getSize()
if currSize == prevSize && len(dlInC) + len(dlOutC) + len(exOutC) == 0 {
quitC <- true
break
}
}
}()
outer:
for {
select {
case url := <-dlInC:
go download(url, dlOutC)
case dlRes := <-dlOutC:
go extract(&dlRes, exOutC)
case exRes := <-exOutC:
//log.Println("Adding: " + exRes.url)
idx.dadd(&exRes)
// for href crawling to add urls
// s = append(s, exRes.urls...)
for i := 0; i < len(s); i++ {
url := s[i]
// has not been crawled
if _, ok := urls[url]; !ok {
// check if in disallow
disa, _ := isDisallowed(url, idx.rules)
// is allowed
if !disa {
idx.insertUrl(url)
urls[url] = struct{}{}
if idx.rules[0].CrawlDelay == 100 {
time.Sleep(100 * time.Millisecond)
} else {
time.Sleep(time.Duration(idx.rules[0].CrawlDelay) * time.Second)
}
dlInC <- url
}
}
}
case end := <-quitC:
fmt.Println(end)
close(dlInC)
close(dlOutC)
close(exOutC)
close(quitC)
break outer
}
}
fmt.Println("complete")
}