Skip to content

Instantly share code, notes, and snippets.

@konecnyna
Last active March 9, 2020 20:31
Show Gist options
  • Save konecnyna/600dd2ad8079e8c4d11d2674d0efa84e to your computer and use it in GitHub Desktop.
Save konecnyna/600dd2ad8079e8c4d11d2674d0efa84e to your computer and use it in GitHub Desktop.
Stocks Scrape
const request = require("request-promise");
const cheerio = require("cheerio");
const NUMBER_REGEX = "\\-?\\+?[0-9]{1,3}(,[0-9]{3})*(.[0-9]+)?";
const stockPriceRegex = () =>
new RegExp(
`Stock Price(?<price>${NUMBER_REGEX})\\s(?<day_change>${NUMBER_REGEX})\\s(?<day_percent>\\(?\\d+.\\d+%)`,
"gm"
);
const afterHoursStockPriceRegex = () =>
new RegExp(
`After hours:\\s(?<price>${NUMBER_REGEX})\\s(?<day_change>${NUMBER_REGEX})\\s(?<day_percent>\\(\\d+.\\d+%\\))`,
"gm"
);
const stockCard = {
priceRegex: stockPriceRegex,
ahRegex: afterHoursStockPriceRegex,
name: /Stock Price \| (?<name>[^.]+)/gm
};
class StocksController {
async get(ticker) {
try {
const stockPrice = await this.scrape(ticker);
return stockPrice;
} catch (e) {
res.json({
name: "N/A",
price: "N/A",
day_change: "N/A",
day_percent_change: "N/A"
});
}
}
async scrape(ticker) {
if (!ticker) {
return {};
}
const body = await this.getHtml(ticker);
return this.parseHtml(body, ticker);
}
async parseHtml(html, ticker) {
const { priceRegex } = stockCard;
const $ = cheerio.load(html);
const text = $.text();
// const name = text.getNameFromTicker(ticker);
const name = "N/A";
const match = priceRegex().exec(text);
if (!match) {
throw new Error(text);
}
const { price, day_change, day_percent } = match.groups;
return {
name: name,
price: price,
day_change: day_change,
day_percent_change: day_percent,
after_hours: this.getAfterHours(text)
};
}
async getHtml(stock) {
const options = {
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
method: "GET",
url: `https://www.google.com/search?q=${stock} stock price`
};
return await request(options);
}
getAfterHours(text) {
const { ahRegex } = stockCard;
const match = ahRegex().exec(text);
if (!match) {
return {};
}
const { price, day_change, day_percent } = match.groups;
return {
price: price,
day_change: day_change,
day_percent_change: day_percent
};
}
};
(new StocksController()).get("tsla")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment