-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
246 lines (223 loc) · 7.01 KB
/
utils.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package krx
import (
_ "embed"
"encoding/json"
"encoding/xml"
"io"
"net/http"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type StockInformation struct {
XMLName xml.Name `xml:"stockprice"`
Text string `xml:",chardata"`
Querytime string `xml:"querytime,attr"`
TBLDailyStock struct {
Text string `xml:",chardata"`
DailyStock []struct {
Text string `xml:",chardata"`
DayDate string `xml:"day_Date,attr"`
DayEndPrice string `xml:"day_EndPrice,attr"`
DayDungrak string `xml:"day_Dungrak,attr"`
DayGetDebi string `xml:"day_getDebi,attr"`
DayStart string `xml:"day_Start,attr"`
DayHigh string `xml:"day_High,attr"`
DayLow string `xml:"day_Low,attr"`
DayVolume string `xml:"day_Volume,attr"`
DayGetAmount string `xml:"day_getAmount,attr"`
}
} `xml:"TBL_DailyStock"`
TBLAskPrice struct {
Text string `xml:",chardata"`
AskPrice []struct {
Text string `xml:",chardata"`
MemberMemdoMem string `xml:"member_memdoMem,attr"`
MemberMemdoVol string `xml:"member_memdoVol,attr"`
MemberMemsoMem string `xml:"member_memsoMem,attr"`
MemberMesuoVol string `xml:"member_mesuoVol,attr"`
} `xml:"AskPrice"`
} `xml:"TBL_AskPrice"`
TBLStockInfo struct {
Text string `xml:",chardata"`
JongName string `xml:"JongName,attr"`
CurJuka string `xml:"CurJuka,attr"`
DungRak string `xml:"DungRak,attr"`
Debi string `xml:"Debi,attr"`
PrevJuka string `xml:"PrevJuka,attr"`
Volume string `xml:"Volume,attr"`
Money string `xml:"Money,attr"`
StartJuka string `xml:"StartJuka,attr"`
HighJuka string `xml:"HighJuka,attr"`
LowJuka string `xml:"LowJuka,attr"`
High52 string `xml:"High52,attr"`
Low52 string `xml:"Low52,attr"`
UpJuka string `xml:"UpJuka,attr"`
DownJuka string `xml:"DownJuka,attr"`
Per string `xml:"Per,attr"`
Amount string `xml:"Amount,attr"`
FaceJuka string `xml:"FaceJuka,attr"`
} `xml:"TBL_StockInfo"`
TBLHoga struct {
Text string `xml:",chardata"`
MesuJan0 string `xml:"mesuJan0,attr"`
MesuHoka0 string `xml:"mesuHoka0,attr"`
MesuJan1 string `xml:"mesuJan1,attr"`
MesuHoka1 string `xml:"mesuHoka1,attr"`
MesuJan2 string `xml:"mesuJan2,attr"`
MesuHoka2 string `xml:"mesuHoka2,attr"`
MesuJan3 string `xml:"mesuJan3,attr"`
MesuHoka3 string `xml:"mesuHoka3,attr"`
MesuJan4 string `xml:"mesuJan4,attr"`
MesuHoka4 string `xml:"mesuHoka4,attr"`
MedoJan0 string `xml:"medoJan0,attr"`
MedoHoka0 string `xml:"medoHoka0,attr"`
MedoJan1 string `xml:"medoJan1,attr"`
MedoHoka1 string `xml:"medoHoka1,attr"`
MedoJan2 string `xml:"medoJan2,attr"`
MedoHoka2 string `xml:"medoHoka2,attr"`
MedoJan3 string `xml:"medoJan3,attr"`
MedoHoka3 string `xml:"medoHoka3,attr"`
MedoJan4 string `xml:"medoJan4,attr"`
MedoHoka4 string `xml:"medoHoka4,attr"`
} `xml:"TBL_Hoga"`
TBLTimeConclude struct {
Text string `xml:",chardata"`
TBLTimeConclude []struct {
Text string `xml:",chardata"`
Time string `xml:"time,attr"`
Negoprice string `xml:"negoprice,attr"`
Dungrak string `xml:"Dungrak,attr"`
Debi string `xml:"Debi,attr"`
Sellprice string `xml:"sellprice,attr"`
Buyprice string `xml:"buyprice,attr"`
Amount string `xml:"amount,attr"`
} `xml:"TBL_TimeConclude"`
} `xml:"TBL_TimeConclude"`
StockInfo struct {
Text string `xml:",chardata"`
KosdaqJisu string `xml:"kosdaqJisu,attr"`
KosdaqJisuBuho string `xml:"kosdaqJisuBuho,attr"`
KosdaqJisuDebi string `xml:"kosdaqJisuDebi,attr"`
StarJisu string `xml:"starJisu,attr"`
StarJisuBuho string `xml:"starJisuBuho,attr"`
StarJisuDebi string `xml:"starJisuDebi,attr"`
Jisu50 string `xml:"jisu50,attr"`
Jisu50Buho string `xml:"jisu50Buho,attr"`
Jisu50Debi string `xml:"jisu50Debi,attr"`
MyNowTime string `xml:"myNowTime,attr"`
MyJangGubun string `xml:"myJangGubun,attr"`
MyPublicPrice string `xml:"myPublicPrice,attr"`
Krx100Jisu string `xml:"krx100Jisu,attr"`
Krx100buho string `xml:"krx100buho,attr"`
Krx100Debi string `xml:"krx100Debi,attr"`
KospiJisu string `xml:"kospiJisu,attr"`
KospiBuho string `xml:"kospiBuho,attr"`
KospiDebi string `xml:"kospiDebi,attr"`
Kospi200Jisu string `xml:"kospi200Jisu,attr"`
Kospi200Buho string `xml:"kospi200Buho,attr"`
Kospi200Debi string `xml:"kospi200Debi,attr"`
} `xml:"stockInfo"`
}
type Company struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
Sector string `json:"sector"`
Industry string `json:"industry"`
ListingDate string `json:"listing_date"`
SettlementMonth string `json:"settlement_month"`
Representative string `json:"representative"`
Homepage string `json:"homepage"`
Region string `json:"region"`
}
type UpDown int
const (
UpperLimit UpDown = iota
Increase
Keep
LowerLimit
Decrease
Unknown
)
//go:embed data/data.json
var file []byte
func getStockInfoBySymbol(symbol string) (*StockInformation, error) {
url := "http://asp1.krx.co.kr/servlet/krx.asp.XMLSiseEng?code="
resp, err := http.Get(url + symbol)
if err != nil {
return nil, errors.WithStack(err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Error(err)
}
}()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.WithStack(err)
}
var stockInfo *StockInformation
if err := xml.Unmarshal(bodyBytes, &stockInfo); err != nil {
return nil, errors.WithStack(err)
}
if len(stockInfo.TBLDailyStock.DailyStock) == 0 {
return nil, status.Errorf(codes.NotFound, "Stock not found by given symbol: %s", symbol)
}
return stockInfo, nil
}
func getKRXMarketInfo() (*StockInformation, error) {
url := "http://asp1.krx.co.kr/servlet/krx.asp.XMLSiseEng?code="
resp, err := http.Get(url + "000000")
if err != nil {
return nil, errors.WithStack(err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Error(err)
}
}()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.WithStack(err)
}
var stockInfo *StockInformation
if err := xml.Unmarshal(bodyBytes, &stockInfo); err != nil {
return nil, errors.WithStack(err)
}
return stockInfo, nil
}
func GetSymbolByCompanyName(name string) (string, error) {
companyInfo, err := getCompanyListFromJsonFile()
if err != nil {
return "", err
}
for _, c := range companyInfo {
if c.Name == name {
return c.Symbol, nil
}
}
return "", status.Errorf(codes.NotFound, "Symbol is not found by given company name: %s", name)
}
func getCompanyListFromJsonFile() ([]*Company, error) {
var companies []*Company
if err := json.Unmarshal(file, &companies); err != nil {
return nil, errors.WithStack(err)
}
return companies, nil
}
func castToUpDown(num string) UpDown {
switch num {
case "1":
return UpperLimit
case "2":
return Increase
case "3":
return Keep
case "4":
return LowerLimit
case "5":
return Decrease
}
return Unknown
}