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"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/olebedev/config"
 | 
						"github.com/olebedev/config"
 | 
				
			||||||
@ -12,7 +13,6 @@ import (
 | 
				
			|||||||
// Config is a pointer to the global config object
 | 
					// Config is a pointer to the global config object
 | 
				
			||||||
var Config *config.Config
 | 
					var Config *config.Config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var started = false
 | 
					 | 
				
			||||||
var baseURL = "https://min-api.cryptocompare.com/data/price"
 | 
					var baseURL = "https://min-api.cryptocompare.com/data/price"
 | 
				
			||||||
var ok = true
 | 
					var ok = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -27,7 +27,6 @@ type Widget struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// NewWidget Make new instance of widget
 | 
					// NewWidget Make new instance of widget
 | 
				
			||||||
func NewWidget() *Widget {
 | 
					func NewWidget() *Widget {
 | 
				
			||||||
	started = false
 | 
					 | 
				
			||||||
	widget := Widget{}
 | 
						widget := Widget{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	widget.setList()
 | 
						widget.setList()
 | 
				
			||||||
@ -51,26 +50,19 @@ func (widget *Widget) setList() {
 | 
				
			|||||||
/* -------------------- Exported Functions -------------------- */
 | 
					/* -------------------- Exported Functions -------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Refresh & update after interval time
 | 
					// 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()
 | 
						widget.updateCurrencies()
 | 
				
			||||||
				time.Sleep(time.Duration(widget.RefreshInterval) * time.Second)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	started = true
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if !ok {
 | 
						if !ok {
 | 
				
			||||||
		widget.Result = fmt.Sprint("Please check your internet connection!")
 | 
							widget.Result = fmt.Sprint("Please check your internet connection!")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	widget.display()
 | 
						widget.display()
 | 
				
			||||||
 | 
						wg.Done()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* -------------------- Unexported Functions -------------------- */
 | 
					/* -------------------- Unexported Functions -------------------- */
 | 
				
			||||||
@ -140,7 +132,6 @@ func (widget *Widget) updateCurrencies() {
 | 
				
			|||||||
		setPrices(&jsonResponse, fromCurrency)
 | 
							setPrices(&jsonResponse, fromCurrency)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	widget.display()
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func makeRequest(currency *fromCurrency) *http.Request {
 | 
					func makeRequest(currency *fromCurrency) *http.Request {
 | 
				
			||||||
 | 
				
			|||||||
@ -5,32 +5,51 @@ import "fmt"
 | 
				
			|||||||
func (widget *Widget) display() {
 | 
					func (widget *Widget) display() {
 | 
				
			||||||
	str := ""
 | 
						str := ""
 | 
				
			||||||
	for _, fromCurrency := range widget.list.items {
 | 
						for _, fromCurrency := range widget.list.items {
 | 
				
			||||||
		str += fmt.Sprintf("%s (%s)\n", fromCurrency.displayName, fromCurrency.name)
 | 
							str += fmt.Sprintf(
 | 
				
			||||||
		str += makeToListText(fromCurrency.to)
 | 
								"[%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
 | 
						widget.Result = str
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func makeToListText(toList []*tCurrency) string {
 | 
					func makeToListText(toList []*tCurrency, colors textColors) string {
 | 
				
			||||||
	str := ""
 | 
						str := ""
 | 
				
			||||||
	for _, toCurrency := range toList {
 | 
						for _, toCurrency := range toList {
 | 
				
			||||||
		str += makeToText(toCurrency)
 | 
							str += makeToText(toCurrency, colors)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return str
 | 
						return str
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func makeToText(toCurrency *tCurrency) string {
 | 
					func makeToText(toCurrency *tCurrency, colors textColors) string {
 | 
				
			||||||
	str := ""
 | 
						str := ""
 | 
				
			||||||
	str += fmt.Sprintf("  %s\n", toCurrency.name)
 | 
						str += fmt.Sprintf("  [%s]%s\n", colors.to.name, toCurrency.name)
 | 
				
			||||||
	for _, info := range toCurrency.info {
 | 
						for _, info := range toCurrency.info {
 | 
				
			||||||
		str += makeInfoText(info)
 | 
							str += makeInfoText(info, colors)
 | 
				
			||||||
		str += "\n\n"
 | 
							str += "\n\n"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return str
 | 
						return str
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func makeInfoText(info tInfo) string {
 | 
					func makeInfoText(info tInfo, colors textColors) string {
 | 
				
			||||||
	return fmt.Sprintf("    Exchange: %s\n", info.exchange) + fmt.Sprintf("    Volume(24h): %f-%f", info.volume24h, info.volume24hTo)
 | 
						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"
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/olebedev/config"
 | 
						"github.com/olebedev/config"
 | 
				
			||||||
@ -12,9 +13,20 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Config is a pointer to the global config object
 | 
					// Config is a pointer to the global config object
 | 
				
			||||||
var Config *config.Config
 | 
					var Config *config.Config
 | 
				
			||||||
var started = false
 | 
					 | 
				
			||||||
var baseURL = "https://min-api.cryptocompare.com/data/top/exchanges"
 | 
					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
 | 
					// Widget Toplist Widget
 | 
				
			||||||
type Widget struct {
 | 
					type Widget struct {
 | 
				
			||||||
	Result string
 | 
						Result string
 | 
				
			||||||
@ -22,15 +34,17 @@ type Widget struct {
 | 
				
			|||||||
	RefreshInterval int
 | 
						RefreshInterval int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	list *cList
 | 
						list *cList
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						colors textColors
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewWidget Make new toplist widget
 | 
					// NewWidget Make new toplist widget
 | 
				
			||||||
func NewWidget() *Widget {
 | 
					func NewWidget() *Widget {
 | 
				
			||||||
	widget := Widget{}
 | 
						widget := Widget{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	started = false
 | 
					 | 
				
			||||||
	widget.list = &cList{}
 | 
						widget.list = &cList{}
 | 
				
			||||||
	widget.setList()
 | 
						widget.setList()
 | 
				
			||||||
 | 
						widget.config()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return &widget
 | 
						return &widget
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -58,27 +72,35 @@ func makeToList(fCurrencyName string, limit int) (list []*tCurrency) {
 | 
				
			|||||||
	return
 | 
						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 -------------------- */
 | 
					/* -------------------- Exported Functions -------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Refresh & update after interval time
 | 
					// 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()
 | 
						widget.updateData()
 | 
				
			||||||
				time.Sleep(time.Second * time.Duration(widget.RefreshInterval))
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}()
 | 
					 | 
				
			||||||
		started = true
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	widget.display()
 | 
						widget.display()
 | 
				
			||||||
 | 
						wg.Done()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* -------------------- Unexported Functions -------------------- */
 | 
					/* -------------------- Unexported Functions -------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (widget *Widget) updateData() {
 | 
					func (widget *Widget) updateData() {
 | 
				
			||||||
 | 
						defer func() {
 | 
				
			||||||
 | 
							recover()
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	client := &http.Client{
 | 
						client := &http.Client{
 | 
				
			||||||
		Timeout: time.Duration(5 * time.Second),
 | 
							Timeout: time.Duration(5 * time.Second),
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@ package cryptolive
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/olebedev/config"
 | 
						"github.com/olebedev/config"
 | 
				
			||||||
	"github.com/senorprogrammer/wtf/cryptoexchanges/cryptolive/price"
 | 
						"github.com/senorprogrammer/wtf/cryptoexchanges/cryptolive/price"
 | 
				
			||||||
@ -40,12 +41,12 @@ func NewWidget() *Widget {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Refresh & update after interval time
 | 
					// Refresh & update after interval time
 | 
				
			||||||
func (widget *Widget) Refresh() {
 | 
					func (widget *Widget) Refresh() {
 | 
				
			||||||
	if widget.Disabled() {
 | 
						var wg sync.WaitGroup
 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	widget.priceWidget.Refresh()
 | 
						wg.Add(2)
 | 
				
			||||||
	widget.toplistWidget.Refresh()
 | 
						widget.priceWidget.Refresh(&wg)
 | 
				
			||||||
 | 
						widget.toplistWidget.Refresh(&wg)
 | 
				
			||||||
 | 
						wg.Wait()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	widget.UpdateRefreshedAt()
 | 
						widget.UpdateRefreshedAt()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user