mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Merge branch 'master' into top-exchanges
This commit is contained in:
@@ -8,14 +8,11 @@ import (
|
||||
|
||||
func (widget *Widget) display() {
|
||||
if ok == false {
|
||||
widget.View.SetText(fmt.Sprintf("%s", errorText))
|
||||
widget.View.SetText(errorText)
|
||||
return
|
||||
}
|
||||
|
||||
str := ""
|
||||
str += summaryText(&widget.summaryList, &widget.TextColors)
|
||||
|
||||
widget.View.SetText(fmt.Sprintf("%s", str))
|
||||
widget.View.SetText(summaryText(&widget.summaryList, &widget.TextColors))
|
||||
}
|
||||
|
||||
func summaryText(list *summaryList, colors *TextColors) string {
|
||||
|
||||
@@ -7,13 +7,9 @@ import (
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
// Config is a pointer to the global config object
|
||||
var Config *config.Config
|
||||
|
||||
type TextColors struct {
|
||||
base struct {
|
||||
name string
|
||||
@@ -28,7 +24,7 @@ type TextColors struct {
|
||||
|
||||
var ok = true
|
||||
var errorText = ""
|
||||
var started = false
|
||||
|
||||
var baseURL = "https://bittrex.com/api/v1.1/public/getmarketsummary"
|
||||
|
||||
// Widget define wtf widget to register widget later
|
||||
@@ -40,13 +36,11 @@ type Widget struct {
|
||||
|
||||
// NewWidget Make new instance of widget
|
||||
func NewWidget() *Widget {
|
||||
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(" Bittrex ", "bittrex", false),
|
||||
summaryList: summaryList{},
|
||||
}
|
||||
|
||||
started = false
|
||||
ok = true
|
||||
errorText = ""
|
||||
|
||||
@@ -57,17 +51,17 @@ func NewWidget() *Widget {
|
||||
}
|
||||
|
||||
func (widget *Widget) config() {
|
||||
widget.TextColors.base.name = Config.UString("wtf.mods.bittrex.colors.base.name", "red")
|
||||
widget.TextColors.base.displayName = Config.UString("wtf.mods.bittrex.colors.base.displayName", "grey")
|
||||
widget.TextColors.market.name = Config.UString("wtf.mods.bittrex.colors.market.name", "red")
|
||||
widget.TextColors.market.field = Config.UString("wtf.mods.bittrex.colors.market.field", "coral")
|
||||
widget.TextColors.market.value = Config.UString("wtf.mods.bittrex.colors.market.value", "white")
|
||||
widget.TextColors.base.name = wtf.Config.UString("wtf.mods.bittrex.colors.base.name", "red")
|
||||
widget.TextColors.base.displayName = wtf.Config.UString("wtf.mods.bittrex.colors.base.displayName", "grey")
|
||||
widget.TextColors.market.name = wtf.Config.UString("wtf.mods.bittrex.colors.market.name", "red")
|
||||
widget.TextColors.market.field = wtf.Config.UString("wtf.mods.bittrex.colors.market.field", "coral")
|
||||
widget.TextColors.market.value = wtf.Config.UString("wtf.mods.bittrex.colors.market.value", "white")
|
||||
}
|
||||
|
||||
func (widget *Widget) setSummaryList() {
|
||||
sCurrencies, _ := Config.Map("wtf.mods.bittrex.summary")
|
||||
sCurrencies, _ := wtf.Config.Map("wtf.mods.bittrex.summary")
|
||||
for baseCurrencyName := range sCurrencies {
|
||||
displayName, _ := Config.String("wtf.mods.bittrex.summary." + baseCurrencyName + ".displayName")
|
||||
displayName, _ := wtf.Config.String("wtf.mods.bittrex.summary." + baseCurrencyName + ".displayName")
|
||||
mCurrencyList := makeSummaryMarketList(baseCurrencyName)
|
||||
widget.summaryList.addSummaryItem(baseCurrencyName, displayName, mCurrencyList)
|
||||
}
|
||||
@@ -76,7 +70,7 @@ func (widget *Widget) setSummaryList() {
|
||||
func makeSummaryMarketList(currencyName string) []*mCurrency {
|
||||
mCurrencyList := []*mCurrency{}
|
||||
|
||||
configMarketList, _ := Config.List("wtf.mods.bittrex.summary." + currencyName + ".market")
|
||||
configMarketList, _ := wtf.Config.List("wtf.mods.bittrex.summary." + currencyName + ".market")
|
||||
for _, mCurrencyName := range configMarketList {
|
||||
mCurrencyList = append(mCurrencyList, makeMarketCurrency(mCurrencyName.(string)))
|
||||
}
|
||||
@@ -102,24 +96,9 @@ func makeMarketCurrency(name string) *mCurrency {
|
||||
|
||||
// Refresh & update after interval time
|
||||
func (widget *Widget) Refresh() {
|
||||
if widget.Disabled() {
|
||||
return
|
||||
}
|
||||
|
||||
if started == false {
|
||||
go func() {
|
||||
for {
|
||||
widget.updateSummary()
|
||||
time.Sleep(time.Second * time.Duration(widget.RefreshInterval()))
|
||||
}
|
||||
}()
|
||||
started = true
|
||||
}
|
||||
|
||||
widget.updateSummary()
|
||||
widget.UpdateRefreshedAt()
|
||||
|
||||
widget.display()
|
||||
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
121
cryptoexchanges/blockfolio/widget.go
Normal file
121
cryptoexchanges/blockfolio/widget.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package blockfolio
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
|
||||
app *tview.Application
|
||||
device_token string
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(" Blockfolio ", "blockfolio", false),
|
||||
device_token: wtf.Config.UString("wtf.mods.blockfolio.device_token"),
|
||||
}
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.View.SetTitle(" Blockfolio ")
|
||||
|
||||
positions, err := Fetch(widget.device_token)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
widget.View.SetText(contentFrom(positions))
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
func contentFrom(positions *AllPositionsResponse) string {
|
||||
res := ""
|
||||
colorName := wtf.Config.UString("wtf.mods.blockfolio.colors.name")
|
||||
colorGrows := wtf.Config.UString("wtf.mods.blockfolio.colors.grows")
|
||||
colorDrop := wtf.Config.UString("wtf.mods.blockfolio.colors.drop")
|
||||
displayHoldings := wtf.Config.UBool("wtf.mods.blockfolio.displayHoldings")
|
||||
var totalFiat float32
|
||||
totalFiat = 0.0
|
||||
for i := 0; i < len(positions.PositionList); i++ {
|
||||
colorForChange := colorGrows
|
||||
if positions.PositionList[i].TwentyFourHourPercentChangeFiat <= 0 {
|
||||
colorForChange = colorDrop
|
||||
}
|
||||
totalFiat += positions.PositionList[i].HoldingValueFiat
|
||||
if displayHoldings {
|
||||
res = res + fmt.Sprintf("[%s]%-6s - %5.2f ([%s]%.3fk [%s]%.2f%s)\n", colorName, positions.PositionList[i].Coin, positions.PositionList[i].Quantity, colorForChange, positions.PositionList[i].HoldingValueFiat/1000, colorForChange, positions.PositionList[i].TwentyFourHourPercentChangeFiat, "%")
|
||||
} else {
|
||||
res = res + fmt.Sprintf("[%s]%-6s - %5.2f ([%s]%.2f%s)\n", colorName, positions.PositionList[i].Coin, positions.PositionList[i].Quantity, colorForChange, positions.PositionList[i].TwentyFourHourPercentChangeFiat, "%")
|
||||
}
|
||||
}
|
||||
if displayHoldings {
|
||||
res = res + fmt.Sprintf("\n[%s]Total value: $%.3fk", "green", totalFiat/1000)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
//always the same
|
||||
const magic = "edtopjhgn2345piuty89whqejfiobh89-2q453"
|
||||
|
||||
type Position struct {
|
||||
Coin string `json:coin`
|
||||
LastPriceFiat float32 `json:lastPriceFiat`
|
||||
TwentyFourHourPercentChangeFiat float32 `json:twentyFourHourPercentChangeFiat`
|
||||
Quantity float32 `json:quantity`
|
||||
HoldingValueFiat float32 `json:holdingValueFiat`
|
||||
}
|
||||
|
||||
type AllPositionsResponse struct {
|
||||
PositionList []Position `json:positionList`
|
||||
}
|
||||
|
||||
func MakeApiRequest(token string, method string) ([]byte, error) {
|
||||
client := &http.Client{}
|
||||
url := "https://api-v0.blockfolio.com/rest/" + method + "/" + token + "?use_alias=true&fiat_currency=USD"
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("magic", magic)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return body, err
|
||||
}
|
||||
|
||||
func GetAllPositions(token string) (*AllPositionsResponse, error) {
|
||||
jsn, err := MakeApiRequest(token, "get_all_positions")
|
||||
var parsed AllPositionsResponse
|
||||
|
||||
err = json.Unmarshal(jsn, &parsed)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse json %v", err)
|
||||
return nil, err
|
||||
}
|
||||
return &parsed, err
|
||||
}
|
||||
|
||||
func Fetch(token string) (*AllPositionsResponse, error) {
|
||||
return GetAllPositions(token)
|
||||
}
|
||||
Reference in New Issue
Block a user