From be606d38e026169fefc501fe120fdb944819e8d7 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Thu, 3 Aug 2023 21:30:09 +0800 Subject: [PATCH] fix(route): finviz news (#12857) * fix(route): finviz news * fix: remove unnecessary escape characters * fix: remove cache * fix: add filter of title * fix: filter empty value --------- --- lib/v2/finviz/news.js | 64 +++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 20 deletions(-) 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(/