-
Notifications
You must be signed in to change notification settings - Fork 1
/
stat.go
151 lines (133 loc) · 3.57 KB
/
stat.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
// Copyright 2019 vogo. All rights reserved.
package aliwepaystat
import (
"bufio"
"bytes"
"encoding/csv"
"io"
"log"
"os"
"path/filepath"
"sort"
"strings"
"github.com/jszwec/csvutil"
"golang.org/x/text/transform"
)
const statOutputFilePrefix = "aliwepaystat-"
func GenHtmlStat(statDir string) {
log.Println("------------------------------------")
sort.Strings(yearMonths)
indexStatFileName := statOutputFilePrefix + "index.html"
indexStatFilePath := filepath.Join(statDir, indexStatFileName)
log.Printf("\t统计文件: %s", indexStatFilePath)
f, err := os.Create(indexStatFilePath)
if err != nil {
panic(err)
}
genIndexStatReport(f)
_ = f.Close()
for _, yearMonth := range yearMonths {
monthStatFileName := statOutputFilePrefix + yearMonth + ".html"
monthStatFilePath := filepath.Join(statDir, monthStatFileName)
log.Printf("\t统计文件: %s", monthStatFilePath)
f, err := os.Create(monthStatFilePath)
if err != nil {
panic(err)
}
genMonthStatReport(f, monthStatsMap[yearMonth])
_ = f.Close()
}
}
func ParseCsvTransDir(baseDir string) {
files, err := os.ReadDir(baseDir)
if err != nil {
log.Fatalf("读取目录失败! %v", err)
}
for _, file := range files {
fileName := file.Name()
if !strings.HasSuffix(fileName, ".csv") {
continue
}
filePath := filepath.Join(baseDir, fileName)
if strings.Contains(fileName, "alipay") {
ParseCsvTransFile(filePath, TransParserAlipay)
} else if strings.Contains(fileName, "微信") {
ParseCsvTransFile(filePath, TransParserWechat)
} else {
log.Fatalf("未知的账单文件(文件名需包含\"alipay\"或\"微信\"): %s", filePath)
}
}
}
func ParseCsvTransFile(filePath string, parser TransParser) {
log.Println()
log.Println("------------------------------------")
log.Printf("-----> parse file: %s", filePath)
file, err := os.Open(filePath)
if err != nil {
log.Fatalf("打开文件错误! %v", err)
}
defer file.Close()
transHeader, err := csvutil.Header(parser.NewTrans(), "csv")
if err != nil {
log.Fatalf("程序错误! %v", err)
}
transformReader := transform.NewReader(file, parser.Enc().NewDecoder())
reader := bufio.NewReader(transformReader)
buf := bytes.NewBuffer(nil)
dataLineStarted := false
// loop read the previous content until the csv title
for {
lineData, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
break
}
log.Fatalf("读取文件错误! %v", err)
}
if len(lineData) == 0 {
continue
}
if dataLineStarted {
lineData = replaceCsvLineFieldsSuffixBlank(lineData)
line := string(lineData)
if len(strings.Split(line, ",")) != parser.FieldNum() {
// ignore none data line
printDataDescLine(line)
continue
}
buf.Write(lineData)
buf.WriteByte('\n')
continue
}
line := string(lineData)
line = strings.ReplaceAll(line, " ", "")
if line == parser.CsvHeader() {
dataLineStarted = true
continue
}
printDataDescLine(line)
}
formattedReader := bytes.NewReader(buf.Bytes())
csvReader := csv.NewReader(formattedReader)
csvReader.TrimLeadingSpace = true
csvReader.FieldsPerRecord = parser.FieldNum()
dec, err := csvutil.NewDecoder(csvReader, transHeader...)
if err != nil {
log.Fatalf("创建解析器失败! %v", err)
}
for {
trans := parser.NewTrans()
if err := dec.Decode(trans); err == io.EOF {
break
} else if err != nil {
log.Printf("解析数据失败! %v", err)
continue
}
getMonthStat(trans.YearMonth()).add(trans)
}
}
func printDataDescLine(line string) {
if !strings.HasPrefix(line, "----") && !strings.HasPrefix(line, ",,,,") {
log.Println(line)
}
}