1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Added widget for blockfolio

This commit is contained in:
Andrew 2018-06-10 01:48:01 +03:00
parent 77823419b5
commit e633d4d617
2 changed files with 115 additions and 5 deletions

106
blockfolio/widget.go Normal file
View File

@ -0,0 +1,106 @@
package blockfolio
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/andrewzolotukhin/wtf/wtf"
"github.com/olebedev/config"
"github.com/rivo/tview"
)
// Config is a pointer to the global config object
var Config *config.Config
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", true),
device_token: Config.UString("wtf.mods.blockfolio.device_token"),
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
widget.UpdateRefreshedAt()
widget.View.SetTitle(" Blockfolio ")
positions, _ := Fetch(widget.device_token)
if _ != nil {
return
}
widget.View.SetText(fmt.Sprintf("%s", contentFrom(positions)))
}
/* -------------------- Unexported Functions -------------------- */
func contentFrom(positions AllPositionsResponse) string {
res := ""
for i := 0; i < len(positions.PositionList); i++ {
res = res + "a"
}
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)
}

14
wtf.go
View File

@ -6,20 +6,17 @@ import (
"os" "os"
"time" "time"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/radovskyb/watcher"
"github.com/rivo/tview"
"github.com/andrewzolotukhin/wtf/bamboohr" "github.com/andrewzolotukhin/wtf/bamboohr"
"github.com/andrewzolotukhin/wtf/bargraph" "github.com/andrewzolotukhin/wtf/bargraph"
"github.com/andrewzolotukhin/wtf/blockfolio"
"github.com/andrewzolotukhin/wtf/clocks" "github.com/andrewzolotukhin/wtf/clocks"
"github.com/andrewzolotukhin/wtf/cmdrunner" "github.com/andrewzolotukhin/wtf/cmdrunner"
"github.com/andrewzolotukhin/wtf/cryptoexchanges/bittrex" "github.com/andrewzolotukhin/wtf/cryptoexchanges/bittrex"
"github.com/andrewzolotukhin/wtf/cryptoexchanges/cryptolive" "github.com/andrewzolotukhin/wtf/cryptoexchanges/cryptolive"
"github.com/andrewzolotukhin/wtf/gcal" "github.com/andrewzolotukhin/wtf/gcal"
"github.com/andrewzolotukhin/wtf/gspreadsheets"
"github.com/andrewzolotukhin/wtf/git" "github.com/andrewzolotukhin/wtf/git"
"github.com/andrewzolotukhin/wtf/github" "github.com/andrewzolotukhin/wtf/github"
"github.com/andrewzolotukhin/wtf/gspreadsheets"
"github.com/andrewzolotukhin/wtf/help" "github.com/andrewzolotukhin/wtf/help"
"github.com/andrewzolotukhin/wtf/ipinfo" "github.com/andrewzolotukhin/wtf/ipinfo"
"github.com/andrewzolotukhin/wtf/jira" "github.com/andrewzolotukhin/wtf/jira"
@ -34,6 +31,10 @@ import (
"github.com/andrewzolotukhin/wtf/todo" "github.com/andrewzolotukhin/wtf/todo"
"github.com/andrewzolotukhin/wtf/weather" "github.com/andrewzolotukhin/wtf/weather"
"github.com/andrewzolotukhin/wtf/wtf" "github.com/andrewzolotukhin/wtf/wtf"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/radovskyb/watcher"
"github.com/rivo/tview"
) )
/* -------------------- Functions -------------------- */ /* -------------------- Functions -------------------- */
@ -210,6 +211,8 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) {
Widgets = append(Widgets, todo.NewWidget(app, pages)) Widgets = append(Widgets, todo.NewWidget(app, pages))
case "weather": case "weather":
Widgets = append(Widgets, weather.NewWidget(app, pages)) Widgets = append(Widgets, weather.NewWidget(app, pages))
case "blockfolio":
Widgets = append(Widgets, blockfolio.NewWidget(app, pages))
default: default:
} }
} }
@ -240,6 +243,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) {
textfile.Config = Config textfile.Config = Config
todo.Config = Config todo.Config = Config
weather.Config = Config weather.Config = Config
blockfolio.Config = Config
wtf.Config = Config wtf.Config = Config
mods, _ := Config.Map("wtf.mods") mods, _ := Config.Map("wtf.mods")