-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.go
58 lines (45 loc) · 1.33 KB
/
extractor.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
package main
import "github.com/PuerkitoBio/goquery"
// Extractor is an interface that wraps methods for elements data extractors
type Extractor interface {
Name() string
Extract(sel *goquery.Selection) Items
}
// Extractors is a collection of named extractors
type Extractors map[string]Extractor
// Item is a map returned from extractors
type Item map[string]string
// Items is a collection of items
type Items []Item
// ItemExtractor executes multiple DataExtractors and it's used to extract Items
type ItemExtractor struct {
name string
Selector string
DataExtractors DataExtractors
}
// NewItemExtractor returns a new ItemExtractor with a given name and extractors
func NewItemExtractor(name string, de DataExtractors) *ItemExtractor {
return &ItemExtractor{
name: name,
DataExtractors: de,
}
}
// Extract returns Items querying `sel` using ItemExtractor's DataExtractors
func (e *ItemExtractor) Extract(sel *goquery.Selection) Items {
var items Items
if e.Selector != "" {
sel = sel.Find(e.Selector)
}
sel.Each(func(i int, s *goquery.Selection) {
item := make(Item)
for name, ext := range e.DataExtractors {
item[name] = ext.Extract(s)
}
items = append(items, item)
})
return items
}
// Name returns the ItemExtractor's name
func (e *ItemExtractor) Name() string {
return e.name
}