UNPKG

stonkinator

Version:

A low level stock data aggregation tool, a boring lib for others to build upon

65 lines (64 loc) 3.27 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LatestNews = void 0; const scraperutils_1 = require("../utils/scraperutils"); /** Scrapes News from MarketWatch * @param ticker - The ticker of the stock to fetch * @param options - @see {@link MarketWatchNewsOptions} for the fetch options * @returns A {@link NewsFeed} that includes the news of that specific channel * @public */ const LatestNews = (ticker, options = { channel: "MarketWatch", page: 0 }) => __awaiter(void 0, void 0, void 0, function* () { try { if (!options.page) options.page = 0; if (!options.channel) options.channel = "MarketWatch"; const $ = yield scraperutils_1.LoadSite(`https://www.marketwatch.com/investing/stock/${ticker}/moreheadlines?channel=${options.channel}&source=ChartingSymbol&pageNumber=${options.page}`); const stories = []; $(".collection__elements") .children() .each((_, el) => { var _a, _b, _c; const childElements = $(el).find(".article__content"); const articleElement = childElements.find("h3 > a"); const detailsElement = childElements.find("div"); //Additional try catch to ignore bad articles try { const data = { imageURL: (_a = $(el) .find("figure > a > img") .attr("data-srcset")) === null || _a === void 0 ? void 0 : _a.split(",").slice(-1)[0].split(" ")[0], url: articleElement.attr("href"), headline: articleElement.text().trim() || "", time: detailsElement.find(".article__timestamp").text(), author: (_b = detailsElement.find(".article__author")) === null || _b === void 0 ? void 0 : _b.text(), label: (_c = detailsElement.find(".article__label")) === null || _c === void 0 ? void 0 : _c.text(), }; stories.push(data); // eslint-disable-next-line no-empty } catch (_d) { } }); return stories; } catch (err) { if (err === "404") { return Promise.reject("Cannot find news feed"); } if (err === "Error while loading site") { return Promise.reject("Cannot load news feed"); } return Promise.reject("Error while parsing news feed"); } }); exports.LatestNews = LatestNews;