1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

update cryptolive

This commit is contained in:
Hossein Mehrabi 2018-06-21 16:03:27 +04:30
parent e0e9e29e91
commit f2ca1c1d5d
5 changed files with 74 additions and 42 deletions

View File

@ -1 +0,0 @@
package cryptolive

View File

@ -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 {

View File

@ -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,
)
}

View File

@ -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),

View File

@ -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()