File size: 4,162 Bytes
a13d04f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package main

import (
	"bufio"
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"regexp"
	"strings"
	"sync"
	"sync/atomic"
	"time"
)

type Record struct {
	URL  string `json:"url"`
	Text string `json:"text"`
	TS   string `json:"ts"`
}

var (
	visited   = make(map[string]bool)
	visitedMu sync.RWMutex
	queue     = make(chan string, 1000000)
	stats     struct {
		pages, bytes, errors int64
	}
	client = &http.Client{
		Timeout: 5 * time.Second,
		Transport: &http.Transport{
			MaxIdleConns:        1000,
			MaxIdleConnsPerHost: 50,
			IdleConnTimeout:     30 * time.Second,
		},
	}
	linkRe   = regexp.MustCompile(`href=["']([^"']+)["']`)
	scriptRe = regexp.MustCompile(`(?is)<script[^>]*>.*?</script>|<style[^>]*>.*?</style>`)
	tagRe    = regexp.MustCompile(`<[^>]+>`)
	spaceRe  = regexp.MustCompile(`\s+`)
)

func hash(s string) string {
	h := md5.Sum([]byte(s))
	return hex.EncodeToString(h[:8])
}

func extractText(html string) string {
	text := scriptRe.ReplaceAllString(html, " ")
	text = tagRe.ReplaceAllString(text, " ")
	text = spaceRe.ReplaceAllString(text, " ")
	text = strings.TrimSpace(text)
	if len(text) > 50000 {
		text = text[:50000]
	}
	return text
}

func extractLinks(html, baseURL string) []string {
	base, err := url.Parse(baseURL)
	if err != nil {
		return nil
	}
	var links []string
	matches := linkRe.FindAllStringSubmatch(html, 100)
	for _, m := range matches {
		if len(m) > 1 {
			href := m[1]
			if u, err := url.Parse(href); err == nil {
				resolved := base.ResolveReference(u)
				if resolved.Scheme == "http" || resolved.Scheme == "https" {
					links = append(links, resolved.String())
				}
			}
		}
	}
	return links
}

func worker(id int, output chan<- Record) {
	for urlStr := range queue {
		h := hash(urlStr)
		visitedMu.RLock()
		seen := visited[h]
		visitedMu.RUnlock()
		if seen {
			continue
		}
		visitedMu.Lock()
		visited[h] = true
		visitedMu.Unlock()

		resp, err := client.Get(urlStr)
		if err != nil {
			atomic.AddInt64(&stats.errors, 1)
			continue
		}

		ct := resp.Header.Get("Content-Type")
		if resp.StatusCode != 200 || !strings.Contains(ct, "text/html") {
			resp.Body.Close()
			atomic.AddInt64(&stats.errors, 1)
			continue
		}

		body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
		resp.Body.Close()
		html := string(body)

		text := extractText(html)
		if len(text) > 200 {
			output <- Record{URL: urlStr, Text: text, TS: time.Now().UTC().Format("2006-01-02T15:04:05")}
			atomic.AddInt64(&stats.pages, 1)
			atomic.AddInt64(&stats.bytes, int64(len(text)))
		}

		for _, link := range extractLinks(html, urlStr) {
			select {
			case queue <- link:
			default:
			}
		}
	}
}

func main() {
	seeds := []string{
		"https://en.wikipedia.org/wiki/Main_Page",
		"https://en.wikipedia.org/wiki/Special:Random",
		"https://news.ycombinator.com/",
		"https://www.reddit.com/r/all/",
		"https://www.reddit.com/r/programming/",
		"https://stackoverflow.com/questions",
		"https://medium.com/",
		"https://dev.to/",
	}

	for _, s := range seeds {
		queue <- s
	}

	os.MkdirAll("/workspace/go_crawl", 0755)
	outPath := fmt.Sprintf("/workspace/go_crawl/crawl_%s.jsonl", time.Now().Format("20060102_150405"))
	f, _ := os.Create(outPath)
	defer f.Close()
	w := bufio.NewWriter(f)
	defer w.Flush()

	output := make(chan Record, 10000)

	go func() {
		for rec := range output {
			data, _ := json.Marshal(rec)
			w.Write(data)
			w.WriteByte('\n')
		}
	}()

	start := time.Now()
	go func() {
		for {
			time.Sleep(10 * time.Second)
			elapsed := time.Since(start).Minutes()
			if elapsed > 0 {
				p := atomic.LoadInt64(&stats.pages)
				b := atomic.LoadInt64(&stats.bytes)
				e := atomic.LoadInt64(&stats.errors)
				fmt.Printf("[%s] %d pgs | %.0fMB | %.1fMB/min | Q:%d | Err:%d\n",
					time.Now().Format("15:04:05"), p, float64(b)/1024/1024, float64(b)/1024/1024/elapsed, len(queue), e)
			}
		}
	}()

	fmt.Printf("=== GO STDLIB CRAWLER ===\nWorkers: 1000\nOutput: %s\n\n", outPath)

	var wg sync.WaitGroup
	for i := 0; i < 1000; i++ {
		wg.Add(1)
		go func(id int) {
			defer wg.Done()
			worker(id, output)
		}(i)
	}
	wg.Wait()
	close(output)
}