mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
55
modules/cryptoexchanges/cryptolive/toplist/display.go
Normal file
55
modules/cryptoexchanges/cryptolive/toplist/display.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package toplist
|
||||
|
||||
import "fmt"
|
||||
|
||||
func (widget *Widget) display() {
|
||||
str := ""
|
||||
for _, fromCurrency := range widget.list.items {
|
||||
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, colors textColors) string {
|
||||
str := ""
|
||||
for _, toCurrency := range toList {
|
||||
str += makeToText(toCurrency, colors)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func makeToText(toCurrency *tCurrency, colors textColors) string {
|
||||
str := ""
|
||||
str += fmt.Sprintf(" [%s]%s\n", colors.to.name, toCurrency.name)
|
||||
for _, info := range toCurrency.info {
|
||||
str += makeInfoText(info, colors)
|
||||
str += "\n\n"
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
41
modules/cryptoexchanges/cryptolive/toplist/toplist.go
Normal file
41
modules/cryptoexchanges/cryptolive/toplist/toplist.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package toplist
|
||||
|
||||
type cList struct {
|
||||
items []*fCurrency
|
||||
}
|
||||
|
||||
type fCurrency struct {
|
||||
name, displayName string
|
||||
limit int
|
||||
to []*tCurrency
|
||||
}
|
||||
|
||||
type tCurrency struct {
|
||||
name string
|
||||
info []tInfo
|
||||
}
|
||||
|
||||
type tInfo struct {
|
||||
exchange string
|
||||
volume24h, volume24hTo float32
|
||||
}
|
||||
|
||||
type responseInterface struct {
|
||||
Response string `json:"Response"`
|
||||
Data []struct {
|
||||
Exchange string `json:"exchange"`
|
||||
FromSymbol string `json:"fromSymbol"`
|
||||
ToSymbol string `json:"toSymbol"`
|
||||
Volume24h float32 `json:"volume24h"`
|
||||
Volume24hTo float32 `json:"volume24hTo"`
|
||||
} `json:"Data"`
|
||||
}
|
||||
|
||||
func (list *cList) addItem(name, displayName string, limit int, to []*tCurrency) {
|
||||
list.items = append(list.items, &fCurrency{
|
||||
name: name,
|
||||
displayName: displayName,
|
||||
limit: limit,
|
||||
to: to,
|
||||
})
|
||||
}
|
||||
137
modules/cryptoexchanges/cryptolive/toplist/widget.go
Normal file
137
modules/cryptoexchanges/cryptolive/toplist/widget.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package toplist
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
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{}
|
||||
|
||||
widget.list = &cList{}
|
||||
widget.setList()
|
||||
widget.config()
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
func (widget *Widget) setList() {
|
||||
currenciesMap, _ := wtf.Config.Map("wtf.mods.cryptolive.top")
|
||||
|
||||
for fromCurrency := range currenciesMap {
|
||||
displayName := wtf.Config.UString("wtf.mods.cryptolive.top."+fromCurrency+".displayName", "")
|
||||
limit := wtf.Config.UInt("wtf.mods.cryptolive.top."+fromCurrency+".limit", 1)
|
||||
widget.list.addItem(fromCurrency, displayName, limit, makeToList(fromCurrency, limit))
|
||||
}
|
||||
}
|
||||
|
||||
func makeToList(fCurrencyName string, limit int) (list []*tCurrency) {
|
||||
toList, _ := wtf.Config.List("wtf.mods.cryptolive.top." + fCurrencyName + ".to")
|
||||
|
||||
for _, toCurrency := range toList {
|
||||
list = append(list, &tCurrency{
|
||||
name: toCurrency.(string),
|
||||
info: make([]tInfo, limit),
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (widget *Widget) config() {
|
||||
// set colors
|
||||
widget.colors.from.name = wtf.Config.UString("wtf.mods.cryptolive.colors.top.from.name", "coral")
|
||||
widget.colors.from.displayName = wtf.Config.UString("wtf.mods.cryptolive.colors.top.from.displayName", "grey")
|
||||
widget.colors.to.name = wtf.Config.UString("wtf.mods.cryptolive.colors.top.to.name", "red")
|
||||
widget.colors.to.field = wtf.Config.UString("wtf.mods.cryptolive.colors.top.to.field", "white")
|
||||
widget.colors.to.value = wtf.Config.UString("wtf.mods.cryptolive.colors.top.to.value", "value")
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
// Refresh & update after interval time
|
||||
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),
|
||||
}
|
||||
|
||||
for _, fromCurrency := range widget.list.items {
|
||||
for _, toCurrency := range fromCurrency.to {
|
||||
|
||||
request := makeRequest(fromCurrency.name, toCurrency.name, fromCurrency.limit)
|
||||
response, _ := client.Do(request)
|
||||
|
||||
var jsonResponse responseInterface
|
||||
|
||||
err := json.NewDecoder(response.Body).Decode(&jsonResponse)
|
||||
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for idx, info := range jsonResponse.Data {
|
||||
toCurrency.info[idx] = tInfo{
|
||||
exchange: info.Exchange,
|
||||
volume24h: info.Volume24h,
|
||||
volume24hTo: info.Volume24hTo,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func makeRequest(fsym, tsym string, limit int) *http.Request {
|
||||
url := fmt.Sprintf("%s?fsym=%s&tsym=%s&limit=%d", baseURL, fsym, tsym, limit)
|
||||
request, _ := http.NewRequest("GET", url, nil)
|
||||
return request
|
||||
}
|
||||
Reference in New Issue
Block a user