Created
May 3, 2020 20:37
-
-
Save DanEdens/499924644d9216b2e27149c8e83cbb7d to your computer and use it in GitHub Desktop.
function to get stock price based on symbol
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
function getStockQuote(symbol) {const url = `https://finance.google.com/finance?q=${symbol}&output=json`got(url).then(response => {/* Must remove the first three characters of the Google finance text returned to parse JSON.*/const stock = JSON.parse(response.body.substr(3))const quote = stock[0]console.log(`${quote.t} ${quote.l} ${quote.c} (${quote.cp}%)`)}).catch(error => {console.log(error.response.body)})} |
//module.exports style
function getStockQuote(symbol) {const url = https://finance.google.com/finance?q=${symbol}&output=json
got(url).then(response => {/* Must remove the first three characters of the Google finance text returned to parse JSON.*/const stock = JSON.parse(response.body.substr(3))const quote = stock[0]console.log(${quote.t} ${quote.l} ${quote.c} (${quote.cp}%)
)}).catch(error => {console.log(error.response.body)})}
getStockQuote: getStockQuote,
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
export async function getStockQuote(symbol) {const url =
https://finance.google.com/finance?q=${symbol}&output=json
got(url).then(response => {/* Must remove the first three characters of the Google finance text returned to parse JSON.*/const stock = JSON.parse(response.body.substr(3))const quote = stock[0]console.log(${quote.t} ${quote.l} ${quote.c} (${quote.cp}%)
)}).catch(error => {console.log(error.response.body)})}