Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(route): finviz news #12857

Merged
merged 6 commits into from
Aug 3, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions lib/v2/finviz/news.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,68 @@ 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 `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');

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] ?? '',
};
const a = item.find('a.nn-tab-link');

return ctx.cache.tryGet(a.prop('href'), () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the (a.prop('href') are undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unable to reproduce locally, I think I'll remove support for caching.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's caused by Ads which are only visible to RSSHub.

Feed screenshot

image

Which can be solved by:

            const descriptionMatches = a
                .parent()
                .prop('data-boxover')
-                .match(/<td class='news_tooltip-tab'>(.*?)<\/td>/);
+                ?.match(/<td class='news_tooltip-tab'>(.*?)<\/td>/);
            const authorMatches = item
                .find('use')
                .first()
                .prop('href')
-                .match(/#(.*?)-(light|dark)/);
+                ?.match(/#(.*?)-(light|dark)/);

and a filter like .filter((item) => item.title)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. 😄

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.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),
};
});
});

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(),
};
};