mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
clean up, handle error, use net/http, read colors from config file
This commit is contained in:
parent
adbb8eab52
commit
16b7cc0bb0
@ -1,11 +1,12 @@
|
|||||||
package cryptolive
|
package cryptolive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/cizixs/gohttp"
|
|
||||||
"github.com/olebedev/config"
|
"github.com/olebedev/config"
|
||||||
"github.com/senorprogrammer/wtf/wtf"
|
"github.com/senorprogrammer/wtf/wtf"
|
||||||
)
|
)
|
||||||
@ -14,6 +15,8 @@ import (
|
|||||||
var Config *config.Config
|
var Config *config.Config
|
||||||
|
|
||||||
var started = false
|
var started = false
|
||||||
|
var baseURL = "https://min-api.cryptocompare.com/data/price"
|
||||||
|
var ok = true
|
||||||
|
|
||||||
// Widget define wtf widget to register widget later
|
// Widget define wtf widget to register widget later
|
||||||
type Widget struct {
|
type Widget struct {
|
||||||
@ -60,6 +63,7 @@ func (widget *Widget) Refresh() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if started == false {
|
if started == false {
|
||||||
|
// this code should run once
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
widget.updateCurrencies()
|
widget.updateCurrencies()
|
||||||
@ -73,6 +77,13 @@ func (widget *Widget) Refresh() {
|
|||||||
|
|
||||||
widget.UpdateRefreshedAt()
|
widget.UpdateRefreshedAt()
|
||||||
widget.View.Clear()
|
widget.View.Clear()
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
widget.View.SetText(
|
||||||
|
fmt.Sprint("Please check your internet connection!"),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
display(widget)
|
display(widget)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,45 +91,21 @@ func (widget *Widget) Refresh() {
|
|||||||
|
|
||||||
func display(widget *Widget) {
|
func display(widget *Widget) {
|
||||||
str := ""
|
str := ""
|
||||||
|
var (
|
||||||
|
fromNameColor = Config.UString("wtf.mods.cryptolive.colors.from.name", "coral")
|
||||||
|
fromDisplayNameColor = Config.UString("wtf.mods.cryptolive.colors.from.displayName", "gray")
|
||||||
|
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 {
|
for _, item := range widget.list.items {
|
||||||
str += fmt.Sprintf("[coral]%s[gray](%s):\n", item.displayName, item.name)
|
str += fmt.Sprintf("[%s]%s[%s](%s):\n", fromNameColor, item.displayName, fromDisplayNameColor, item.name)
|
||||||
for _, toItem := range item.to {
|
for _, toItem := range item.to {
|
||||||
str += fmt.Sprintf("\t%s[%s]: %f\n", toItem.name, "green", toItem.price)
|
str += fmt.Sprintf("\t[%s]%s: [%s]%f\n", toNameColor, toItem.name, toPriceColor, toItem.price)
|
||||||
}
|
}
|
||||||
str += "\n"
|
str += "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(
|
widget.View.SetText(fmt.Sprintf("\n%s", str))
|
||||||
widget.View,
|
|
||||||
"\n%s",
|
|
||||||
str,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (widget *Widget) updateCurrencies() {
|
|
||||||
defer func() {
|
|
||||||
recover()
|
|
||||||
}()
|
|
||||||
for _, fromCurrency := range widget.list.items {
|
|
||||||
request := gohttp.New().Path("data", "price").Query("fsym", fromCurrency.name)
|
|
||||||
tsyms := ""
|
|
||||||
for _, toCurrency := range fromCurrency.to {
|
|
||||||
tsyms += fmt.Sprintf("%s,", toCurrency.name)
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := request.Query("tsyms", tsyms).Get("https://min-api.cryptocompare.com")
|
|
||||||
if err != nil {
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonResponse := &cResponse{}
|
|
||||||
response.AsJSON(jsonResponse)
|
|
||||||
|
|
||||||
responseRef := reflect.Indirect(reflect.ValueOf(jsonResponse))
|
|
||||||
for idx, toCurrency := range fromCurrency.to {
|
|
||||||
fromCurrency.to[idx].price = responseRef.FieldByName(toCurrency.name).Interface().(float32)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getToList(fromName string) []*toCurrency {
|
func getToList(fromName string) []*toCurrency {
|
||||||
@ -135,3 +122,58 @@ func getToList(fromName string) []*toCurrency {
|
|||||||
|
|
||||||
return toList
|
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)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
responseRef := reflect.Indirect(reflect.ValueOf(response))
|
||||||
|
for idx, toCurrency := range currencry.to {
|
||||||
|
currencry.to[idx].price = responseRef.FieldByName(toCurrency.name).Interface().(float32)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user