mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	update cryptolive
This commit is contained in:
		
							parent
							
								
									e0e9e29e91
								
							
						
					
					
						commit
						f2ca1c1d5d
					
				@ -1 +0,0 @@
 | 
			
		||||
package cryptolive
 | 
			
		||||
@ -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() {
 | 
			
		||||
func (widget *Widget) Refresh(wg *sync.WaitGroup) {
 | 
			
		||||
	if len(widget.list.items) == 0 {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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()
 | 
			
		||||
	wg.Done()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- Unexported Functions -------------------- */
 | 
			
		||||
@ -140,7 +132,6 @@ func (widget *Widget) updateCurrencies() {
 | 
			
		||||
		setPrices(&jsonResponse, fromCurrency)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	widget.display()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func makeRequest(currency *fromCurrency) *http.Request {
 | 
			
		||||
 | 
			
		||||
@ -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,
 | 
			
		||||
		)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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() {
 | 
			
		||||
func (widget *Widget) Refresh(wg *sync.WaitGroup) {
 | 
			
		||||
	if len(widget.list.items) == 0 {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !started {
 | 
			
		||||
		go func() {
 | 
			
		||||
			for {
 | 
			
		||||
	widget.updateData()
 | 
			
		||||
				time.Sleep(time.Second * time.Duration(widget.RefreshInterval))
 | 
			
		||||
			}
 | 
			
		||||
		}()
 | 
			
		||||
		started = true
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	widget.display()
 | 
			
		||||
	wg.Done()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- Unexported Functions -------------------- */
 | 
			
		||||
 | 
			
		||||
func (widget *Widget) updateData() {
 | 
			
		||||
	defer func() {
 | 
			
		||||
		recover()
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	client := &http.Client{
 | 
			
		||||
		Timeout: time.Duration(5 * time.Second),
 | 
			
		||||
 | 
			
		||||
@ -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()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user