-
Notifications
You must be signed in to change notification settings - Fork 0
/
getters.go
394 lines (364 loc) · 8.53 KB
/
getters.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
)
type AskBid struct {
Ask float64
Bid float64
}
type TickerGetter func(string) (*AskBid, error)
func getHTTPResponseBodyFromUrl(url string) ([]byte, error) {
if debug {
log.Println("getHTTPResponseBodyFromUrl", url)
}
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("http.Get: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadAll: %v", err)
}
if debug {
log.Println("BODY:", string(body))
}
return body, nil
}
type CoinmateTicker struct {
Data struct {
Ask float64 `json:"ask"`
Bid float64 `json:"bid"`
} `json:"data"`
}
func CoinmateGetter(ticker string) (*AskBid, error) {
url := "https://coinmate.io/api/ticker?currencyPair=" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData CoinmateTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
return &AskBid{tickerData.Data.Ask, tickerData.Data.Bid}, nil
}
type CoinbaseTicker struct {
Price string `json:"price"`
Ask string `json:"ask"`
Bid string `json:"bid"`
Volume string `json:"volume"`
Size string `json:"size"`
}
// ticker might be BTC-USD{T}
func CoinbaseGetter(ticker string) (*AskBid, error) {
url := "https://api.pro.coinbase.com/products/" + ticker + "/ticker"
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData CoinbaseTicker
err = json.Unmarshal(body, &tickerData)
//fmt.Printf("Coinbase %s %#v\n", ticker, tickerData)
if err != nil {
return nil, err
}
a := tickerData.Ask
b := tickerData.Bid
af, err := strconv.ParseFloat(a, 64)
if err != nil {
return nil, err
}
bf, err := strconv.ParseFloat(b, 64)
if err != nil {
return nil, err
}
return &AskBid{af, bf}, nil
}
func GeminiGetter(ticker string) (*AskBid, error) {
url := "https://api.gemini.com/v1/pubticker/" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData CoinbaseTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
a := tickerData.Ask
b := tickerData.Bid
af, err := strconv.ParseFloat(a, 64)
if err != nil {
return nil, err
}
bf, err := strconv.ParseFloat(b, 64)
if err != nil {
return nil, err
}
return &AskBid{af, bf}, nil
}
type BybitTicker struct {
Reusult struct {
List []struct {
Ask string `json:"ask1Price"`
Bid string `json:"bid1Price"`
} `json:"list"`
} `json:"result"`
}
func BybitGetter(ticker string) (*AskBid, error) {
url := "https://api-testnet.bybit.com/v5/market/tickers?category=linear&symbol=" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData BybitTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
a := tickerData.Reusult.List[0].Ask
b := tickerData.Reusult.List[0].Bid
af, err := strconv.ParseFloat(a, 64)
if err != nil {
return nil, err
}
bf, err := strconv.ParseFloat(b, 64)
if err != nil {
return nil, err
}
return &AskBid{af, bf}, nil
}
type BitstampTicker struct {
Ask string `json:"ask"`
Bid string `json:"bid"`
}
func BitstampGetter(ticker string) (*AskBid, error) {
url := "https://www.bitstamp.net/api/v2/ticker/" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
//fmt.Printf("Bitstamp %s %s\n", ticker, string(body))
//fmt.Println(url)
var tickerData BitstampTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
a := tickerData.Ask
b := tickerData.Bid
af, err := strconv.ParseFloat(a, 64)
if err != nil {
return nil, err
}
bf, err := strconv.ParseFloat(b, 64)
if err != nil {
return nil, err
}
return &AskBid{af, bf}, nil
}
type KrakenTicker struct {
Result map[string]struct {
A []string `json:"a"`
B []string `json:"b"`
} `json:"result"`
}
func KrakenGetter(ticker string) (*AskBid, error) {
url := "https://api.kraken.com/0/public/Ticker?pair=" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData KrakenTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
a := tickerData.Result[ticker].A[0]
b := tickerData.Result[ticker].B[0]
af, err := strconv.ParseFloat(a, 64)
if err != nil {
return nil, err
}
bf, err := strconv.ParseFloat(b, 64)
if err != nil {
return nil, err
}
return &AskBid{af, bf}, nil
}
type HuobiTicker struct {
Tick struct {
Bid []float64 `json:"bid"`
Ask []float64 `json:"ask"`
} `json:"tick"`
}
func HuobiGetter(ticker string) (*AskBid, error) {
url := "https://api.huobi.pro/market/detail/merged?symbol=" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData HuobiTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
//fmt.Printf("Huobi %s %#v\n", ticker, tickerData)
a := tickerData.Tick.Ask[0]
b := tickerData.Tick.Bid[0]
return &AskBid{a, b}, nil
}
type BinanceTicker struct {
Ask string `json:"askPrice"`
Bid string `json:"bidPrice"`
}
func BinanceGetter(ticker string) (*AskBid, error) {
url := "https://api.binance.com/api/v3/ticker/bookTicker?symbol=" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
//fmt.Println(string(body))
var tickerData BinanceTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
a := tickerData.Ask
b := tickerData.Bid
af, err := strconv.ParseFloat(a, 64)
if err != nil {
return nil, err
}
bf, err := strconv.ParseFloat(b, 64)
if err != nil {
return nil, err
}
return &AskBid{af, bf}, nil
}
type GateIOTicker struct {
Bid string `json:"highest_bid"`
Ask string `json:"lowest_ask"`
}
func GateIOGetter(ticker string) (*AskBid, error) {
url := "https://api.gateio.ws/api/v4/spot/tickers?currency_pair=" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData []GateIOTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
a := tickerData[0].Ask
b := tickerData[0].Bid
af, err := strconv.ParseFloat(a, 64)
if err != nil {
return nil, err
}
bf, err := strconv.ParseFloat(b, 64)
if err != nil {
return nil, err
}
return &AskBid{af, bf}, nil
}
type BitfinexTicker []float64
func BitfinexGetter(ticker string) (*AskBid, error) {
url := "https://api-pub.bitfinex.com/v2/ticker/t" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData BitfinexTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
a := tickerData[2]
b := tickerData[0]
return &AskBid{a, b}, nil
}
type KUCoinTicker struct {
Data struct {
Price string `json:"price"`
Bid string `json:"bestBid"`
Ask string `json:"bestAsk"`
} `json:"data"`
}
func KUCoinGetter(ticker string) (*AskBid, error) {
url := "https://api.kucoin.com/api/v1/market/orderbook/level1?symbol=" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData KUCoinTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
a := tickerData.Data.Ask
b := tickerData.Data.Bid
af, err := strconv.ParseFloat(a, 64)
if err != nil {
return nil, err
}
bf, err := strconv.ParseFloat(b, 64)
if err != nil {
return nil, err
}
return &AskBid{af, bf}, nil
}
type BitflyerTicker struct {
Ask float64 `json:"best_ask"`
Bid float64 `json:"best_bid"`
}
func BitflyerGetter(ticker string) (*AskBid, error) {
url := "https://api.bitflyer.com/v1/ticker?product_code=" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData BitflyerTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
return &AskBid{tickerData.Ask, tickerData.Bid}, nil
}
type OkxTicker struct {
Data []struct {
Ask string `json:"askPx"`
Bid string `json:"bidPx"`
} `json:"data"`
}
func OkxGetter(ticker string) (*AskBid, error) {
url := "https://www.okx.com/api/v5/market/ticker?instId=" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return nil, err
}
var tickerData OkxTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return nil, err
}
a := tickerData.Data[0].Ask
b := tickerData.Data[0].Bid
af, err := strconv.ParseFloat(a, 64)
if err != nil {
return nil, err
}
bf, err := strconv.ParseFloat(b, 64)
if err != nil {
return nil, err
}
return &AskBid{af, bf}, nil
}