-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
186 lines (157 loc) · 3.7 KB
/
processor.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
package main
import (
"crypto/tls"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/oglinuk/goccer"
"golang.org/x/net/html"
)
// processor orchestrates the crawling and creation of document entries
// that are stored in an sqlite3 database
type processor struct {
DomainCheck map[string]struct{}
URLCheck map[string]struct{}
Seeds []string
Doc *html.Tokenizer
Ttype html.TokenType
T html.Token
C *http.Client
Wp *goccer.Workerpool
Dbi *DBInstance
}
// NewProcessor constructor
func NewProcessor() *processor {
c := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Timeout: time.Second * 15,
}
return &processor{
DomainCheck: make(map[string]struct{}),
URLCheck: make(map[string]struct{}),
Seeds: []string{
"https://fourohfournotfound.com",
"https://fourohfournotfound.com",
"https://en.wikipedia.org/wiki/Search_engine",
"https://creativecommons.org",
},
Doc: nil,
Ttype: 0,
T: html.Token{},
C: c,
Wp: goccer.NewWorkerpool(),
Dbi: NewDBInstance(),
}
}
// iterate p.Doc TokenType and Token
func (p *processor) iterate() {
p.Ttype = p.Doc.Next()
p.T = p.Doc.Token()
}
// getMetadata extracts a title and description from an HTML page
func (p *processor) getMetadata(body io.ReadCloser) (string, string) {
title := ""
desc := ""
p.Doc = html.NewTokenizer(body)
for {
p.iterate()
if p.Ttype == html.ErrorToken {
break
}
// Get title from <title> tag
if p.Ttype == html.StartTagToken && p.T.Data == "title" {
p.iterate()
title = p.T.Data
}
// TODO: Add check for schema.org format
// Check for open graph <meta property="og:..." content="..."> tags
if p.Ttype == html.StartTagToken && p.T.Data == "meta" {
prop := ""
for _, a := range p.T.Attr {
if a.Key == "content" && prop == "og:title" {
title = a.Val
}
if a.Key == "content" && prop == "og:description" {
desc = a.Val
}
prop = a.Val
}
}
// If all else fails use <h1> for title
if p.Ttype == html.StartTagToken && p.T.Data == "h1" && title == "" {
p.iterate()
title = p.T.Data
}
// If all else fails use the first <p> tag for desc
if p.Ttype == html.StartTagToken && p.T.Data == "p" && desc == "" {
for {
p.iterate()
if p.Ttype == html.TextToken {
if desc == "" {
desc = p.T.Data
} else {
desc = fmt.Sprintf("%s%s", desc, p.T.Data)
}
}
if p.Ttype == html.EndTagToken && p.T.Data == "p" {
desc = strings.Join(strings.Fields(desc), " ")
break
}
if p.Ttype == html.ErrorToken {
break
}
}
}
}
return title, desc
}
// createDoc from given URL and insert into database
func (p *processor) createDoc(URL string) {
resp, err := p.C.Get(URL)
if err != nil {
log.Printf("p.C.Get: %s\n", err.Error())
return
}
defer resp.Body.Close()
title, desc := p.getMetadata(resp.Body)
if title == "" && desc == "" {
return
}
log.Printf("Processed %s | Title: %s | Desc: %s\n", URL, title, desc)
doc := NewDocument(title, URL, desc)
p.Dbi.Insert(doc)
}
// filter already visited URLs
func (p *processor) filter() []string {
var filtered []string
for _, URL := range p.Seeds {
if _, exists := p.URLCheck[URL]; !exists {
p.URLCheck[URL] = struct{}{}
filtered = append(filtered, URL)
}
}
return filtered
}
func (p *processor) process() {
collected := p.Wp.Queue(p.filter())
for _, c := range collected {
parsed, err := url.Parse(c)
if err != nil {
log.Printf("url.Parse: %s", err.Error())
}
URL := fmt.Sprintf("http://%s", parsed.Hostname())
if _, exists := p.DomainCheck[URL]; !exists {
p.DomainCheck[URL] = struct{}{}
p.createDoc(URL)
}
}
p.Seeds = collected
}