Skip to content

Commit

Permalink
fix(route): finviz news (DIYgod#12857)
Browse files Browse the repository at this point in the history
* fix(route): finviz news

* fix: remove unnecessary escape characters

* fix: remove cache

* fix: add filter of title

* fix: filter empty value

---------
  • Loading branch information
nczitzk committed Aug 26, 2023
1 parent dae8c4f commit 59eb44f
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions lib/v2/finviz/news.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/<td class='news_tooltip-tab'>(.*?)<\/td>/);
const authorMatches = item
.find('use')
.first()
.prop('href')
?.match(/#(.*?)-(light|dark)/);

return {
title: a.text(),
link: a.attr('href'),
pubDate: timezone(parseDate(item.find('.nn-date').text(), ['MMM-DD', 'HH:mmA']), -4),
description:
item
.find('td[data-boxover]')
.attr('data-boxover')
?.match(/<td class='news_tooltip-tab'>([\s\S]*)<\/td>/)[1] ?? '',
link: a.prop('href'),
description: descriptionMatches ? descriptionMatches[1] : undefined,
author: authorMatches ? authorMatches[1].replace(/-/g, ' ') : 'finviz',
pubDate: timezone(parseDate(item.find('td.nn-date').text(), ['HH:mmA', 'MMM-DD']), -4),
};
});
})
.filter((item) => item.title);

const icon = $('link[rel="icon"]').prop('href');

ctx.state.data = {
item: items,
title: `finviz - ${category}`,
link: currentUrl,
item: items,
description: $('meta[name="description"]').prop('content'),
language: 'en-US',
image: new URL($('a.logo svg use').first().prop('href'), rootUrl).href,
icon,
logo: icon,
subtitle: $('title').text(),
};
};

0 comments on commit 59eb44f

Please sign in to comment.