Created
April 15, 2021 18:31
-
-
Save jondcallahan/3a12e1ba264a63e63e4a936e9f7db394 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
#!/usr/bin/env deno run --allow-net | |
// Parameters | |
// Required parameters: | |
// @raycast.schemaVersion 1 | |
// @raycast.title Stocks | |
// @raycast.mode inline | |
// @raycast.refreshTime 15m | |
// Optional parameters: | |
// @raycast.icon 📈 | |
// Documentation: | |
// @raycast.author Thomas Paul Mann | |
// @raycast.authorURL https://github.com/thomaspaulmann | |
// @raycast.description Keep track of your stock portfolio. | |
const SYMBOLS = ["INTU", "ESGV", "SPY"]; | |
const reset = "\x1b[0m"; | |
const red = "\x1b[31m"; | |
const green = "\x1b[32m"; | |
const dollarFormat = | |
new Intl.NumberFormat("en-us", { style: "currency", currency: "USD" }).format; | |
const percentFormat = | |
new Intl.NumberFormat("en-us", { style: "percent", maximumFractionDigits: 2 }) | |
.format; | |
async function fetchStock(symbol: string): Promise<string> { | |
const res = await fetch(`https://api.lil.software/stocks?symbol=${symbol}`); | |
if (res.ok) { | |
const { current, previous_close: prevClose } = await res.json(); | |
// const current = body.current; | |
// const prevClose = body.previous_close; | |
const growth = ((current - prevClose) / prevClose); | |
const message = `${dollarFormat(current)} (${percentFormat(growth)})`; | |
if (growth > 0) { | |
return `${symbol}: ${green}${message}${reset}`; | |
} else if (growth < 0) { | |
return `${symbol}: ${red}${message}${reset}`; | |
} | |
return `${symbol}: ${message}`; | |
} else { | |
return "Error"; | |
} | |
} | |
(async () => { | |
const portfolio = await Promise.all( | |
SYMBOLS.map(async (symbol) => await fetchStock(symbol)), | |
); | |
console.log(portfolio.join(" ")); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment