-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.go
196 lines (162 loc) · 4.7 KB
/
helper.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
package jquants_api_go
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"olympos.io/encoding/edn"
"os"
"time"
)
const BASE_URL = "https://api.jpx-jquants.com/v1"
const REFRESH_TOKEN_FILE = "refresh_token.edn"
const ID_TOKEN_FILE = "id_token.edn"
type Login struct {
UserName string `edn:"mailaddress" json:"mailaddress"`
Password string `edn:"password" json:"password"`
}
type RefreshToken struct {
RefreshToken string `edn:"refreshToken" json:"refreshToken"`
}
type IdToken struct {
IdToken string `edn:"idToken" json:"idToken"`
}
type JSONTime int64
/*
https://kenzo0107.github.io/2020/05/19/2020-05-20-go-json-time/
*/
// String converts the unix timestamp into a string
func (t JSONTime) String() string {
tm := t.Time()
return fmt.Sprintf("\"%s\"", tm.Format("2006-01-02"))
}
// Time returns a `time.Time` representation of this value.
func (t JSONTime) Time() time.Time {
return time.Unix(int64(t), 0)
}
// UnmarshalJSON will unmarshal both string and int JSON values
func (t *JSONTime) UnmarshalJSON(buf []byte) error {
s := bytes.Trim(buf, `"`)
aa, _ := time.Parse("20060102", string(s))
*t = JSONTime(aa.Unix())
return nil
}
type Quote struct {
Code string `json:"Code"`
Close float64 `json:"Close"`
Date JSONTime `json:"Date"`
AdjustmentHigh float64 `json:"AdjustmentHigh"`
Volume float64 `json:"Volume"`
TurnoverValue float64 `json:"TurnoverValue"`
AdjustmentClose float64 `json:"AdjustmentClose"`
AdjustmentLow float64 `json:"AdjustmentLow"`
Low float64 `json:"Low"`
High float64 `json:"High"`
Open float64 `json:"Open"`
AdjustmentOpen float64 `json:"AdjustmentOpen"`
AdjustmentFactor float64 `json:"AdjustmentFactor"`
AdjustmentVolume float64 `json:"AdjustmentVolume"`
}
type DailyQuotes struct {
DailyQuotes []Quote `json:"daily_quotes"`
}
func Check(e error) {
if e != nil {
panic(e)
}
}
func sendRequest(url string, idToken string) *http.Response {
req, err := http.NewRequest(http.MethodGet, url, nil)
Check(err)
req.Header = http.Header{
"Authorization": {"Bearer " + idToken},
}
client := http.Client{}
res, _ := client.Do(req)
return res
}
/**
PRIVATE METHODS TO READ / WRITE CONFIG FILES
*/
func getConfigDir() string {
homeDir, _ := os.UserHomeDir()
configDir := homeDir + "/.config/jquants/"
os.MkdirAll(configDir, os.ModePerm)
return configDir
}
func readConfigFile(file string) []byte {
s, _ := os.ReadFile(getConfigDir() + file)
return s
}
func writeConfigFile(file string, content []byte) {
os.WriteFile(getConfigFile(file), content, 0664)
}
func getConfigFile(file string) string {
return fmt.Sprintf("%s/%s", getConfigDir(), file)
}
func GetUser() Login {
s, _ := os.ReadFile(getConfigFile("login.edn"))
var user Login
edn.Unmarshal(s, &user)
return user
}
func ReadRefreshToken() RefreshToken {
var refreshToken RefreshToken
s := readConfigFile(REFRESH_TOKEN_FILE)
edn.Unmarshal(s, &refreshToken)
return refreshToken
}
func ReadIdToken() IdToken {
var idToken IdToken
s := readConfigFile(ID_TOKEN_FILE)
edn.Unmarshal(s, &idToken)
return idToken
}
func PrepareLogin(username string, password string) {
var user = Login{username, password}
encoded, _ := edn.Marshal(&user)
writeConfigFile("login.edn", encoded)
}
func GetRefreshToken() (RefreshToken, error) {
var user = GetUser()
url := fmt.Sprintf("%s/token/auth_user", BASE_URL)
// fmt.Printf("%s", url)
data, err := json.Marshal(user)
// https://golang.cafe/blog/golang-convert-byte-slice-to-io-reader.html
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
// fmt.Println(req)
client := http.Client{}
res, err := client.Do(req)
var rt RefreshToken
json.NewDecoder(res.Body).Decode(&rt)
encoded, err := edn.Marshal(&rt)
writeConfigFile(REFRESH_TOKEN_FILE, encoded)
return rt, err
}
func GetIdToken() (IdToken, error) {
var token = ReadRefreshToken()
url := fmt.Sprintf("%s/token/auth_refresh?refreshtoken=%s", BASE_URL, token.RefreshToken)
req, err := http.NewRequest(http.MethodPost, url, nil)
client := http.Client{}
res, err := client.Do(req)
var rt IdToken
json.NewDecoder(res.Body).Decode(&rt)
encoded, err := edn.Marshal(&rt)
writeConfigFile(ID_TOKEN_FILE, encoded)
return rt, err
}
func Daily(code string, date string, from string, to string) DailyQuotes {
idtoken := ReadIdToken()
baseUrl := fmt.Sprintf("%s/prices/daily_quotes?code=%s", BASE_URL, code)
var url string
if from != "" && to != "" {
url = fmt.Sprintf("%s&from=%s&to=%s", baseUrl, from, to)
} else {
url = fmt.Sprintf("%s&date=%s", baseUrl, date)
}
res := sendRequest(url, idtoken.IdToken)
var quotes DailyQuotes
err_ := json.NewDecoder(res.Body).Decode("es)
Check(err_)
return quotes
}