aboutsummaryrefslogtreecommitdiff
path: root/webCrawler2/crawler.go
blob: 8725df835da7e5505fbea10eb5980515932fd3a6 (plain)
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
package main

import "crypto/md5"
import "sync/atomic"
import "os"
import "fmt"
import "log"
import "time"
import "sync"
import "strconv"
import "strings"
import "net/url"
import "github.com/PuerkitoBio/goquery"

type link struct {
	u     *url.URL
	depth int
}

var mutex *sync.Mutex
var Prev map[string]bool
var tFile *os.File
var base string
var links_visited uint64 = 0

func validLink(s string) bool {
	return true
	//return (strings.HasSuffix(s, ".html") || strings.HasSuffix(s, "/") || strings.HasSuffix(s, "\\"))
}

func addLinks(doc *goquery.Document, jobs chan link, current link, depth int) {
	doc.Find("body a").Each(func(index int, item *goquery.Selection) {
		link_s, _ := item.Attr("href")

		d := depth + 1

		u, err := url.Parse(link_s)
		if err != nil {
			panic(err)
		}

		if !u.IsAbs() {
			u = current.u.ResolveReference(u)
		}
		if strings.Contains(u.String(), base) && validLink(u.String()) {
			mutex.Lock()
			if !Prev[u.String()] {
				jobs <- link{u, d}
				Prev[u.String()] = true
			}
			mutex.Unlock()
		}
	})
}

func consume(doc *goquery.Document, url link, worker_id int) {
	f, _ := os.Create(fmt.Sprintf("./pages/%x", md5.Sum([]byte(url.u.String()))))
	s, _ := doc.Html()
	f.Write([]byte(s))
}

func worker(done chan bool, jobs chan link, depth int, id int, total uint64) {
	for {
		x := atomic.LoadUint64(&links_visited)
		if x >= total {
			fmt.Printf("Max links reached\n\tlinks: %d total: %d\n", x, total)
			done <- true
			return
		}

		atomic.AddUint64(&links_visited, 1)
		select {
		case j := <-jobs:
			if j.depth < depth {
				doc, err := goquery.NewDocument(j.u.String())
				docs <- doc
				if err != nil {
					log.Print("Error Reading Document: " + j.u.String() + err.Error())
					break
				}

				fmt.Printf("Adding links from %s depth: %d...\n", j.u.String(), j.depth)

				consume(doc, j, id)
				addLinks(doc, jobs, j, j.depth, id)
			}
		case <-time.After(time.Second * 10):
			done <- true
			return
		}
	}
}

func init() {
	mutex = &sync.Mutex{}
	Prev = make(map[string]bool)
	var err error
	tFile, err = os.Create("./test.txt")
	if err != nil {
		panic(err)
	}
}

func main() {
	var d, w, b int
	var t uint64

	if len(os.Args) < 4 {
		fmt.Printf("usage: crawler url [depth max_links] [workers]\n")
		panic("test")
	}

	base = strings.TrimPrefix(os.Args[1], "http://www.")
	base = strings.TrimPrefix(base, "https://www.")
	if base == os.Args[1] {
		panic(base)
	}

	d, _ = strconv.Atoi(os.Args[2])
	b, _ = (strconv.Atoi(os.Args[3]))
	t = uint64(b)
	if len(os.Args) >= 4 {
		b, _ = (strconv.Atoi(os.Args[3]))
		t = uint64(b)

	} else {
		t = 10
	}
	if len(os.Args) == 5 {
		w, _ = strconv.Atoi(os.Args[4])
	} else {
		w = 4
	}

	links := make(chan link, 1024*1024)
	docs := make(chan *goquery.Document, 100)
	done := make(chan bool)

	u, err := url.Parse(os.Args[1])
	if err != nil {
		panic(err)
	}

	if !u.IsAbs() {
		panic("Cannot start with relative url")
	}
	links <- link{u, 0}

	//send first job

	for i := 0; i < w; i++ {
		go worker(done, jobs, d, i, t)
	}

	for i := 0; i < w*2; {
		<-done

		fmt.Printf("%d done\n", i)
		i++
	}

	tFile.Close()
	close(done)
	close(links)
	close(docs)
}