This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitfinex_to_influx.go
99 lines (80 loc) · 1.9 KB
/
bitfinex_to_influx.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
package main
import (
"log"
"math/rand"
"os"
"strings"
"time"
"github.com/bitfinexcom/bitfinex-api-go/v1"
"github.com/influxdata/influxdb/client/v2"
)
func writePoints(fields map[string]interface{}) {
// Conntect to Influx
InfluxHost := os.Getenv("INFLUX_HOST")
InfluxDB := os.Getenv("INFLUX_DB")
InfluxUser := os.Getenv("INFLUX_USER")
InfluxPass := os.Getenv("INFLUX_Pass")
clnt, err := client.NewHTTPClient(client.HTTPConfig{
Addr: InfluxHost,
Username: InfluxUser,
Password: InfluxPass,
})
if err != nil {
log.Fatal(err)
}
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: InfluxDB,
Precision: "us",
})
if err != nil {
log.Fatal(err)
}
rand.Seed(time.Now().UnixNano())
tags := make(map[string]string)
pt, err := client.NewPoint(
"crypto",
tags,
fields,
time.Now(),
)
if err != nil {
log.Fatal(err)
}
bp.AddPoint(pt)
if err := clnt.Write(bp); err != nil {
log.Fatal(err)
}
}
func getData(element bitfinex.WalletBalance, client *bitfinex.Client) map[string]interface{} {
change := element.Currency + "usd"
Ticker, err := client.Ticker.Get(change)
if err != nil {
log.Fatal(err)
return make(map[string]interface{})
} else {
returnMap := make(map[string]interface{})
returnMap["Curency"] = strings.ToUpper(element.Currency)
returnMap["Volume"] = Ticker.Volume
returnMap["Ask"] = Ticker.Ask
returnMap["Bid"] = Ticker.Bid
returnMap["High"] = Ticker.High
returnMap["LastPrice"] = Ticker.LastPrice
returnMap["Low"] = Ticker.Low
returnMap["Mid"] = Ticker.Mid
returnMap["Balance"] = element.Amount
return returnMap
}
}
func main() {
key := os.Getenv("BFX_API_KEY")
secret := os.Getenv("BFX_API_SECRET")
client := bitfinex.NewClient().Auth(key, secret)
balance, err := client.Balances.All()
if err != nil {
log.Fatal(err)
} else {
for _, element := range balance {
writePoints(getData(element, client))
}
}
}