forked from wtfutil/wtf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.go
169 lines (138 loc) · 4.21 KB
/
widget.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
package bittrex
import (
"encoding/json"
"fmt"
"time"
"net/http"
"github.com/senorprogrammer/wtf/wtf"
)
type TextColors struct {
base struct {
name string
displayName string
}
market struct {
name string
field string
value string
}
}
var ok = true
var errorText = ""
var baseURL = "https://bittrex.com/api/v1.1/public/getmarketsummary"
// Widget define wtf widget to register widget later
type Widget struct {
wtf.TextWidget
summaryList
TextColors
}
// NewWidget Make new instance of widget
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" Bittrex ", "bittrex", false),
summaryList: summaryList{},
}
ok = true
errorText = ""
widget.config()
widget.setSummaryList()
return &widget
}
func (widget *Widget) config() {
widget.TextColors.base.name = wtf.Config.UString("wtf.mods.bittrex.colors.base.name", "red")
widget.TextColors.base.displayName = wtf.Config.UString("wtf.mods.bittrex.colors.base.displayName", "grey")
widget.TextColors.market.name = wtf.Config.UString("wtf.mods.bittrex.colors.market.name", "red")
widget.TextColors.market.field = wtf.Config.UString("wtf.mods.bittrex.colors.market.field", "coral")
widget.TextColors.market.value = wtf.Config.UString("wtf.mods.bittrex.colors.market.value", "white")
}
func (widget *Widget) setSummaryList() {
sCurrencies, _ := wtf.Config.Map("wtf.mods.bittrex.summary")
for baseCurrencyName := range sCurrencies {
displayName, _ := wtf.Config.String("wtf.mods.bittrex.summary." + baseCurrencyName + ".displayName")
mCurrencyList := makeSummaryMarketList(baseCurrencyName)
widget.summaryList.addSummaryItem(baseCurrencyName, displayName, mCurrencyList)
}
}
func makeSummaryMarketList(currencyName string) []*mCurrency {
mCurrencyList := []*mCurrency{}
configMarketList, _ := wtf.Config.List("wtf.mods.bittrex.summary." + currencyName + ".market")
for _, mCurrencyName := range configMarketList {
mCurrencyList = append(mCurrencyList, makeMarketCurrency(mCurrencyName.(string)))
}
return mCurrencyList
}
func makeMarketCurrency(name string) *mCurrency {
return &mCurrency{
name: name,
summaryInfo: summaryInfo{
High: "",
Low: "",
Volume: "",
Last: "",
OpenBuyOrders: "",
OpenSellOrders: "",
},
}
}
/* -------------------- Exported Functions -------------------- */
// Refresh & update after interval time
func (widget *Widget) Refresh() {
widget.updateSummary()
widget.UpdateRefreshedAt()
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) updateSummary() {
// In case if anything bad happened!
defer func() {
recover()
}()
client := &http.Client{
Timeout: time.Duration(5 * time.Second),
}
for _, baseCurrency := range widget.summaryList.items {
for _, mCurrency := range baseCurrency.markets {
request := makeRequest(baseCurrency.name, mCurrency.name)
response, err := client.Do(request)
if err != nil {
ok = false
errorText = "Please Check Your Internet Connection!"
break
} else {
ok = true
errorText = ""
}
if response.StatusCode != http.StatusOK {
errorText = response.Status
ok = false
break
} else {
ok = true
errorText = ""
}
defer response.Body.Close()
jsonResponse := summaryResponse{}
decoder := json.NewDecoder(response.Body)
decoder.Decode(&jsonResponse)
if !jsonResponse.Success {
ok = false
errorText = fmt.Sprintf("%s-%s: %s", baseCurrency.name, mCurrency.name, jsonResponse.Message)
break
}
ok = true
errorText = ""
mCurrency.Last = fmt.Sprintf("%f", jsonResponse.Result[0].Last)
mCurrency.High = fmt.Sprintf("%f", jsonResponse.Result[0].High)
mCurrency.Low = fmt.Sprintf("%f", jsonResponse.Result[0].Low)
mCurrency.Volume = fmt.Sprintf("%f", jsonResponse.Result[0].Volume)
mCurrency.OpenBuyOrders = fmt.Sprintf("%d", jsonResponse.Result[0].OpenBuyOrders)
mCurrency.OpenSellOrders = fmt.Sprintf("%d", jsonResponse.Result[0].OpenSellOrders)
}
}
widget.display()
}
func makeRequest(baseName, marketName string) *http.Request {
url := fmt.Sprintf("%s?market=%s-%s", baseURL, baseName, marketName)
request, _ := http.NewRequest("GET", url, nil)
return request
}