google-stock-realtime
Advanced tools
Comparing version 1.0.2 to 1.0.3
60
index.js
@@ -6,3 +6,3 @@ 'use strict'; | ||
const getStockOverview = async (symbol) => { | ||
const browser = await puppeteer.launch({args: ["--no-sandbox"]}); | ||
const browser = await puppeteer.launch({ args: ["--no-sandbox"] }); | ||
@@ -42,3 +42,3 @@ const page = await browser.newPage(); | ||
switch(field){ | ||
switch (field) { | ||
case 'Open': stockData.open = parseNumber(value); break; | ||
@@ -55,4 +55,4 @@ case 'High': stockData.high = parseNumber(value); break; | ||
} | ||
if(field.includes("CDP score")) | ||
if (field.includes("CDP score")) | ||
stockData.cdpScore = value; | ||
@@ -80,6 +80,56 @@ | ||
function parseNumber(value) { | ||
const number = (value === "-") ? null : 1 * value; | ||
const number = (value === "-") ? null : 1 * value; | ||
return number; | ||
} | ||
const getStockLastPrice = 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 = ["", ":NYSE", ":NASDAQ"]; | ||
symbol = symbol.toUpperCase(); | ||
let fetchError = null; | ||
for (let postfix of symbolPostfix) { | ||
try { | ||
const rsp = await page.goto('https://www.google.com/finance/quote/' + symbol + postfix ); | ||
if (rsp.status() !== 200) { | ||
continue; | ||
} | ||
const stockData = { symbol }; | ||
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; | ||
} | ||
if (Object.keys(stockData).length !== 4) { | ||
continue; | ||
} | ||
browser.close(); | ||
return stockData; | ||
} | ||
catch (err) { | ||
fetchError = err; | ||
} | ||
} | ||
browser.close(); | ||
throw new Error(`unable to fetch data for ${symbol}`, fetchError); | ||
}; | ||
exports.getStockOverview = getStockOverview; | ||
exports.getStockLastPrice = getStockLastPrice; |
{ | ||
"name": "google-stock-realtime", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Real time stock information from trusted google source", | ||
@@ -10,20 +10,24 @@ "main": "index.js", | ||
"keywords": [ | ||
"GoogleStocks", | ||
"Google Stocks", | ||
"Stocks", | ||
"RealtimeAPI", | ||
"Realtime API", | ||
"Realtime", | ||
"API", | ||
"GoogleStockAPI", | ||
"GoogleStocksAPI", | ||
"Google Stock API", | ||
"Google Stocks API", | ||
"API", | ||
"RealtimeStockData", | ||
"RealtimeStock", | ||
"FreeStockData", | ||
"FreeStockAPI", | ||
"StockInformation", | ||
"StockAPI", | ||
"Realtime Stock Data", | ||
"Realtime Stock", | ||
"Free Stock Data", | ||
"Free Stock API", | ||
"Stock Information", | ||
"Stock API", | ||
"Stock", | ||
"FreeAPI", | ||
"GoogleAPI", | ||
"FinanceAPI" | ||
"Free API", | ||
"Google API", | ||
"Finance API", | ||
"Latest Stock Price", | ||
"Latest Stock Profit Loss Percentage", | ||
"Profit Loss Percentage", | ||
"Today value" | ||
], | ||
@@ -30,0 +34,0 @@ "author": "Vinay Guda <vinayguda05@gmail.com> (https://gudavinay.github.io/)", |
# google-finance-data | ||
Google Realtime Stock Data API for real-time stock quotes from Google Finance | ||
Google Realtime Stock Data API for real-time stock quotes from Google Finance. | ||
@@ -34,2 +34,16 @@ We fetch information from the results posted in Google search page. | ||
*/ | ||
realtimeStock.getStockLastPrice("AAPL") | ||
.then(data => console.log(JSON.stringify(data, null, 2))) | ||
.catch(err => console.error(err.stack ? err.stack : err)); | ||
/* | ||
Output: | ||
{ | ||
"symbol": "AAPL", | ||
"realtimePrice": "$150.72", | ||
"profitLossPercentage": "1.30%", | ||
"profitLossToday": "+1.93 Today" | ||
} | ||
*/ | ||
``` | ||
@@ -36,0 +50,0 @@ |
8294
102
51