-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrodatago.go
More file actions
200 lines (190 loc) · 4.59 KB
/
microdatago.go
File metadata and controls
200 lines (190 loc) · 4.59 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
package microdatago
import (
"bytes"
"encoding/json"
"io"
"net/url"
"reflect"
"strings"
"unicode"
"github.com/diggernaut/cast"
"github.com/diggernaut/goquery"
)
// Parser is an HTML parser that extracts microdata
type Parser struct {
r io.Reader
Microdata []map[string]interface{}
baseURL *url.URL
}
// JSON converts the data object to JSON
func (p *Parser) JSON() ([]byte, error) {
b, err := json.Marshal(p.Microdata)
if err != nil {
return nil, err
}
return b, nil
}
// NewParser creates a new parser for extracting microdata
// r is a reader over an HTML document
// baseURL is the base URL for resolving relative URLs
func NewParser(r io.Reader, baseURL *url.URL) *Parser {
return &Parser{
r: r,
baseURL: baseURL,
}
}
// Parse the document and return an error if there is any issue with parsing
func (p *Parser) Parse() error {
dom, err := goquery.NewDocumentFromReader(p.r)
if err != nil {
return err
}
// Doing DOM clean-up, filter out nodes without microdata markup
selector := dom.Find("[itemprop],[itemscope]")
selector2 := dom.Find("*")
selector2.Each(func(i int, s2 *goquery.Selection) {
found := false
selector.Each(func(i int, s *goquery.Selection) {
if s2.IsSelection(s) {
found = true
return
}
sel := s2.HasSelection(s)
if sel.Length() > 0 {
found = true
return
}
})
if !found {
s2.Remove()
}
})
selector = dom.Find(":not([itemprop],[itemscope])")
for selector.Length() > 0 {
selector.Each(func(i int, s *goquery.Selection) {
if _, ok := s.Attr("itemprop"); ok {
return
}
if _, ok := s.Attr("itemscope"); ok {
return
}
html, _ := s.Html()
s.ReplaceWithHtml(html)
})
selector = dom.Find(":not([itemprop],[itemscope])")
}
// Parsing DOM with microdata to the object
selector = dom.ChildrenFiltered("*")
selector.Each(func(i int, s *goquery.Selection) {
item := make(map[string]interface{})
p.extractItem(s, item)
p.Microdata = append(p.Microdata, item)
})
return nil
}
func (p *Parser) extractItem(selector *goquery.Selection, item map[string]interface{}) {
var fieldname string
if itemprop, ok := selector.Attr("itemprop"); ok {
fieldname = itemprop
} else {
if itemtype, ok := selector.Attr("itemtype"); ok {
itemtypeComponents := strings.Split(itemtype, "/")
if len(itemtypeComponents) > 0 {
fieldname = itemtypeComponents[len(itemtypeComponents)-1]
} else {
fieldname = itemtype
}
}
}
sel := selector.ChildrenFiltered("*")
if sel.Length() > 0 {
newItem := make(map[string]interface{})
if href, ok := selector.Attr("href"); ok {
if href != "" {
relURL, err := url.Parse(href)
if err == nil {
url := p.baseURL.ResolveReference(relURL)
newItem["url"] = url.String()
}
}
}
sel.Each(func(i int, s *goquery.Selection) {
p.extractItem(s, newItem)
})
if currentValue, ok := item[fieldname]; ok {
if it := reflect.TypeOf(currentValue).Kind(); it == reflect.Slice {
anArray := cast.ToSlice(currentValue)
anArray = append(anArray, newItem)
item[fieldname] = anArray
} else {
var anArray []interface{}
anArray = append(anArray, currentValue, newItem)
item[fieldname] = anArray
}
} else {
item[fieldname] = newItem
}
} else {
value := ""
if content, ok := selector.Attr("content"); ok {
if content != "" {
value = content
}
}
if value == "" {
if content, ok := selector.Attr("href"); ok {
if content != "" {
relURL, err := url.Parse(content)
if err == nil {
url := p.baseURL.ResolveReference(relURL)
value = url.String()
}
}
}
}
if value == "" {
if content, ok := selector.Attr("src"); ok {
if content != "" {
relURL, err := url.Parse(content)
if err == nil {
url := p.baseURL.ResolveReference(relURL)
value = url.String()
}
}
}
}
if value == "" {
text := strings.TrimSpace(stringMinifier(selector.Text()))
value = text
}
if currentValue, ok := item[fieldname]; ok {
if it := reflect.TypeOf(currentValue).Kind(); it == reflect.Slice {
anArray := cast.ToSlice(currentValue)
anArray = append(anArray, value)
item[fieldname] = anArray
} else {
var anArray []interface{}
anArray = append(anArray, currentValue, value)
item[fieldname] = anArray
}
} else {
item[fieldname] = value
}
}
}
func stringMinifier(in string) string {
buf := bytes.NewBufferString("")
white := false
for _, c := range in {
if unicode.IsSpace(c) {
if !white {
buf.WriteString(" ")
}
white = true
} else {
buf.WriteString(string(c))
white = false
}
}
return buf.String()
}