forked from wtfutil/wtf
/
widget.go
139 lines (107 loc) · 3.1 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
package toplist
import (
"encoding/json"
"fmt"
"net/http"
"os"
"sync"
"time"
"github.com/olebedev/config"
)
// Config is a pointer to the global config object
var Config *config.Config
var baseURL = "https://min-api.cryptocompare.com/data/top/exchanges"
type textColors struct {
from struct {
name string
displayName string
}
to struct {
name string
field string
value string
}
}
// Widget Toplist Widget
type Widget struct {
Result string
RefreshInterval int
list *cList
colors textColors
}
// NewWidget Make new toplist widget
func NewWidget() *Widget {
widget := Widget{}
widget.list = &cList{}
widget.setList()
widget.config()
return &widget
}
func (widget *Widget) setList() {
currenciesMap, _ := Config.Map("wtf.mods.cryptolive.top")
for fromCurrency := range currenciesMap {
displayName := Config.UString("wtf.mods.cryptolive.top."+fromCurrency+".displayName", "")
limit := Config.UInt("wtf.mods.cryptolive.top."+fromCurrency+".limit", 1)
widget.list.addItem(fromCurrency, displayName, limit, makeToList(fromCurrency, limit))
}
}
func makeToList(fCurrencyName string, limit int) (list []*tCurrency) {
toList, _ := Config.List("wtf.mods.cryptolive.top." + fCurrencyName + ".to")
for _, toCurrency := range toList {
list = append(list, &tCurrency{
name: toCurrency.(string),
info: make([]tInfo, limit),
})
}
return
}
func (widget *Widget) config() {
// set colors
widget.colors.from.name = Config.UString("wtf.mods.cryptolive.colors.top.from.name", "coral")
widget.colors.from.displayName = Config.UString("wtf.mods.cryptolive.colors.top.from.displayName", "grey")
widget.colors.to.name = Config.UString("wtf.mods.cryptolive.colors.top.to.name", "red")
widget.colors.to.field = Config.UString("wtf.mods.cryptolive.colors.top.to.field", "white")
widget.colors.to.value = Config.UString("wtf.mods.cryptolive.colors.top.to.value", "value")
}
/* -------------------- Exported Functions -------------------- */
// Refresh & update after interval time
func (widget *Widget) Refresh(wg *sync.WaitGroup) {
if len(widget.list.items) == 0 {
return
}
widget.updateData()
widget.display()
wg.Done()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) updateData() {
defer func() {
recover()
}()
client := &http.Client{
Timeout: time.Duration(5 * time.Second),
}
for _, fromCurrency := range widget.list.items {
for _, toCurrency := range fromCurrency.to {
request := makeRequest(fromCurrency.name, toCurrency.name, fromCurrency.limit)
response, _ := client.Do(request)
var jsonResponse responseInterface
err := json.NewDecoder(response.Body).Decode(&jsonResponse)
if err != nil {
os.Exit(1)
}
for idx, info := range jsonResponse.Data {
toCurrency.info[idx] = tInfo{
exchange: info.Exchange,
volume24h: info.Volume24h,
volume24hTo: info.Volume24hTo,
}
}
}
}
}
func makeRequest(fsym, tsym string, limit int) *http.Request {
url := fmt.Sprintf("%s?fsym=%s&tsym=%s&limit=%d", baseURL, fsym, tsym, limit)
request, _ := http.NewRequest("GET", url, nil)
return request
}