-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (109 loc) · 2.4 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"strings"
"github.com/tychy/toukibo-parser/pdf"
"github.com/tychy/toukibo-parser/toukibo"
)
var (
mode string
path string
target string
)
func main() {
flag.StringVar(&mode, "mode", "run", "run or find")
flag.StringVar(&path, "path", "testdata/pdf/sample1.pdf", "pdf file path")
flag.StringVar(&target, "target", "", "")
flag.Parse()
switch mode {
case "run":
mainRun()
case "find":
mainFind(target)
default:
fmt.Println("invalid mode")
}
}
func mainRun() {
content, err := readPdf(path)
if err != nil {
panic(err)
}
h, err := toukibo.Parse(content)
if err != nil {
panic(err)
}
repName, err := h.GetHoujinRepresentativeNames()
if err != nil {
panic(err)
}
execs, err := h.GetHoujinExecutives()
if err != nil {
panic(err)
}
execNames, err := h.GetHoujinExecutiveNames()
if err != nil {
panic(err)
}
stock := h.GetHoujinStock()
fmt.Println("HoujinKaku: " + h.GetHoujinKaku())
fmt.Println("HoujinName: " + h.GetHoujinName())
fmt.Println("HoujinAddress: " + h.GetHoujinAddress())
fmt.Print("HoujinExecutiveValues: \n" + execs.String())
fmt.Println("HoujinExecutiveNames: [" + strings.Join(execNames, ",") + "]")
fmt.Println("HoujinRepresentativeNames: [" + strings.Join(repName, ",") + "]")
fmt.Printf("HoujinCapital: %d\n", h.GetHoujinCapital())
fmt.Printf("HoujinStock: %d\n", stock.Total)
fmt.Print("HoujinPreferredStock: \n" + stock.String())
fmt.Println("HoujinCreatedAt: " + h.GetHoujinCreatedAt())
fmt.Println("HoujinBankruptedAt: " + h.GetHoujinBankruptedAt())
fmt.Println("HoujinDissolvedAt: " + h.GetHoujinDissolvedAt())
fmt.Println("HoujinContinuedAt: " + h.GetHoujinContinuedAt())
return
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func mainFind(s string) {
content, err := readPdf(path)
if err != nil {
panic(err)
}
if strings.Contains(content, s) {
fmt.Println("found in " + path)
// 前後を表示
for {
idx := strings.Index(content, s)
if idx == -1 {
break
}
fmt.Println(content[max(0, idx-60):min(len(content), idx+240)])
content = content[idx+1:]
}
}
return
}
func readPdf(path string) (string, error) {
r, err := pdf.Open(path)
if err != nil {
return "", err
}
var buf bytes.Buffer
b, err := r.GetPlainText()
if err != nil {
return "", err
}
buf.ReadFrom(b)
return buf.String(), nil
}