55 lines
1.2 KiB
JavaScript
55 lines
1.2 KiB
JavaScript
const config = require('../config');
|
|
const { fetchJson } = require('../http');
|
|
|
|
function getDateRange() {
|
|
const to = new Date();
|
|
const from = new Date(to);
|
|
from.setDate(from.getDate() - 1);
|
|
|
|
return {
|
|
from: from.toISOString().slice(0, 10),
|
|
to: to.toISOString().slice(0, 10),
|
|
};
|
|
}
|
|
|
|
async function fetchFinnhubArticles() {
|
|
if (!config.finnhub?.apiKey) {
|
|
return [];
|
|
}
|
|
|
|
const { from, to } = getDateRange();
|
|
const articles = [];
|
|
|
|
for (const symbol of config.finnhub.tickers || []) {
|
|
const params = new URLSearchParams({
|
|
symbol,
|
|
from,
|
|
to,
|
|
token: config.finnhub.apiKey,
|
|
});
|
|
|
|
const data = await fetchJson(`https://finnhub.io/api/v1/company-news?${params.toString()}`);
|
|
for (const item of data || []) {
|
|
const title = String(item.headline || '').trim();
|
|
const url = String(item.url || '').trim();
|
|
|
|
if (!title || !url) {
|
|
continue;
|
|
}
|
|
|
|
articles.push({
|
|
title,
|
|
description: item.summary || null,
|
|
url,
|
|
source: 'finnhub',
|
|
pubDate: item.datetime ? new Date(item.datetime * 1000).toISOString() : null,
|
|
});
|
|
}
|
|
}
|
|
|
|
return articles;
|
|
}
|
|
|
|
module.exports = {
|
|
fetchFinnhubArticles,
|
|
};
|