Last active
November 16, 2020 17:14
-
-
Save mutsuda/9a5abb55e38e80f66e22abc921fc5d86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Read parameter from widget | |
let parameter = args.widgetParameter | |
var parameters = ""; | |
if (parameter) parameters = parameter.split(";") | |
else | |
{ | |
// If no parameters were found, ask the user to put them | |
let w = new ListWidget() | |
error = w.addText("Introduce tu cuenta y token de IndexaCapital separados por un ; en la configuración del Widget") | |
error.font = new Font("Avenir-Book", 11) | |
Script.setWidget(w) | |
Script.complete() | |
return | |
} | |
let portfolio = await loadItems("portfolio", parameters) | |
let performance = await loadItems("performance", parameters) | |
let widget = createWidget(portfolio, performance) | |
Script.setWidget(widget) | |
Script.complete() | |
// Get the data from the API in the given endpoint | |
async function loadItems(endpoint, parameters) | |
{ | |
const url = "https://api.indexacapital.com/accounts/" + parameters[0] + "/" + endpoint | |
const request = new Request(url) | |
request.headers = { 'X-Auth-Token':parameters[1] } | |
const response = await request.loadJSON() | |
return response | |
} | |
// Create the widget with the portfolio and performance info | |
function createWidget(recportfolio, recperformance) | |
{ | |
let totalfont = new Font("Avenir-Heavy",20) | |
let retfont = new Font("Avenir-Book",17) | |
let w = new ListWidget() | |
w.backgroundColor = new Color("#123456") | |
let takes = 0 | |
let cgs = 0 | |
let total = 0 | |
total_amount = (recportfolio["portfolio"]["total_amount"]).toFixed(2).toString()+"€" | |
time_return = (recperformance["return"]["time_return"] * 100).toFixed(2).toString()+"%" | |
amount = w.addText(total_amount) | |
ret = w.addText(time_return) | |
amount.font = totalfont | |
ret.font = retfont | |
amount.textColor = Color.white() | |
ret.textColor = Color.white() | |
ret.textOpacity = 0.7 | |
w.backgroundColor = new Color("#217ec1") | |
return w | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great idea! I think you can simplify it a bit by getting the total amount from the portfolio, if I'm not mistaken:
Sorry for the
bash
, always end up using that when I'm testing something...Edit: I actually did that, plus adding the widget a small title, check it out if you're interested - https://gist.github.com/deigote/faf6fc75db9289893c538639f96e66c8 . Again, great idea, I also didn't know about scriptable which looks like fun 😄 .