forked from Akagi201/cryptotrader
/
okcoin.go
94 lines (78 loc) · 1.83 KB
/
okcoin.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
// Package okcoin okcoin rest api package
package okcoin
import (
"io/ioutil"
"net/http"
"strconv"
"github.com/Akagi201/cryptotrader/model"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
const (
API = "https://www.okcoin.com/api/v1/"
)
// OkCoin API data
type OkCoin struct {
AccessKey string
SecretKey string
}
// New create new OkCoin API data
func New(accessKey string, secretKey string) *OkCoin {
return &OkCoin{
AccessKey: accessKey,
SecretKey: secretKey,
}
}
// GetTicker 行情
func (oc *OkCoin) GetTicker(base string, quote string) (*model.Ticker, error) {
url := API + "ticker.do" + "?symbol=" + quote + "_" + base
log.Debugf("Request url: %v", url)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
log.Debugf("Response body: %v", string(body))
buyRes := gjson.GetBytes(body, "ticker.buy").String()
buy, err := strconv.ParseFloat(buyRes, 64)
if err != nil {
return nil, err
}
sellRes := gjson.GetBytes(body, "ticker.sell").String()
sell, err := strconv.ParseFloat(sellRes, 64)
if err != nil {
return nil, err
}
lastRes := gjson.GetBytes(body, "ticker.last").String()
last, err := strconv.ParseFloat(lastRes, 64)
if err != nil {
return nil, err
}
lowRes := gjson.GetBytes(body, "ticker.low").String()
low, err := strconv.ParseFloat(lowRes, 64)
if err != nil {
return nil, err
}
highRes := gjson.GetBytes(body, "ticker.high").String()
high, err := strconv.ParseFloat(highRes, 64)
if err != nil {
return nil, err
}
volRes := gjson.GetBytes(body, "ticker.vol").String()
vol, err := strconv.ParseFloat(volRes, 64)
if err != nil {
return nil, err
}
return &model.Ticker{
Buy: buy,
Sell: sell,
Last: last,
Low: low,
High: high,
Vol: vol,
}, nil
}