diff --git a/lib/v2/finviz/news.js b/lib/v2/finviz/news.js index 961676f2d..7ba638992 100644 --- a/lib/v2/finviz/news.js +++ b/lib/v2/finviz/news.js @@ -3,43 +3,67 @@ const cheerio = require('cheerio'); const timezone = require('@/utils/timezone'); const { parseDate } = require('@/utils/parse-date'); +const categories = { + news: 0, + blogs: 1, +}; + module.exports = async (ctx) => { - const category = ctx.params.category ?? 'news'; + const { category = 'News' } = ctx.params; + const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 200; + + if (!categories.hasOwnProperty(category.toLowerCase())) { + throw Error(`No category '${category}'.`); + } const rootUrl = 'https://finviz.com'; - const currentUrl = `${rootUrl}/news.ashx`; + const currentUrl = new URL('news.ashx', rootUrl).href; - const response = await got({ - method: 'get', - url: currentUrl, - }); + const { data: response } = await got(currentUrl); - const $ = cheerio.load(response.data); + const $ = cheerio.load(response); - const items = $('.table-fixed') - .eq(category === 'blog' ? 1 : 0) - .find('.nn') + const items = $('table.table-fixed') + .eq(categories[category.toLowerCase()]) + .find('tr.nn') + .slice(0, limit) .toArray() .map((item) => { item = $(item); - const a = item.find('.nn-tab-link'); + const a = item.find('a.nn-tab-link'); + + const descriptionMatches = a + .parent() + .prop('data-boxover') + ?.match(/