/
catalog.go
164 lines (141 loc) · 3.57 KB
/
catalog.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
package service
import (
"bufio"
"fmt"
"io"
"io/fs"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/antchfx/htmlquery"
"github.com/go-gota/gota/dataframe"
"github.com/google/wire"
)
type paifuList struct {
south3 []paifu
south4 []paifu
east3 []paifu
east4 []paifu
}
type paifu struct {
Date string `json:"date"`
GameType string `json:"game_type"`
LogID string `json:"log_id"`
}
type catalog struct {
inputPath string
filesPaths []string
data *paifuList
}
const DEST_PATH = "input/catalog"
func NewCatalog(iPath string) *catalog {
return &catalog{
inputPath: iPath,
filesPaths: make([]string, 0),
data: &paifuList{
south3: make([]paifu, 0),
south4: make([]paifu, 0),
east3: make([]paifu, 0),
east4: make([]paifu, 0),
},
}
}
func (c *catalog) Archive() {
files, _ := c.GetFiles(c.inputPath)
c.filesPaths = append(c.filesPaths, files...)
for _, v := range c.filesPaths {
c.ReadSingleFile(v)
}
east4csv, err := os.OpenFile(path.Join(DEST_PATH, "east4.csv"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
fmt.Println(err)
return
}
defer east4csv.Close()
fmt.Println(len(c.data.south4))
df := dataframe.LoadStructs(c.data.south4)
if err := df.WriteCSV(east4csv); err != nil {
fmt.Println(err)
}
}
func (c *catalog) GetFiles(inputPath string) ([]string, error) {
var filesPaths []string
filepath.Walk(inputPath, func(filePath string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
dir := path.Dir(filePath)
ext := path.Ext(filePath)
if ext != ".html" {
return nil
}
file := filepath.Join(dir, info.Name())
filesPaths = append(filesPaths, file)
return nil
})
return filesPaths, nil
}
func (c *catalog) ReadSingleFile(filePath string) {
filename := path.Base(filePath)
re := regexp.MustCompile(`^scc(.*)\.html$`)
fileDate := re.ReplaceAllString(filename, "$1")
// fileTime, _ := time.Parse(fileDate, "20060102")
// yearS := string(rune(fileTime.Year()))
f, err := os.Open(filePath)
if err != nil {
return
}
defer f.Close()
r := bufio.NewReader(f)
for {
// ReadLine is a low-level line-reading primitive.
// Most callers should use ReadBytes('\n') or ReadString('\n') instead or use a Scanner.
bytes, _, err := r.ReadLine()
if err == io.EOF {
break
}
if err != nil {
return
}
line := string(bytes)
lineContentList := strings.Split(line, "|")
min := strings.TrimSpace(lineContentList[0])
sec := strings.TrimSpace(lineContentList[1])
// gameType := strings.TrimSpace(lineContentList[2])
doc, err := htmlquery.Parse(strings.NewReader(strings.TrimSpace(lineContentList[3])))
if err != nil {
fmt.Println(err.Error())
return
}
link := htmlquery.SelectAttr(htmlquery.FindOne(doc, "//a/@href"), "href")
u, _ := url.Parse(link)
date, _ := time.Parse("20060102 15:04:05", fileDate+" "+min+":"+sec)
var data = paifu{}
data.Date = date.Format(time.RFC3339)
data.LogID = u.Query().Get("log")
data.GameType = strings.Split(data.LogID, "-")[1]
switch data.GameType {
case "00b1":
c.data.east3 = append(c.data.east3, data)
case "00f1":
c.data.east3 = append(c.data.south3, data)
case "00b9":
c.data.south3 = append(c.data.south3, data)
case "00f9":
c.data.south3 = append(c.data.south3, data)
case "00a1":
c.data.east4 = append(c.data.east4, data)
case "00e1":
c.data.east4 = append(c.data.east4, data)
case "00a9":
c.data.south4 = append(c.data.south4, data)
case "00e9":
c.data.south4 = append(c.data.south4, data)
}
}
}
var CatalogSet = wire.NewSet(NewCatalog)