From 10b6431be10f5e5b9e3eb462ea0eff7ee4846e27 Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Wed, 6 Jun 2018 02:53:15 +0430 Subject: [PATCH 1/5] cryptolist supports toplist --- cryptoexchanges/cryptolive/cryptolive.go | 27 --- cryptoexchanges/cryptolive/price/price.go | 28 +++ cryptoexchanges/cryptolive/price/widget.go | 166 ++++++++++++++++++ cryptoexchanges/cryptolive/toplist/display.go | 23 +++ cryptoexchanges/cryptolive/toplist/toplist.go | 41 +++++ cryptoexchanges/cryptolive/toplist/widget.go | 117 ++++++++++++ cryptoexchanges/cryptolive/widget.go | 145 ++------------- 7 files changed, 391 insertions(+), 156 deletions(-) create mode 100644 cryptoexchanges/cryptolive/price/price.go create mode 100644 cryptoexchanges/cryptolive/price/widget.go create mode 100644 cryptoexchanges/cryptolive/toplist/display.go create mode 100644 cryptoexchanges/cryptolive/toplist/toplist.go create mode 100644 cryptoexchanges/cryptolive/toplist/widget.go diff --git a/cryptoexchanges/cryptolive/cryptolive.go b/cryptoexchanges/cryptolive/cryptolive.go index b9e641a0..f92d7dbf 100644 --- a/cryptoexchanges/cryptolive/cryptolive.go +++ b/cryptoexchanges/cryptolive/cryptolive.go @@ -1,28 +1 @@ package cryptolive - -type list struct { - items []*fromCurrency -} - -type fromCurrency struct { - name string - displayName string - to []*toCurrency -} - -type toCurrency struct { - name string - price float32 -} - -type cResponse map[string]float32 - -/* -------------------- Unexported Functions -------------------- */ - -func (l *list) addItem(name string, displayName string, to []*toCurrency) { - l.items = append(l.items, &fromCurrency{ - name: name, - displayName: displayName, - to: to, - }) -} diff --git a/cryptoexchanges/cryptolive/price/price.go b/cryptoexchanges/cryptolive/price/price.go new file mode 100644 index 00000000..76464d9a --- /dev/null +++ b/cryptoexchanges/cryptolive/price/price.go @@ -0,0 +1,28 @@ +package price + +type list struct { + items []*fromCurrency +} + +type fromCurrency struct { + name string + displayName string + to []*toCurrency +} + +type toCurrency struct { + name string + price float32 +} + +type cResponse map[string]float32 + +/* -------------------- Unexported Functions -------------------- */ + +func (l *list) addItem(name string, displayName string, to []*toCurrency) { + l.items = append(l.items, &fromCurrency{ + name: name, + displayName: displayName, + to: to, + }) +} diff --git a/cryptoexchanges/cryptolive/price/widget.go b/cryptoexchanges/cryptolive/price/widget.go new file mode 100644 index 00000000..26d8ef9d --- /dev/null +++ b/cryptoexchanges/cryptolive/price/widget.go @@ -0,0 +1,166 @@ +package price + +import ( + "encoding/json" + "fmt" + "net/http" + "time" + + "github.com/olebedev/config" +) + +// Config is a pointer to the global config object +var Config *config.Config + +var started = false +var baseURL = "https://min-api.cryptocompare.com/data/price" +var ok = true + +// Widget define wtf widget to register widget later +type Widget struct { + *list + + Result string + + RefreshInterval int +} + +// NewWidget Make new instance of widget +func NewWidget() *Widget { + started = false + widget := Widget{} + + widget.setList() + + return &widget +} + +func (widget *Widget) setList() { + currenciesMap, _ := Config.Map("wtf.mods.cryptolive.currencies") + + widget.list = &list{} + + for currency := range currenciesMap { + displayName, _ := Config.String("wtf.mods.cryptolive.currencies." + currency + ".displayName") + toList := getToList(currency) + widget.list.addItem(currency, displayName, toList) + } + +} + +/* -------------------- Exported Functions -------------------- */ + +// Refresh & update after interval time +func (widget *Widget) Refresh() { + + if started == false { + // this code should run once + go func() { + for { + widget.updateCurrencies() + time.Sleep(time.Duration(widget.RefreshInterval) * time.Second) + } + }() + + } + + started = true + + if !ok { + widget.Result = fmt.Sprint("Please check your internet connection!") + return + } + widget.display() +} + +/* -------------------- Unexported Functions -------------------- */ + +func (widget *Widget) display() { + str := "" + var ( + fromNameColor = Config.UString("wtf.mods.cryptolive.colors.from.name", "coral") + fromDisplayNameColor = Config.UString("wtf.mods.cryptolive.colors.from.displayName", "grey") + toNameColor = Config.UString("wtf.mods.cryptolive.colors.to.name", "white") + toPriceColor = Config.UString("wtf.mods.cryptolive.colors.to.price", "green") + ) + for _, item := range widget.list.items { + str += fmt.Sprintf(" [%s]%s[%s] (%s)\n", fromNameColor, item.displayName, fromDisplayNameColor, item.name) + for _, toItem := range item.to { + str += fmt.Sprintf("\t[%s]%s: [%s]%f\n", toNameColor, toItem.name, toPriceColor, toItem.price) + } + str += "\n" + } + + widget.Result = fmt.Sprintf("\n%s", str) +} + +func getToList(fromName string) []*toCurrency { + toNames, _ := Config.List("wtf.mods.cryptolive.currencies." + fromName + ".to") + + var toList []*toCurrency + + for _, to := range toNames { + toList = append(toList, &toCurrency{ + name: to.(string), + price: 0, + }) + } + + return toList +} + +func (widget *Widget) updateCurrencies() { + defer func() { + recover() + }() + for _, fromCurrency := range widget.list.items { + + var ( + client http.Client + jsonResponse cResponse + ) + + client = http.Client{ + Timeout: time.Duration(5 * time.Second), + } + + request := makeRequest(fromCurrency) + response, err := client.Do(request) + + if err != nil { + ok = false + } else { + ok = true + } + + defer response.Body.Close() + + _ = json.NewDecoder(response.Body).Decode(&jsonResponse) + + setPrices(&jsonResponse, fromCurrency) + } + + widget.display() +} + +func makeRequest(currency *fromCurrency) *http.Request { + fsym := currency.name + tsyms := "" + for _, to := range currency.to { + tsyms += fmt.Sprintf("%s,", to.name) + } + + url := fmt.Sprintf("%s?fsym=%s&tsyms=%s", baseURL, fsym, tsyms) + request, err := http.NewRequest("GET", url, nil) + + if err != nil { + } + + return request +} + +func setPrices(response *cResponse, currencry *fromCurrency) { + for idx, toCurrency := range currencry.to { + currencry.to[idx].price = (*response)[toCurrency.name] + } +} diff --git a/cryptoexchanges/cryptolive/toplist/display.go b/cryptoexchanges/cryptolive/toplist/display.go new file mode 100644 index 00000000..5b5f2434 --- /dev/null +++ b/cryptoexchanges/cryptolive/toplist/display.go @@ -0,0 +1,23 @@ +package toplist + +import "fmt" + +func (widget *Widget) display() { + str := "" + for _, fromCurrency := range widget.list.items { + str += fmt.Sprintf("%s (%s)\n", fromCurrency.displayName, fromCurrency.name) + for _, toCurrency := range fromCurrency.to { + str += fmt.Sprintf(" %s\n", toCurrency.name) + for _, info := range toCurrency.info { + str += makeInfoRow(info) + str += "\n\n" + } + } + } + + widget.Result = str +} + +func makeInfoRow(info tInfo) string { + return fmt.Sprintf(" Exchange: %s\n", info.exchange) + fmt.Sprintf(" Volume(24h): %f-%f", info.volume24h, info.volume24hTo) +} diff --git a/cryptoexchanges/cryptolive/toplist/toplist.go b/cryptoexchanges/cryptolive/toplist/toplist.go new file mode 100644 index 00000000..9bb48947 --- /dev/null +++ b/cryptoexchanges/cryptolive/toplist/toplist.go @@ -0,0 +1,41 @@ +package toplist + +type cList struct { + items []*fCurrency +} + +type fCurrency struct { + name, displayName string + limit int + to []*tCurrency +} + +type tCurrency struct { + name string + info []tInfo +} + +type tInfo struct { + exchange string + volume24h, volume24hTo float32 +} + +type responseInterface struct { + Response string `json:"Response"` + Data []struct { + Exchange string `json:"exchange"` + FromSymbol string `json:"fromSymbol"` + ToSymbol string `json:"toSymbol"` + Volume24h float32 `json:"volume24h"` + Volume24hTo float32 `json:"volume24hTo"` + } `json:"Data"` +} + +func (list *cList) addItem(name, displayName string, limit int, to []*tCurrency) { + list.items = append(list.items, &fCurrency{ + name: name, + displayName: displayName, + limit: limit, + to: to, + }) +} diff --git a/cryptoexchanges/cryptolive/toplist/widget.go b/cryptoexchanges/cryptolive/toplist/widget.go new file mode 100644 index 00000000..b040cee0 --- /dev/null +++ b/cryptoexchanges/cryptolive/toplist/widget.go @@ -0,0 +1,117 @@ +package toplist + +import ( + "encoding/json" + "fmt" + "net/http" + "os" + "time" + + "github.com/olebedev/config" +) + +// Config is a pointer to the global config object +var Config *config.Config +var started = false +var baseURL = "https://min-api.cryptocompare.com/data/top/exchanges" + +// Widget Toplist Widget +type Widget struct { + Result string + + RefreshInterval int + + list *cList +} + +// NewWidget Make new toplist widget +func NewWidget() *Widget { + widget := Widget{} + + started = false + widget.list = &cList{} + widget.setList() + + 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", 5) + 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 +} + +/* -------------------- Exported Functions -------------------- */ + +// Refresh & update after interval time +func (widget *Widget) Refresh() { + + if !started { + go func() { + for { + widget.updateData() + time.Sleep(time.Second * time.Duration(widget.RefreshInterval)) + } + }() + started = true + } + + widget.display() +} + +/* -------------------- Unexported Functions -------------------- */ + +func (widget *Widget) updateData() { + + 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 +} diff --git a/cryptoexchanges/cryptolive/widget.go b/cryptoexchanges/cryptolive/widget.go index e58b9435..eba98559 100644 --- a/cryptoexchanges/cryptolive/widget.go +++ b/cryptoexchanges/cryptolive/widget.go @@ -1,54 +1,41 @@ package cryptolive import ( - "encoding/json" "fmt" - "net/http" - "time" "github.com/olebedev/config" + "github.com/senorprogrammer/wtf/cryptoexchanges/cryptolive/price" + "github.com/senorprogrammer/wtf/cryptoexchanges/cryptolive/toplist" "github.com/senorprogrammer/wtf/wtf" ) // Config is a pointer to the global config object var Config *config.Config -var started = false -var baseURL = "https://min-api.cryptocompare.com/data/price" -var ok = true - // Widget define wtf widget to register widget later type Widget struct { wtf.TextWidget - - *list + priceWidget *price.Widget + toplistWidget *toplist.Widget } // NewWidget Make new instance of widget func NewWidget() *Widget { - started = false + price.Config = Config + toplist.Config = Config + widget := Widget{ - TextWidget: wtf.NewTextWidget(" CryptoLive ", "cryptolive", false), + TextWidget: wtf.NewTextWidget(" CryptoLive ", "cryptolive", false), + priceWidget: price.NewWidget(), + toplistWidget: toplist.NewWidget(), } - widget.setList() + widget.priceWidget.RefreshInterval = widget.RefreshInterval() + widget.toplistWidget.RefreshInterval = widget.RefreshInterval() return &widget } -func (widget *Widget) setList() { - currenciesMap, _ := Config.Map("wtf.mods.cryptolive.currencies") - - widget.list = &list{} - - for currency := range currenciesMap { - displayName, _ := Config.String("wtf.mods.cryptolive.currencies." + currency + ".displayName") - toList := getToList(currency) - widget.list.addItem(currency, displayName, toList) - } - -} - /* -------------------- Exported Functions -------------------- */ // Refresh & update after interval time @@ -57,28 +44,11 @@ func (widget *Widget) Refresh() { return } - if started == false { - // this code should run once - go func() { - for { - widget.updateCurrencies() - time.Sleep(time.Duration(widget.RefreshInterval()) * time.Second) - } - }() - - } - - started = true + widget.priceWidget.Refresh() + widget.toplistWidget.Refresh() widget.UpdateRefreshedAt() - widget.View.Clear() - if !ok { - widget.View.SetText( - fmt.Sprint("Please check your internet connection!"), - ) - return - } display(widget) } @@ -86,90 +56,7 @@ func (widget *Widget) Refresh() { func display(widget *Widget) { str := "" - var ( - fromNameColor = Config.UString("wtf.mods.cryptolive.colors.from.name", "coral") - fromDisplayNameColor = Config.UString("wtf.mods.cryptolive.colors.from.displayName", "grey") - toNameColor = Config.UString("wtf.mods.cryptolive.colors.to.name", "white") - toPriceColor = Config.UString("wtf.mods.cryptolive.colors.to.price", "green") - ) - for _, item := range widget.list.items { - str += fmt.Sprintf(" [%s]%s[%s] (%s)\n", fromNameColor, item.displayName, fromDisplayNameColor, item.name) - for _, toItem := range item.to { - str += fmt.Sprintf("\t[%s]%s: [%s]%f\n", toNameColor, toItem.name, toPriceColor, toItem.price) - } - str += "\n" - } - + str += widget.priceWidget.Result + str += widget.toplistWidget.Result widget.View.SetText(fmt.Sprintf("\n%s", str)) } - -func getToList(fromName string) []*toCurrency { - toNames, _ := Config.List("wtf.mods.cryptolive.currencies." + fromName + ".to") - - var toList []*toCurrency - - for _, to := range toNames { - toList = append(toList, &toCurrency{ - name: to.(string), - price: 0, - }) - } - - return toList -} - -func (widget *Widget) updateCurrencies() { - defer func() { - recover() - }() - for _, fromCurrency := range widget.list.items { - - var ( - client http.Client - jsonResponse cResponse - ) - - client = http.Client{ - Timeout: time.Duration(5 * time.Second), - } - - request := makeRequest(fromCurrency) - response, err := client.Do(request) - - if err != nil { - ok = false - } else { - ok = true - } - - defer response.Body.Close() - - _ = json.NewDecoder(response.Body).Decode(&jsonResponse) - - setPrices(&jsonResponse, fromCurrency) - } - - display(widget) -} - -func makeRequest(currency *fromCurrency) *http.Request { - fsym := currency.name - tsyms := "" - for _, to := range currency.to { - tsyms += fmt.Sprintf("%s,", to.name) - } - - url := fmt.Sprintf("%s?fsym=%s&tsyms=%s", baseURL, fsym, tsyms) - request, err := http.NewRequest("GET", url, nil) - - if err != nil { - } - - return request -} - -func setPrices(response *cResponse, currencry *fromCurrency) { - for idx, toCurrency := range currencry.to { - currencry.to[idx].price = (*response)[toCurrency.name] - } -} From 17da5fd2daa54359c208957bc947c2eaa0c59f2b Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Wed, 6 Jun 2018 02:55:20 +0430 Subject: [PATCH 2/5] changed limit default --- cryptoexchanges/cryptolive/toplist/widget.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cryptoexchanges/cryptolive/toplist/widget.go b/cryptoexchanges/cryptolive/toplist/widget.go index b040cee0..557ca325 100644 --- a/cryptoexchanges/cryptolive/toplist/widget.go +++ b/cryptoexchanges/cryptolive/toplist/widget.go @@ -40,7 +40,7 @@ func (widget *Widget) setList() { for fromCurrency := range currenciesMap { displayName := Config.UString("wtf.mods.cryptolive.top."+fromCurrency+".displayName", "") - limit := Config.UInt("wtf.mods.cryptolive.top."+fromCurrency+".limit", 5) + limit := Config.UInt("wtf.mods.cryptolive.top."+fromCurrency+".limit", 1) widget.list.addItem(fromCurrency, displayName, limit, makeToList(fromCurrency, limit)) } } From e0e9e29e916d85e6fab69b0ef0df1e164925dc07 Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Wed, 6 Jun 2018 03:10:30 +0430 Subject: [PATCH 3/5] clean up displaying --- cryptoexchanges/cryptolive/toplist/display.go | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/cryptoexchanges/cryptolive/toplist/display.go b/cryptoexchanges/cryptolive/toplist/display.go index 5b5f2434..57d7b2ca 100644 --- a/cryptoexchanges/cryptolive/toplist/display.go +++ b/cryptoexchanges/cryptolive/toplist/display.go @@ -6,18 +6,31 @@ func (widget *Widget) display() { str := "" for _, fromCurrency := range widget.list.items { str += fmt.Sprintf("%s (%s)\n", fromCurrency.displayName, fromCurrency.name) - for _, toCurrency := range fromCurrency.to { - str += fmt.Sprintf(" %s\n", toCurrency.name) - for _, info := range toCurrency.info { - str += makeInfoRow(info) - str += "\n\n" - } - } + str += makeToListText(fromCurrency.to) } widget.Result = str } -func makeInfoRow(info tInfo) string { +func makeToListText(toList []*tCurrency) string { + str := "" + for _, toCurrency := range toList { + str += makeToText(toCurrency) + } + + return str +} + +func makeToText(toCurrency *tCurrency) string { + str := "" + str += fmt.Sprintf(" %s\n", toCurrency.name) + for _, info := range toCurrency.info { + str += makeInfoText(info) + str += "\n\n" + } + return str +} + +func makeInfoText(info tInfo) string { return fmt.Sprintf(" Exchange: %s\n", info.exchange) + fmt.Sprintf(" Volume(24h): %f-%f", info.volume24h, info.volume24hTo) } From f2ca1c1d5d18cec3540d0747f9487b36dac4ddbc Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Thu, 21 Jun 2018 16:03:27 +0430 Subject: [PATCH 4/5] update cryptolive --- cryptoexchanges/cryptolive/cryptolive.go | 1 - cryptoexchanges/cryptolive/price/widget.go | 21 +++------ cryptoexchanges/cryptolive/toplist/display.go | 37 +++++++++++---- cryptoexchanges/cryptolive/toplist/widget.go | 46 ++++++++++++++----- cryptoexchanges/cryptolive/widget.go | 11 +++-- 5 files changed, 74 insertions(+), 42 deletions(-) delete mode 100644 cryptoexchanges/cryptolive/cryptolive.go diff --git a/cryptoexchanges/cryptolive/cryptolive.go b/cryptoexchanges/cryptolive/cryptolive.go deleted file mode 100644 index f92d7dbf..00000000 --- a/cryptoexchanges/cryptolive/cryptolive.go +++ /dev/null @@ -1 +0,0 @@ -package cryptolive diff --git a/cryptoexchanges/cryptolive/price/widget.go b/cryptoexchanges/cryptolive/price/widget.go index 26d8ef9d..85c5deb0 100644 --- a/cryptoexchanges/cryptolive/price/widget.go +++ b/cryptoexchanges/cryptolive/price/widget.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "sync" "time" "github.com/olebedev/config" @@ -12,7 +13,6 @@ import ( // Config is a pointer to the global config object var Config *config.Config -var started = false var baseURL = "https://min-api.cryptocompare.com/data/price" var ok = true @@ -27,7 +27,6 @@ type Widget struct { // NewWidget Make new instance of widget func NewWidget() *Widget { - started = false widget := Widget{} widget.setList() @@ -51,26 +50,19 @@ func (widget *Widget) setList() { /* -------------------- Exported Functions -------------------- */ // Refresh & update after interval time -func (widget *Widget) Refresh() { - - if started == false { - // this code should run once - go func() { - for { - widget.updateCurrencies() - time.Sleep(time.Duration(widget.RefreshInterval) * time.Second) - } - }() - +func (widget *Widget) Refresh(wg *sync.WaitGroup) { + if len(widget.list.items) == 0 { + return } - started = true + widget.updateCurrencies() if !ok { widget.Result = fmt.Sprint("Please check your internet connection!") return } widget.display() + wg.Done() } /* -------------------- Unexported Functions -------------------- */ @@ -140,7 +132,6 @@ func (widget *Widget) updateCurrencies() { setPrices(&jsonResponse, fromCurrency) } - widget.display() } func makeRequest(currency *fromCurrency) *http.Request { diff --git a/cryptoexchanges/cryptolive/toplist/display.go b/cryptoexchanges/cryptolive/toplist/display.go index 57d7b2ca..53d6da16 100644 --- a/cryptoexchanges/cryptolive/toplist/display.go +++ b/cryptoexchanges/cryptolive/toplist/display.go @@ -5,32 +5,51 @@ import "fmt" func (widget *Widget) display() { str := "" for _, fromCurrency := range widget.list.items { - str += fmt.Sprintf("%s (%s)\n", fromCurrency.displayName, fromCurrency.name) - str += makeToListText(fromCurrency.to) + str += fmt.Sprintf( + "[%s]%s [%s](%s)\n", + widget.colors.from.displayName, + fromCurrency.displayName, + widget.colors.from.name, + fromCurrency.name, + ) + str += makeToListText(fromCurrency.to, widget.colors) } widget.Result = str } -func makeToListText(toList []*tCurrency) string { +func makeToListText(toList []*tCurrency, colors textColors) string { str := "" for _, toCurrency := range toList { - str += makeToText(toCurrency) + str += makeToText(toCurrency, colors) } return str } -func makeToText(toCurrency *tCurrency) string { +func makeToText(toCurrency *tCurrency, colors textColors) string { str := "" - str += fmt.Sprintf(" %s\n", toCurrency.name) + str += fmt.Sprintf(" [%s]%s\n", colors.to.name, toCurrency.name) for _, info := range toCurrency.info { - str += makeInfoText(info) + str += makeInfoText(info, colors) str += "\n\n" } return str } -func makeInfoText(info tInfo) string { - return fmt.Sprintf(" Exchange: %s\n", info.exchange) + fmt.Sprintf(" Volume(24h): %f-%f", info.volume24h, info.volume24hTo) +func makeInfoText(info tInfo, colors textColors) string { + return fmt.Sprintf( + " [%s]Exchange: [%s]%s\n", + colors.to.field, + colors.to.value, + info.exchange, + ) + + fmt.Sprintf( + " [%s]Volume(24h): [%s]%f-[%s]%f", + colors.to.field, + colors.to.value, + info.volume24h, + colors.to.value, + info.volume24hTo, + ) } diff --git a/cryptoexchanges/cryptolive/toplist/widget.go b/cryptoexchanges/cryptolive/toplist/widget.go index 557ca325..feb95332 100644 --- a/cryptoexchanges/cryptolive/toplist/widget.go +++ b/cryptoexchanges/cryptolive/toplist/widget.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "os" + "sync" "time" "github.com/olebedev/config" @@ -12,9 +13,20 @@ import ( // Config is a pointer to the global config object var Config *config.Config -var started = false 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 @@ -22,15 +34,17 @@ type Widget struct { RefreshInterval int list *cList + + colors textColors } // NewWidget Make new toplist widget func NewWidget() *Widget { widget := Widget{} - started = false widget.list = &cList{} widget.setList() + widget.config() return &widget } @@ -58,27 +72,35 @@ func makeToList(fCurrencyName string, limit int) (list []*tCurrency) { 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() { - - if !started { - go func() { - for { - widget.updateData() - time.Sleep(time.Second * time.Duration(widget.RefreshInterval)) - } - }() - started = true +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), diff --git a/cryptoexchanges/cryptolive/widget.go b/cryptoexchanges/cryptolive/widget.go index eba98559..150b4ed7 100644 --- a/cryptoexchanges/cryptolive/widget.go +++ b/cryptoexchanges/cryptolive/widget.go @@ -2,6 +2,7 @@ package cryptolive import ( "fmt" + "sync" "github.com/olebedev/config" "github.com/senorprogrammer/wtf/cryptoexchanges/cryptolive/price" @@ -40,12 +41,12 @@ func NewWidget() *Widget { // Refresh & update after interval time func (widget *Widget) Refresh() { - if widget.Disabled() { - return - } + var wg sync.WaitGroup - widget.priceWidget.Refresh() - widget.toplistWidget.Refresh() + wg.Add(2) + widget.priceWidget.Refresh(&wg) + widget.toplistWidget.Refresh(&wg) + wg.Wait() widget.UpdateRefreshedAt() From 495342e3c95351e163a5c07f758d8b8e6a4b8594 Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Thu, 21 Jun 2018 16:03:45 +0430 Subject: [PATCH 5/5] update cryptolive config in docs --- .../modules/cryptocurrencies/cryptolive.md | 56 ++++++++++++------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/_site/content/posts/modules/cryptocurrencies/cryptolive.md b/_site/content/posts/modules/cryptocurrencies/cryptolive.md index bf38443b..2fd27310 100644 --- a/_site/content/posts/modules/cryptocurrencies/cryptolive.md +++ b/_site/content/posts/modules/cryptocurrencies/cryptolive.md @@ -28,33 +28,49 @@ None. ```yaml cryptolive: - colors: + enabled: true + position: + top: 5 + left: 2 + height: 1 + width: 2 + updateInterval: 15 + currencies: + BTC: + displayName: Bitcoin + to: + - USD + - EUR + - ETH + - LTC + - DOGE + LTC: + displayName: Ethereum + to: + - USD + - EUR + - BTC + top: + BTC: + displayName: Bitcoin + limit: 5 + to: + - USD + colors: from: name: coral displayName: grey to: name: white price: green - currencies: - BTC: - displayName: Bitcoin + top: + from: + name: grey + displayName: coral to: - - USD - - EUR - - ETH - ETH: - displayName: Ethereum - to: - - USD - - EUR - - ETH - enabled: true - position: - top: 5 - left: 2 - height: 1 - width: 2 - updateInterval: 15 + name: red + field: white + value: green ``` ### Attributes