Linance / server.js
ka1kuk's picture
Upload 3 files
6927998
raw
history blame
No virus
8.7 kB
const express = require('express');
const cors = require('cors');
const yahooFinance = require('yahoo-finance2').default;
const fetch = require('node-fetch');
const app = express();
const PORT = 3001;
app.use(cors());
app.use(express.json());
app.get('/api/search/:query', async (req, res) => {
const query = req.params.query;
try {
const results = await yahooFinance.search(query);
res.json(results);
} catch (error) {
res.status(500).json({ message: 'Error searching stocks.' });
}
});
app.get('/api/quote/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const quote = await yahooFinance.quote(symbol);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching quote data.' });
}
});
app.get('/api/historical/:symbol', async (req, res) => {
const symbol = req.params.symbol;
const interval = req.query.interval || '1d'; // Default to 1 day
const range = req.query.range || '1y'; // Default to 1 year
try {
const url = `https://query1.finance.yahoo.com/v8/finance/chart/${symbol}?interval=${interval}&range=${range}`;
const response = await fetch(url);
const data = await response.json();
if (!data.chart.result) {
return res.status(404).json({ message: `No historical data found for ${symbol}` });
}
const timestamps = data.chart.result[0].timestamp;
const closePrices = data.chart.result[0].indicators.quote[0].close;
const openPrices = data.chart.result[0].indicators.quote[0].open;
const highPrices = data.chart.result[0].indicators.quote[0].high;
const lowPrices = data.chart.result[0].indicators.quote[0].low;
const adjustClosePrices = data.chart.result[0].indicators.adjclose[0].adjclose;
const volume = data.chart.result[0].indicators.quote[0].volume;
const historicalData = timestamps.map((time, index) => ({
date: new Date(time * 1000).toISOString().slice(0, 10), // Convert timestamp to date format
open: openPrices[index],
high: highPrices[index],
low: lowPrices[index],
close: closePrices[index],
adjustclose: adjustClosePrices[index],
volume: volume[index]
}));
res.json(historicalData.reverse());
} catch (error) {
console.error(`Error fetching historical data for ${symbol}:`, error);
res.status(500).json({ message: 'Error fetching historical data.', error: error.message });
}
});
app.get('/api/summary/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['summaryDetail', 'summaryProfile'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching summary data.' });
}
})
app.get('/api/assetProfile/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['assetProfile'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching assetProfile data.' });
}
})
app.get('/api/balanceSheetHistory/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['balanceSheetHistory', 'balanceSheetHistoryQuarterly'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching balanceSheet data.' });
}
})
app.get('/api/calendar/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['calendarEvents'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching calendarEvents data.' });
}
})
app.get('/api/cashflow/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['cashflowStatementHistory', 'cashflowStatementHistoryQuarterly'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching cashflowstatement data.' });
}
})
app.get('/api/statistic/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['defaultKeyStatistics'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching statistic data.' });
}
})
app.get('/api/earnings/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['earnings', 'earningsHistory', 'earningsTrend'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching earnings data.' });
}
})
app.get('/api/financial/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['financialData'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching financial data.' });
}
})
app.get('/api/fund/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['fundOwnership', 'fundPerformance', 'fundProfile'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching fund data.' });
}
})
app.get('/api/income/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['incomeStatementHistory', 'incomeStatementHistoryQuarterly'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching incomestatement data.' });
}
})
app.get('/api/trend/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['indexTrend', 'industryTrend'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching trend data.' });
}
})
app.get('/api/insider/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['insiderHolders', 'insiderTransactions', 'institutionOwnership'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching insider data.' });
}
})
app.get('/api/major/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['topHoldings', 'majorDirectHolders', 'majorHoldersBreakdown'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching majorholders data.' });
}
})
app.get('/api/netshare/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['netSharePurchaseActivity', 'price'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching netshare data.' });
}
})
app.get('/api/recommend/:symbol', async (req, res) => {
const symbol = req.params.symbol;
try {
const queryOptions = { modules: ['recommendationTrend'] };
const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
res.json(quote);
} catch (error) {
res.status(500).json({ message: 'Error fetching recommendationTrend data.' });
}
})
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});