forked from metacoin/flo-market-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
432 lines (374 loc) · 9.71 KB
/
parse.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
// BITTREX
type Bittrex_BTC_FLO struct {
Message string `json:"message"`
Result []struct {
Ask float64 `json:"Ask"`
BaseVolume float64 `json:"BaseVolume"`
Bid float64 `json:"Bid"`
Created string `json:"Created"`
High float64 `json:"High"`
Last float64 `json:"Last"`
Low float64 `json:"Low"`
MarketName string `json:"MarketName"`
OpenBuyOrders int64 `json:"OpenBuyOrders"`
OpenSellOrders int64 `json:"OpenSellOrders"`
PrevDay float64 `json:"PrevDay"`
TimeStamp string `json:"TimeStamp"`
Volume float64 `json:"Volume"`
} `json:"result"`
Success bool `json:"success"`
}
func get_bittrex_btc_flo_last(url string) float64 {
resp, err := http.Get(url)
if err != nil {
fmt.Println("\nError getting Bittrex market data.")
fmt.Printf("%v\n", err)
return 0
} else {
return parse_bittrex_btc_flo_last(resp)
}
return 0.0
}
// get a http response from bittrex, parse it out
func parse_bittrex_btc_flo_last(resp *http.Response) float64 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("\nError getting Bittrex market data.")
fmt.Printf("%v\n", err)
return 0
} else {
data := Bittrex_BTC_FLO{}
err := json.Unmarshal(body, &data)
// TODO: make this not suck
if err != nil {
return 0
}
if len(data.Result) > 0 {
result := data.Result[0]
return result.Last
} else {
fmt.Printf("\nAPI timeout (bittrex)...\n")
}
}
return 0.0
}
func get_bittrex_btc_flo_volu(url string) float64 {
resp, err := http.Get(url)
if err != nil {
fmt.Println("\nError getting Bittrex market volume.")
fmt.Printf("%v\n", err)
return 0
} else {
return parse_bittrex_btc_flo_volu(resp)
}
return 0.0
}
// get a http response from bittrex, parse it out
func parse_bittrex_btc_flo_volu(resp *http.Response) float64 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("\nError getting Bittrex market volume.")
fmt.Printf("%v\n", err)
return 0
} else {
data := Bittrex_BTC_FLO{}
json.Unmarshal(body, &data)
if len(data.Result) > 0 {
result := data.Result[0]
return result.Volume
} else {
fmt.Printf("\nAPI timeout (bittrex)...\n")
}
}
return 0.0
}
// POLONIEX
type Poloniex_BTC_FLO_last struct {
Amount float64 `json:"amount,string"`
Date string `json:"date"`
GlobalTradeID int64 `json:"globalTradeID"`
Rate float64 `json:"rate,string"`
Total float64 `json:"total,string"`
TradeID int64 `json:"tradeID"`
Type string `json:"type"`
}
func get_poloniex_btc_flo_last(url string) float64 {
resp, err := http.Get(url)
if err != nil {
fmt.Println("\nError getting Poloniex FLO exchange rate.")
fmt.Printf("%v\n", err)
return 0
} else {
return parse_poloniex_btc_flo_last(resp)
}
return 0.0
}
func parse_poloniex_btc_flo_last(resp *http.Response) float64 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("\nError getting Poloniex market data.")
fmt.Printf("%v\n", err)
return 0
} else {
var alldata []interface{}
json.Unmarshal(body, &alldata)
if len(alldata) > 0 {
something := alldata[0].(map[string]interface{})
for k, v := range something {
if k == "rate" {
if v == nil {
return 0
}
rv, err := strconv.ParseFloat(v.(string), 64)
if err != nil {
fmt.Println("\nCan't parse Polonirex BTC/FLO JSON.")
fmt.Printf("%v\n", err)
return 0
} else {
return rv
}
}
}
} else {
fmt.Printf("\nAPI timeout (poloniex)\n")
}
}
return 0.0
}
type Poloniex_BTC_FLO_volu struct {
BTCFLO struct {
BTC float64 `json:"BTC,string"`
FLO float64 `json:"FLO,string"`
} `json:"BTC_FLO"`
}
func get_poloniex_btc_flo_volu(url string) float64 {
resp, err := http.Get(url)
if err != nil {
fmt.Println("\nError getting Poloniex market volume.")
fmt.Printf("%v\n", err)
return 0
} else {
return parse_poloniex_btc_flo_volu(resp)
}
return 0.0
}
func parse_poloniex_btc_flo_volu(resp *http.Response) float64 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("\nError getting Poloniex market volume.")
fmt.Printf("%v\n", err)
return 0
} else {
var alldata interface{}
json.Unmarshal(body, &alldata)
// TODO: make this not suck
if alldata == nil {
return 0
}
datamap := alldata.(map[string]interface{})
for k, v := range datamap {
if k == "BTC_FLO" {
vv := v.(map[string]interface{})
for kk, vvv := range vv {
if kk == "FLO" {
if vvv == nil {
return 0
}
rv, err := strconv.ParseFloat(vvv.(string), 64)
if err != nil {
fmt.Println("\nError parsing Poloniex volume JSON.")
fmt.Printf("%v\n", err)
return 0
} else {
return rv
}
}
}
}
}
}
return 0.0
}
// CRYPTSY
func get_cryptsy_btc_ltc_last(url string) float64 {
resp, err := http.Get(url)
if err != nil {
fmt.Println("\nError getting Cryptsy BTC/LTC exchange rate.")
fmt.Printf("%v\n", err)
return 0
} else {
return parse_cryptsy_btc_ltc_last(resp)
}
return 0.0
}
func parse_cryptsy_btc_ltc_last(resp *http.Response) float64 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("\nError getting Cryptsy market data.")
fmt.Printf("%v\n", err)
return 0
} else {
var alldata interface{}
json.Unmarshal(body, &alldata)
// TODO: make this not suck
if alldata == nil {
return 0
}
something := alldata.(map[string]interface{})
//fmt.Printf("somethingelse: %v\n", something)
for k, v := range something {
if k == "return" {
s := v.(map[string]interface{})
for k2, v2 := range s {
if k2 == "markets" {
s2 := v2.(map[string]interface{})
for k3, v3 := range s2 {
if k3 == "LTC" {
s3 := v3.(map[string]interface{})
//fmt.Printf("\ns3: %v\n", s3)
for k4, v4 := range s3 {
if k4 == "lasttradeprice" {
if v4 == nil {
return 0.0
}
rv, err := strconv.ParseFloat(v4.(string), 64)
if err != nil {
return 0.0
} else {
return rv
}
}
}
}
}
}
}
}
}
}
return 0.0
}
func get_cryptsy_ltc_flo_last(url string) (float64, float64) {
resp, err := http.Get(url)
if err != nil {
fmt.Println("\nError getting Cryptsy FLO/LTC exchange rate.")
fmt.Printf("%v\n", err)
return 0, 0
} else {
//fmt.Printf("getting cryptsy ltc/flo data from %v\n", url)
return parse_cryptsy_ltc_flo_last(resp)
}
return 0.0, 0.0
}
// this actually returns last trade and volume
func parse_cryptsy_ltc_flo_last(resp *http.Response) (float64, float64) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("\nError getting Cryptsy market volume.")
fmt.Printf("%v\n", err)
return 0, 0
} else {
var alldata interface{}
json.Unmarshal(body, &alldata)
// TODO: make this not suck
if alldata == nil {
return 0, 0
}
something := alldata.(map[string]interface{})
//fmt.Printf("somethingelse: %v\n", something)
for k, v := range something {
//fmt.Printf("k, v: %v, %v\n", k, v)
if k == "return" {
s := v.(map[string]interface{})
for k2, v2 := range s {
if k2 == "markets" {
s2 := v2.(map[string]interface{})
for k3, v3 := range s2 {
if k3 == "FLO" {
s3 := v3.(map[string]interface{})
//fmt.Printf("\ns3: %v\n", s3)
var volu float64 = 0.0
var last float64 = 0.0
for k4, v4 := range s3 {
if k4 == "lasttradeprice" {
var err error
if v4 == nil {
return 0.0, 0.0
}
last, err = strconv.ParseFloat(v4.(string), 64)
if err != nil {
// error
fmt.Printf("error parsing cryptsy lasttradeprice: %v\n", err)
} else {
// last
}
}
if k4 == "volume" {
if v4 == nil {
return 0.0, 0.0
}
volu, err = strconv.ParseFloat(v4.(string), 64)
if err != nil {
// error
fmt.Printf("error parsing cryptsy volume: %v\n", err)
} else {
// volume
}
}
}
//fmt.Printf("last: %v, volu: %v\n", last, volu)
return last, volu
}
}
}
}
}
}
}
return 0.0, 0.0
}
type Bitcoinaverage struct {
_24hAvg float64 `json:"24h_avg"`
Ask float64 `json:"ask"`
Bid float64 `json:"bid"`
Last float64 `json:"last"`
Timestamp string `json:"timestamp"`
VolumeBtc float64 `json:"volume_btc"`
VolumePercent float64 `json:"volume_percent"`
}
func get_bitcoinaverage_usd(url string) float64 {
resp, err := http.Get(url)
if err != nil {
fmt.Println("\nError getting Bitcoinaverage market data.")
fmt.Printf("%v\n", err)
return 0
} else {
return parse_bitcoinaverage_usd(resp)
}
return 0.0
}
func parse_bitcoinaverage_usd(resp *http.Response) float64 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("\nError getting Bitcoinaverage USD exchange rate.")
fmt.Printf("%v\n", err)
return 0
} else {
alldata := Bitcoinaverage{}
err := json.Unmarshal(body, &alldata)
// TODO: make this not suck
if err != nil {
return 0
}
return alldata.Last
}
return 0.0
}