-
Notifications
You must be signed in to change notification settings - Fork 1
/
postprocess.go
108 lines (97 loc) · 2.16 KB
/
postprocess.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
package youcrawl
import (
"encoding/csv"
"fmt"
"os"
)
type PostProcess interface {
Process(store GlobalStore) error
}
type OutputCSVPostProcessOption struct {
// output path.
// if not provided,use `./output.csv` as default value
OutputPath string
// with header.
// default : false
WithHeader bool
// key to write
// if not provided,will write all key
Keys []string
// key to csv column name.
// if not provide,use key name as csv column name
KeysMapping map[string]string
// if value not exist in item.
// by default,use empty string
NotExistValue string
}
type OutputCSVPostProcess struct {
option OutputCSVPostProcessOption
}
func NewOutputCSVPostProcess(option OutputCSVPostProcessOption) *OutputCSVPostProcess {
return &OutputCSVPostProcess{
option: option,
}
}
func (o *OutputCSVPostProcess) Process(store GlobalStore) error {
data := store.GetValue("items")
if data == nil {
return nil
}
output := data.([]map[string]interface{})
file, err := os.Create(o.option.OutputPath)
defer file.Close()
if err != nil {
return err
}
csvRows := make([][]string, 0)
//scan keys
keys := o.option.Keys
if keys == nil {
keys := make([]string, 0)
seenKeys := make(map[string]bool)
for _, item := range output {
for key := range item {
_, hasSeen := seenKeys[key]
if !hasSeen {
keys = append(keys, key)
seenKeys[key] = true
}
}
}
}
if o.option.WithHeader {
if o.option.KeysMapping == nil {
csvRows = append(csvRows, keys)
} else {
colHeader := make([]string, 0)
for _, key := range keys {
name, exist := o.option.KeysMapping[key]
if !exist {
colHeader = append(colHeader, key)
} else {
colHeader = append(colHeader, name)
}
}
csvRows = append(csvRows, colHeader)
}
}
for _, item := range output {
row := make([]string, 0)
for _, key := range keys {
value, exist := item[key]
if !exist {
row = append(row, o.option.NotExistValue)
} else {
row = append(row, fmt.Sprintf("%v", value))
}
}
csvRows = append(csvRows, row)
}
writer := csv.NewWriter(file)
defer writer.Flush()
err = writer.WriteAll(csvRows)
if err != nil {
return err
}
return nil
}