-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (123 loc) · 3.33 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/luno/luno-go"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
type BalanceData struct {
Timestamp string
Asset string
AccountID string
Balance string
Reserved string
}
func main() {
// Initialize the Luno client
lunoClient := luno.NewClient()
lunoClient.SetAuth(os.Getenv("LUNO_API_KEY"), os.Getenv("LUNO_API_SECRET"))
// Create context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Get balances from Luno
balances, err := getLunoBalances(ctx, lunoClient)
if err != nil {
log.Fatal("Error getting Luno balances:", err)
}
// Save to Google Sheets
if err := saveToGoogleSheets(ctx, balances); err != nil {
log.Fatal("Error saving to Google Sheets:", err)
}
log.Println("Successfully saved balances to Google Sheets")
}
func getLunoBalances(ctx context.Context, client *luno.Client) ([]BalanceData, error) {
request := &luno.GetBalancesRequest{}
response, err := client.GetBalances(ctx, request)
if err != nil {
return nil, err
}
var balances []BalanceData
timestamp := time.Now().Format("2006-01-02 15:04:05")
for _, balance := range response.Balance {
balances = append(balances, BalanceData{
Timestamp: timestamp,
Asset: balance.Asset,
AccountID: balance.AccountId,
Balance: balance.Balance.String(),
Reserved: balance.Reserved.String(),
})
log.Printf("Asset: %s, Account ID: %s, Balance: %s, Reserved: %s\n",
balance.Asset,
balance.AccountId,
balance.Balance.String(),
balance.Reserved.String(),
)
}
return balances, nil
}
func saveToGoogleSheets(ctx context.Context, balances []BalanceData) error {
// Read credentials file
credBytes, err := os.ReadFile(os.Getenv("JSON_CREDENTIALS"))
if err != nil {
return fmt.Errorf("failed to read credentials file: %v", err)
}
// Create JWT config
config, err := google.JWTConfigFromJSON(credBytes, sheets.SpreadsheetsScope)
if err != nil {
return fmt.Errorf("failed to create JWT config: %v", err)
}
// Create client
client := config.Client(ctx)
// Create sheets service
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("failed to create sheets service: %v", err)
}
spreadsheetId := os.Getenv("SPREADSHEET_ID")
range_ := "<spreadsheetName!A:E>"
// First, get existing values to check if headers exist
existing, err := srv.Spreadsheets.Values.Get(spreadsheetId, range_).Do()
if err != nil {
return fmt.Errorf("failed to get existing values: %v", err)
}
// Prepare data
var values [][]interface{}
// Only add headers if the sheet is empty
if len(existing.Values) == 0 {
values = append(values, []interface{}{
"Timestamp",
"Asset",
"Account ID",
"Balance",
"Reserved",
})
}
// Add balance data
for _, balance := range balances {
values = append(values, []interface{}{
balance.Timestamp,
balance.Asset,
balance.AccountID,
balance.Balance,
balance.Reserved,
})
}
valueRange := &sheets.ValueRange{
Values: values,
}
// Append data to sheet
_, err = srv.Spreadsheets.Values.Append(
spreadsheetId,
range_,
valueRange,
).ValueInputOption("USER_ENTERED").Do()
if err != nil {
return fmt.Errorf("failed to append data: %v", err)
}
return nil
}