Skip to content

Commit

Permalink
increased getPrice performance and bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jackHedaya committed Apr 4, 2019
1 parent 48dd751 commit b396993
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,28 @@ class RealtimeStock extends EventEmitter {

const page = await browser.newPage();

await page.goto(`https://finance.yahoo.com/quote/${stock}`);

const element = await page.$('span[data-reactid="34"]:nth-child(1)');
const handler = await element.getProperty("textContent");

const value = await handler.jsonValue();
await page.setRequestInterception(true);

page.on("request", req => {
const skip = ["stylesheet", "font", "image", "script"];
const resourceType = req.resourceType();

if (skip.includes(resourceType)) {
this.emit("debug", `Skipping resource of type '${resourceType}' while accessing ${s} price.`);
req.abort();
} else {
req.continue();
}
});

await page.close();
await page.goto(`https://finance.yahoo.com/quote/${stock}`);

return parseFloat(value.replace(/,/g, ""));
return await page.evaluate(() => {
return document
.querySelector("#quote-market-notice")
.parentElement.querySelector("span")
.textContent.replace(/,/g, "");
});
} catch (e) {
this.emit("logs", e);
this.emit("debug", `Getting ${s} price failed. See 'logs' to get the error message`);
Expand Down

0 comments on commit b396993

Please sign in to comment.