diff --git a/blockfolio/widget.go b/blockfolio/widget.go new file mode 100644 index 00000000..04ed4cd8 --- /dev/null +++ b/blockfolio/widget.go @@ -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) +} diff --git a/wtf.go b/wtf.go index 71443c45..d2b41a68 100644 --- a/wtf.go +++ b/wtf.go @@ -6,20 +6,17 @@ import ( "os" "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/bargraph" + "github.com/andrewzolotukhin/wtf/blockfolio" "github.com/andrewzolotukhin/wtf/clocks" "github.com/andrewzolotukhin/wtf/cmdrunner" "github.com/andrewzolotukhin/wtf/cryptoexchanges/bittrex" "github.com/andrewzolotukhin/wtf/cryptoexchanges/cryptolive" "github.com/andrewzolotukhin/wtf/gcal" - "github.com/andrewzolotukhin/wtf/gspreadsheets" "github.com/andrewzolotukhin/wtf/git" "github.com/andrewzolotukhin/wtf/github" + "github.com/andrewzolotukhin/wtf/gspreadsheets" "github.com/andrewzolotukhin/wtf/help" "github.com/andrewzolotukhin/wtf/ipinfo" "github.com/andrewzolotukhin/wtf/jira" @@ -34,6 +31,10 @@ import ( "github.com/andrewzolotukhin/wtf/todo" "github.com/andrewzolotukhin/wtf/weather" "github.com/andrewzolotukhin/wtf/wtf" + "github.com/gdamore/tcell" + "github.com/olebedev/config" + "github.com/radovskyb/watcher" + "github.com/rivo/tview" ) /* -------------------- Functions -------------------- */ @@ -210,6 +211,8 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) { Widgets = append(Widgets, todo.NewWidget(app, pages)) case "weather": Widgets = append(Widgets, weather.NewWidget(app, pages)) + case "blockfolio": + Widgets = append(Widgets, blockfolio.NewWidget(app, pages)) default: } } @@ -240,6 +243,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) { textfile.Config = Config todo.Config = Config weather.Config = Config + blockfolio.Config = Config wtf.Config = Config mods, _ := Config.Map("wtf.mods")