google-stock-realtime
Advanced tools
Comparing version 1.0.3 to 1.1.0
53
index.js
@@ -88,3 +88,3 @@ 'use strict'; | ||
const symbolPostfix = ["", ":NYSE", ":NASDAQ"]; | ||
const symbolPostfix = [":NASDAQ", ":NYSE"]; | ||
symbol = symbol.toUpperCase(); | ||
@@ -96,3 +96,3 @@ let fetchError = null; | ||
try { | ||
const rsp = await page.goto('https://www.google.com/finance/quote/' + symbol + postfix ); | ||
const rsp = await page.goto('https://www.google.com/finance/quote/' + symbol + postfix); | ||
@@ -132,3 +132,52 @@ if (rsp.status() !== 200) { | ||
const getMultipleStockLastPrice = async (symbol) => { | ||
const browser = await puppeteer.launch({ args: ["--no-sandbox"] }); | ||
const page = await browser.newPage(); | ||
await page.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"); | ||
const symbolPostfix = ":NASDAQ"; | ||
// symbol = symbol.toUpperCase(); | ||
let fetchError = null; | ||
let resp = {}; | ||
for (let stock of symbol) { | ||
stock = stock.toUpperCase(); | ||
resp[stock] = {}; | ||
try { | ||
const rsp = await page.goto('https://www.google.com/finance/quote/' + stock + symbolPostfix); | ||
if (rsp.status() !== 200) { | ||
continue; | ||
} | ||
const stockData = { stock }; | ||
const handles = await page.$$('.rPF6Lc'); // The div encapsulating all three details | ||
for (const element of handles) { | ||
const realtimePrice = await page.evaluate(el => el.querySelector("div > span > div > div").textContent, element); | ||
stockData.realtimePrice = realtimePrice; | ||
const profitLossPercentage = await page.evaluate(el => el.querySelector("div > div:nth-child(2) > div > span > div").textContent, element); | ||
stockData.profitLossPercentage = profitLossPercentage; | ||
const profitLossToday = await page.evaluate(el => el.querySelector("div > div:nth-child(2) > div > span:nth-child(2)").textContent, element); | ||
stockData.profitLossToday = profitLossToday; | ||
} | ||
resp[stock] = stockData; | ||
} | ||
catch (err) { | ||
fetchError = err; | ||
} | ||
} | ||
browser.close(); | ||
if(Object.keys(resp).length > 0){ | ||
return resp; | ||
}else{ | ||
throw new Error(`unable to fetch data for ${symbol}`, fetchError); | ||
} | ||
}; | ||
exports.getStockOverview = getStockOverview; | ||
exports.getStockLastPrice = getStockLastPrice; | ||
exports.getMultipleStockLastPrice = getMultipleStockLastPrice; |
{ | ||
"name": "google-stock-realtime", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Real time stock information from trusted google source", | ||
@@ -31,3 +31,6 @@ "main": "index.js", | ||
"Profit Loss Percentage", | ||
"Today value" | ||
"Today value", | ||
"Stock Realtime", | ||
"real-time stock", | ||
"Multiple Stock" | ||
], | ||
@@ -34,0 +37,0 @@ "author": "Vinay Guda <vinayguda05@gmail.com> (https://gudavinay.github.io/)", |
@@ -48,2 +48,36 @@ # google-finance-data | ||
*/ | ||
realtimeStock.getMultipleStockLastPrice(["AAPL","TSLA","MSFT","NVDA"]) | ||
.then(data => console.log(JSON.stringify(data, null, 2))) | ||
.catch(err => console.error(err.stack ? err.stack : err)); | ||
/* | ||
Output: | ||
{ | ||
"AAPL": { | ||
"stock": "AAPL", | ||
"realtimePrice": "$150.72", | ||
"profitLossPercentage": "1.30%", | ||
"profitLossToday": "+1.93 Today" | ||
}, | ||
"TSLA": { | ||
"stock": "TSLA", | ||
"realtimePrice": "$183.17", | ||
"profitLossPercentage": "2.01%", | ||
"profitLossToday": "-3.75 Today" | ||
}, | ||
"MSFT": { | ||
"stock": "MSFT", | ||
"realtimePrice": "$241.68", | ||
"profitLossPercentage": "0.021%", | ||
"profitLossToday": "-0.050 Today" | ||
}, | ||
"NVDA": { | ||
"stock": "NVDA", | ||
"realtimePrice": "$156.77", | ||
"profitLossPercentage": "1.46%", | ||
"profitLossToday": "-2.33 Today" | ||
} | ||
} | ||
*/ | ||
``` | ||
@@ -50,0 +84,0 @@ |
11075
142
85