ka1kuk commited on
Commit
176b3b2
·
1 Parent(s): 9f49b1a

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +63 -209
app.js CHANGED
@@ -1,252 +1,106 @@
1
  const express = require('express');
 
2
  const cors = require('cors');
3
- const yahooFinance = require('yahoo-finance2').default;
4
- const fetch = require('node-fetch');
5
 
6
  const app = express();
7
- // Constants
8
- const PORT = 7860;
9
- const HOST = '0.0.0.0';
10
 
11
- app.use(cors());
12
- app.use(express.json());
13
 
14
- app.get('/', (req, res) => {
15
- res.setHeader('Content-Type', 'text/plain');
16
- 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>');
17
- })
18
-
19
- app.get('/api/search/:query', async (req, res) => {
20
- const query = req.params.query;
21
- try {
22
- const results = await yahooFinance.search(query);
23
- res.json(results);
24
- } catch (error) {
25
- res.status(500).json({ message: 'Error searching stocks.' });
26
- }
27
- });
28
-
29
- app.get('/api/quote/:symbol', async (req, res) => {
30
- const symbol = req.params.symbol;
31
  try {
32
- const quote = await yahooFinance.quote(symbol);
33
- res.json(quote);
34
  } catch (error) {
35
- res.status(500).json({ message: 'Error fetching quote data.' });
36
  }
37
  });
38
 
39
- app.get('/api/chart/:symbol', async (req, res) => {
40
- const symbol = req.params.symbol;
41
- const interval = req.query.interval || '1d'; // Default to 1 day
42
- const range = req.query.range || '1y'; // Default to 1 year
43
-
44
  try {
45
- const url = `https://query1.finance.yahoo.com/v8/finance/chart/${symbol}?interval=${interval}&range=${range}`;
46
- const response = await fetch(url);
47
- const data = await response.json();
48
-
49
- if (!data.chart.result) {
50
- return res.status(404).json({ message: `No historical data found for ${symbol}` });
51
  }
52
-
53
- const timestamps = data.chart.result[0].timestamp;
54
- const closePrices = data.chart.result[0].indicators.quote[0].close;
55
- const openPrices = data.chart.result[0].indicators.quote[0].open;
56
- const highPrices = data.chart.result[0].indicators.quote[0].high;
57
- const lowPrices = data.chart.result[0].indicators.quote[0].low;
58
- const adjustClosePrices = data.chart.result[0].indicators.adjclose[0].adjclose;
59
- const volume = data.chart.result[0].indicators.quote[0].volume;
60
-
61
- const historicalData = timestamps.map((time, index) => ({
62
- date: new Date(time * 1000).toISOString().slice(0, 10), // Convert timestamp to date format
63
- open: openPrices[index],
64
- high: highPrices[index],
65
- low: lowPrices[index],
66
- close: closePrices[index],
67
- adjustclose: adjustClosePrices[index],
68
- volume: volume[index]
69
- }));
70
-
71
- res.json(historicalData.reverse());
72
-
73
  } catch (error) {
74
- console.error(`Error fetching historical data for ${symbol}:`, error);
75
- res.status(500).json({ message: 'Error fetching historical data.', error: error.message });
76
  }
77
  });
78
 
79
- app.get('/api/summary/:symbol', async (req, res) => {
80
- const symbol = req.params.symbol;
81
  try {
82
- const queryOptions = { modules: ['summaryDetail', 'summaryProfile'] };
83
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
84
- res.json(quote);
85
  } catch (error) {
86
- res.status(500).json({ message: 'Error fetching summary data.' });
87
  }
88
- })
89
-
90
- app.get('/api/assetprofile/:symbol', async (req, res) => {
91
- const symbol = req.params.symbol;
92
- try {
93
- const queryOptions = { modules: ['assetProfile'] };
94
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
95
- res.json(quote);
96
- } catch (error) {
97
- res.status(500).json({ message: 'Error fetching assetProfile data.' });
98
- }
99
- })
100
-
101
- app.get('/api/balancesheet/:symbol', async (req, res) => {
102
- const symbol = req.params.symbol;
103
- try {
104
- const queryOptions = { modules: ['balanceSheetHistory', 'balanceSheetHistoryQuarterly'] };
105
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
106
- res.json(quote);
107
- } catch (error) {
108
- res.status(500).json({ message: 'Error fetching balanceSheet data.' });
109
- }
110
- })
111
-
112
- app.get('/api/calendar/:symbol', async (req, res) => {
113
- const symbol = req.params.symbol;
114
- try {
115
- const queryOptions = { modules: ['calendarEvents'] };
116
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
117
- res.json(quote);
118
- } catch (error) {
119
- res.status(500).json({ message: 'Error fetching calendarEvents data.' });
120
- }
121
- })
122
-
123
- app.get('/api/cashflow/:symbol', async (req, res) => {
124
- const symbol = req.params.symbol;
125
- try {
126
- const queryOptions = { modules: ['cashflowStatementHistory', 'cashflowStatementHistoryQuarterly'] };
127
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
128
- res.json(quote);
129
- } catch (error) {
130
- res.status(500).json({ message: 'Error fetching cashflowstatement data.' });
131
- }
132
- })
133
-
134
- app.get('/api/statistic/:symbol', async (req, res) => {
135
- const symbol = req.params.symbol;
136
- try {
137
- const queryOptions = { modules: ['defaultKeyStatistics'] };
138
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
139
- res.json(quote);
140
- } catch (error) {
141
- res.status(500).json({ message: 'Error fetching statistic data.' });
142
- }
143
- })
144
-
145
- app.get('/api/earnings/:symbol', async (req, res) => {
146
- const symbol = req.params.symbol;
147
- try {
148
- const queryOptions = { modules: ['earnings', 'earningsHistory', 'earningsTrend'] };
149
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
150
- res.json(quote);
151
- } catch (error) {
152
- res.status(500).json({ message: 'Error fetching earnings data.' });
153
- }
154
- })
155
-
156
- app.get('/api/financial/:symbol', async (req, res) => {
157
- const symbol = req.params.symbol;
158
- try {
159
- const queryOptions = { modules: ['financialData'] };
160
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
161
- res.json(quote);
162
- } catch (error) {
163
- res.status(500).json({ message: 'Error fetching financial data.' });
164
- }
165
- })
166
-
167
- app.get('/api/fund/:symbol', async (req, res) => {
168
- const symbol = req.params.symbol;
169
- try {
170
- const queryOptions = { modules: ['fundOwnership', 'fundPerformance', 'fundProfile'] };
171
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
172
- res.json(quote);
173
- } catch (error) {
174
- res.status(500).json({ message: 'Error fetching fund data.' });
175
- }
176
- })
177
 
178
- app.get('/api/income/:symbol', async (req, res) => {
179
- const symbol = req.params.symbol;
180
  try {
181
- const queryOptions = { modules: ['incomeStatementHistory', 'incomeStatementHistoryQuarterly'] };
182
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
183
- res.json(quote);
 
 
 
184
  } catch (error) {
185
- res.status(500).json({ message: 'Error fetching incomestatement data.' });
186
  }
187
- })
188
 
189
- app.get('/api/trend/:symbol', async (req, res) => {
190
- const symbol = req.params.symbol;
191
  try {
192
- const queryOptions = { modules: ['indexTrend', 'industryTrend'] };
193
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
194
- res.json(quote);
 
 
 
195
  } catch (error) {
196
- res.status(500).json({ message: 'Error fetching trend data.' });
197
  }
198
- })
199
 
200
- app.get('/api/insider/:symbol', async (req, res) => {
201
- const symbol = req.params.symbol;
202
  try {
203
- const queryOptions = { modules: ['insiderHolders', 'insiderTransactions', 'institutionOwnership'] };
204
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
205
- res.json(quote);
206
  } catch (error) {
207
- res.status(500).json({ message: 'Error fetching insider data.' });
208
  }
209
- })
210
 
211
- app.get('/api/major/:symbol', async (req, res) => {
212
- const symbol = req.params.symbol;
213
  try {
214
- const queryOptions = { modules: ['topHoldings', 'majorDirectHolders', 'majorHoldersBreakdown'] };
215
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
216
- res.json(quote);
217
  } catch (error) {
218
- res.status(500).json({ message: 'Error fetching majorholders data.' });
219
  }
220
- })
221
-
222
 
223
- app.get('/api/netshare/:symbol', async (req, res) => {
224
- const symbol = req.params.symbol;
225
  try {
226
- const queryOptions = { modules: ['netSharePurchaseActivity', 'price'] };
227
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
228
- res.json(quote);
229
  } catch (error) {
230
- res.status(500).json({ message: 'Error fetching netshare data.' });
231
  }
232
- })
233
 
234
- app.get('/api/recommend/:symbol', async (req, res) => {
235
- const symbol = req.params.symbol;
236
- try {
237
- const queryOptions = { modules: ['recommendationTrend'] };
238
- const quote = await yahooFinance.quoteSummary(symbol, queryOptions);
239
- res.json(quote);
240
- } catch (error) {
241
- res.status(500).json({ message: 'Error fetching recommendationTrend data.' });
242
- }
243
- })
244
 
245
- app.listen(PORT, HOST, () => {
246
- if (HOST == '0.0.0.0') {
247
- console.log(`Running on http://127.0.0.1:${PORT}`);
248
- } else {
249
- console.log(`Running on http://${HOST}:${PORT}`);
250
- }
251
 
252
- });
 
 
 
 
1
  const express = require('express');
2
+ const bodyParser = require('body-parser');
3
  const cors = require('cors');
4
+ const { findAll, insert, findById, updateById, deleteById, createNewSheet, getSheetNames, deleteSheet } = require('./store');// assuming your Google Sheets script is named js
 
5
 
6
  const app = express();
7
+ const port = 3000;
 
 
8
 
9
+ app.use(bodyParser.json());
10
+ app.use(cors())
11
 
12
+ // Get all data
13
+ app.get('/data', async (req, res) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  try {
15
+ const data = await findAll();
16
+ res.status(200).json(data);
17
  } catch (error) {
18
+ res.status(500).json({ message: 'Error retrieving data', error });
19
  }
20
  });
21
 
22
+ // Get data by ID
23
+ app.get('/data/:id', async (req, res) => {
 
 
 
24
  try {
25
+ const data = await findById(req.params.id);
26
+ if (data) {
27
+ res.status(200).json(data);
28
+ } else {
29
+ res.status(404).json({ message: 'Data not found' });
 
30
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  } catch (error) {
32
+ res.status(500).json({ message: 'Error retrieving data', error });
 
33
  }
34
  });
35
 
36
+ // Insert new data
37
+ app.post('/data', async (req, res) => {
38
  try {
39
+ const insertedData = await insert(req.body);
40
+ res.status(201).json(insertedData);
 
41
  } catch (error) {
42
+ res.status(500).json({ message: 'Error inserting data', error });
43
  }
44
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ // Update data by ID
47
+ app.put('/data/:id', async (req, res) => {
48
  try {
49
+ const updatedData = await updateById(req.params.id, req.body);
50
+ if (updatedData) {
51
+ res.status(200).json(updatedData);
52
+ } else {
53
+ res.status(404).json({ message: 'Data not found' });
54
+ }
55
  } catch (error) {
56
+ res.status(500).json({ message: 'Error updating data', error });
57
  }
58
+ });
59
 
60
+ // Delete data by ID
61
+ app.delete('/data/:id', async (req, res) => {
62
  try {
63
+ const deleted = await deleteById(req.params.id);
64
+ if (deleted) {
65
+ res.status(204).send();
66
+ } else {
67
+ res.status(404).json({ message: 'Data not found' });
68
+ }
69
  } catch (error) {
70
+ res.status(500).json({ message: 'Error deleting data', error });
71
  }
72
+ });
73
 
74
+ app.post('/sheet', async (req, res) => {
 
75
  try {
76
+ const result = await createNewSheet(req.body.title);
77
+ res.status(201).send(result);
 
78
  } catch (error) {
79
+ res.status(500).json({ message: 'Error creating sheet', error });
80
  }
81
+ });
82
 
83
+ app.get('/sheets', async (req, res) => {
 
84
  try {
85
+ const sheetNames = await getSheetNames();
86
+ res.status(200).json(sheetNames);
 
87
  } catch (error) {
88
+ res.status(500).json({ message: 'Error retrieving sheet names', error: error.message });
89
  }
90
+ });
 
91
 
92
+ app.delete('/sheet/:name', async (req, res) => {
 
93
  try {
94
+ const result = await deleteSheet(req.params.name);
95
+ res.status(200).send(result);
 
96
  } catch (error) {
97
+ res.status(500).json({ message: 'Error deleting sheet', error: error.message });
98
  }
99
+ });
100
 
 
 
 
 
 
 
 
 
 
 
101
 
 
 
 
 
 
 
102
 
103
+ // Start the server
104
+ app.listen(port, () => {
105
+ console.log(`Server running on http://localhost:${port}`);
106
+ });