Skip to content

Commit

Permalink
update cryptolive
Browse files Browse the repository at this point in the history
  • Loading branch information
jeangovil committed Jun 21, 2018
1 parent e0e9e29 commit f2ca1c1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 42 deletions.
1 change: 0 additions & 1 deletion cryptoexchanges/cryptolive/cryptolive.go

This file was deleted.

21 changes: 6 additions & 15 deletions cryptoexchanges/cryptolive/price/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"sync"
"time"

"github.com/olebedev/config"
Expand All @@ -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

Expand All @@ -27,7 +27,6 @@ type Widget struct {

// NewWidget Make new instance of widget
func NewWidget() *Widget {
started = false
widget := Widget{}

widget.setList()
Expand All @@ -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 -------------------- */
Expand Down Expand Up @@ -140,7 +132,6 @@ func (widget *Widget) updateCurrencies() {
setPrices(&jsonResponse, fromCurrency)
}

widget.display()
}

func makeRequest(currency *fromCurrency) *http.Request {
Expand Down
37 changes: 28 additions & 9 deletions cryptoexchanges/cryptolive/toplist/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
46 changes: 34 additions & 12 deletions cryptoexchanges/cryptolive/toplist/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,46 @@ import (
"fmt"
"net/http"
"os"
"sync"
"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"

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

started = false
widget.list = &cList{}
widget.setList()
widget.config()

return &widget
}
Expand Down Expand Up @@ -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),
Expand Down
11 changes: 6 additions & 5 deletions cryptoexchanges/cryptolive/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cryptolive

import (
"fmt"
"sync"

"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/cryptoexchanges/cryptolive/price"
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit f2ca1c1

Please sign in to comment.