|
const express = require('express'); |
|
const cors = require('cors'); |
|
const yahooFinance = require('yahoo-finance2').default; |
|
const fetch = require('node-fetch'); |
|
|
|
const app = express(); |
|
|
|
const PORT = 7860; |
|
const HOST = '0.0.0.0'; |
|
|
|
app.use(cors()); |
|
app.use(express.json()); |
|
|
|
app.get('/', (req, res) => { |
|
res.setHeader('Content-Type', 'text/plain'); |
|
res.end('This is Yahoo finance api \n\nSearch : /api/search/:<symbol/name>\nQuote : /api/quote/:<symbol>\nChart : /api/chart/:<symbol>?interval=<interval>&range=<range>\nSummary : /api/summary/:<symbol>\nAssetProfile : /api/assetprofile/:<symbol>\nBalancesheet : /api/balancesheet/:<symbol>\nCalendar events : /api/calendar/:<symbol>\nCashflow statements : /api/cashflow/:<symbol>'); |
|
}) |
|
|
|
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/chart/:symbol', async (req, res) => { |
|
const symbol = req.params.symbol; |
|
const interval = req.query.interval || '1d'; |
|
const range = req.query.range || '1y'; |
|
|
|
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), |
|
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/getall/:symbol', async (req, res) => { |
|
const symbol = req.params.symbol |
|
try { |
|
const queryOptions = { modules: ['defaultKeyStatistics', 'summaryDetail', 'earnings', 'earningsHistory', 'earningsTrend', 'assetProfile', 'balanceSheetHistory', 'balanceSheetHistoryQuarterly', 'calendarEvents', 'cashflowStatementHistory', 'cashflowStatementHistoryQuarterly', 'summaryProfile', 'financialData', 'fundOwnership', 'fundPerformance', 'fundProfile', 'incomeStatementHistory', 'incomeStatementHistoryQuarterly', 'indexTrend', 'industryTrend', 'netSharePurchaseActivity', 'price'] }; |
|
const quote = await yahooFinance.quoteSummary(symbol, queryOptions); |
|
res.json(quote); |
|
} catch (error) { |
|
res.status(500).json({ message: 'Error fetching summary data.' }); |
|
} |
|
}) |
|
|
|
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/balancesheet/: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, HOST, () => { |
|
if (HOST == '0.0.0.0') { |
|
console.log(`Running on http://127.0.0.1:${PORT}`); |
|
} else { |
|
console.log(`Running on http://${HOST}:${PORT}`); |
|
} |
|
|
|
}); |
|
|