mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
made cryptolive module
This commit is contained in:
parent
2de364a2ef
commit
36e98b0299
44
cryptolive/cryptolive.go
Normal file
44
cryptolive/cryptolive.go
Normal file
@ -0,0 +1,44 @@
|
||||
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 struct {
|
||||
BTC float32 `json:"BTC"`
|
||||
HBZ float32 `json:"HBZ"`
|
||||
ETH float32 `json:"ETH"`
|
||||
EOS float32 `json:"EOS"`
|
||||
BCH float32 `json:"BCH"`
|
||||
TRX float32 `json:"TRX"`
|
||||
XRP float32 `json:"XRP"`
|
||||
LTC float32 `json:"LTC"`
|
||||
ETC float32 `json:"ETC"`
|
||||
ADA float32 `json:"ADA"`
|
||||
CMT float32 `json:"CMT"`
|
||||
DASH float32 `json:"DASH"`
|
||||
ZEC float32 `json:"ZEC"`
|
||||
IOT float32 `json:"IOT"`
|
||||
ONT float32 `json:"ONT"`
|
||||
NEO float32 `json:"NEO"`
|
||||
BTG float32 `json:"BTG"`
|
||||
LSK float32 `json:"LSK"`
|
||||
ELA float32 `json:"ELA"`
|
||||
DTA float32 `json:"DTA"`
|
||||
NANO float32 `json:"NANO"`
|
||||
WTC float32 `json:"WTC"`
|
||||
DOGE float32 `json:"DOGE"`
|
||||
USD float32 `json:"USD"`
|
||||
EUR float32 `json:"EUR"`
|
||||
}
|
133
cryptolive/widget.go
Normal file
133
cryptolive/widget.go
Normal file
@ -0,0 +1,133 @@
|
||||
package cryptolive
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/cizixs/gohttp"
|
||||
|
||||
"reflect"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
// Config is a pointer to the global config object
|
||||
var Config *config.Config
|
||||
|
||||
var started = false
|
||||
|
||||
// Widget define wtf widget to register widget later
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
|
||||
CurrentIcon int
|
||||
|
||||
*list
|
||||
}
|
||||
|
||||
// NewWidget Make new instance of widget
|
||||
func NewWidget() *Widget {
|
||||
started = false
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(" $ CryptoLive ", "cryptolive", false),
|
||||
}
|
||||
|
||||
currenciesMap, _ := Config.Map("wtf.mods.cryptolive.currencies")
|
||||
|
||||
var currencies []*fromCurrency
|
||||
|
||||
for currency := range currenciesMap {
|
||||
displayName, _ := Config.String("wtf.mods.cryptolive.currencies." + currency + ".displayName")
|
||||
toCList, _ := Config.List("wtf.mods.cryptolive.currencies." + currency + ".to")
|
||||
var toList []*toCurrency
|
||||
for _, v := range toCList {
|
||||
toList = append(toList, &toCurrency{
|
||||
name: v.(string),
|
||||
price: -1,
|
||||
})
|
||||
}
|
||||
currencies = append(currencies, &fromCurrency{
|
||||
name: currency,
|
||||
displayName: displayName,
|
||||
to: toList,
|
||||
})
|
||||
}
|
||||
|
||||
widget.list = &list{
|
||||
items: currencies,
|
||||
}
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
// Refresh & update after interval time
|
||||
func (widget *Widget) Refresh() {
|
||||
if widget.Disabled() {
|
||||
return
|
||||
}
|
||||
|
||||
if started == false {
|
||||
go func() {
|
||||
for {
|
||||
widget.updateCurrencies()
|
||||
time.Sleep(time.Duration(widget.RefreshInterval()) * time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
started = true
|
||||
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.View.Clear()
|
||||
display(widget)
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func display(widget *Widget) {
|
||||
str := ""
|
||||
for _, item := range widget.list.items {
|
||||
str += fmt.Sprintf("[coral]%s[gray](%s):\n", item.displayName, item.name)
|
||||
for _, toItem := range item.to {
|
||||
str += fmt.Sprintf("\t%s[%s]: %f\n", toItem.name, "green", toItem.price)
|
||||
}
|
||||
str += "\n"
|
||||
}
|
||||
|
||||
fmt.Fprintf(
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user