Skip to content

Commit 3bea3cb

Browse files
authored
Merge pull request #3 from shweshi/dev
close #2 export data in json and csv
2 parents 55577e1 + d627e65 commit 3bea3cb

10 files changed

Lines changed: 113 additions & 18 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ terminal-stocks -t [ticker] --historical [domain] // to see the historical price
4949
terminal-stocks --tickers ITC.NS,INFY.NS // to get the current prices of the multiple stocks
5050
terminal-stocks -m // to see the market summary
5151
terminal-stocks --market // to see the market summary
52+
terminal-stokcs --market --json // to export the data as json file
53+
terminal-stokcs --market --csv // to expor the data as csv file
5254
```
5355

5456
### Example

app/services/csvService.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const fs = require('fs')
2+
const jsonexport = require('jsonexport');
3+
4+
module.exports = {
5+
csvExport: csvExport,
6+
};
7+
8+
function csvExport(data) {
9+
jsonexport(data, function (err, csv) {
10+
if (err) return console.error(err);
11+
fs.writeFileSync('terminal-stocks_' + Date.now() + '_.csv', csv);
12+
})
13+
}

app/services/jsonService.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require('fs')
2+
3+
module.exports = {
4+
jsonExport: jsonExport,
5+
};
6+
7+
function jsonExport(data) {
8+
fs.writeFileSync('terminal-stocks_' + Date.now() + '_.json', JSON.stringify(data));
9+
}

app/transformer/responseTransformer.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ module.exports = {
1010
transformHistoricalPrices: transformHistoricalPrices,
1111
transformMarketSummary: transformMarketSummary,
1212
transformError: transformError,
13-
transformChart: transformChart
13+
transformChart: transformChart,
14+
transformExportJsonSuccess: transformExportJsonSuccess,
15+
transformExportCsvSuccess: transformExportCsvSuccess,
1416
};
1517

1618
function transformChart(data) {
@@ -32,7 +34,7 @@ function transformChart(data) {
3234
x: time,
3335
y: data.indicators.quote[0].volume
3436
}
35-
37+
3638
screen.append(line)
3739
line.setData([linedata])
3840
screen.key(['escape', 'q', 'C-c'], function (ch, key) {
@@ -149,5 +151,13 @@ function transformHistoricalPrices(data) {
149151
}
150152

151153
function transformError(error) {
152-
return `\nSorry, we couldn't find. Please check the stock ticker and provide correct one.\n\n`;
154+
return `\nSorry, we couldn't find. Please check the stock ticker and provide correct one.\n\n ${error.message}`;
155+
}
156+
157+
function transformExportJsonSuccess() {
158+
return `\nExported to json successfully.\nStored in: ${process.cwd()}\n`;
159+
}
160+
161+
function transformExportCsvSuccess() {
162+
return `\nExported to csv successfully.\nStored in: ${process.cwd()}\n`;
153163
}

bin/index.js

100644100755
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,34 @@ var argv = require('yargs/yargs')(process.argv.slice(2))
1616
.alias('c', 'chart')
1717
.describe('c', 'Fetch chart for given ticker')
1818
.describe('historical', 'To get historical price information of the stock')
19+
.describe('json', 'To export the data as json')
20+
.describe('csv', 'To export the data csv')
1921
.help('h')
2022
.alias('h', 'help')
2123
.alias('v', 'version')
2224
.argv;
2325

26+
const options = {};
27+
28+
if (argv.json) {
29+
options.export = 'json';
30+
} else if (argv.csv) {
31+
options.export = 'csv';
32+
}
33+
2434
if (argv.tickers) {
2535
const tickers = argv.tickers.split(',');
26-
stocksCli.fetchCurrentPrice(tickers);
36+
stocksCli.fetchCurrentPrice(tickers, options);
2737
}
2838
if (argv.market) {
29-
stocksCli.fetchMarketSummary();
39+
stocksCli.fetchMarketSummary(options, options);
3040
} else if (argv.t || argv.ticker) {
3141
const ticker = argv.t || argv.ticker;
3242
if (argv.historical) {
33-
stocksCli.fetchHistoricalPrices(ticker, { page: 1, limit: 10 });
43+
stocksCli.fetchHistoricalPrices(ticker, { page: 1, limit: 10, export: options.export });
3444
} else if (argv.chart) {
3545
stocksCli.fetchChart(ticker);
3646
} else {
37-
stocksCli.fetchCurrentPrice([ticker]);
47+
stocksCli.fetchCurrentPrice([ticker], options);
3848
}
3949
}

cli/stocksCli.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var yahooService = require('../app/services/yahooService');
2+
var jsonService = require('../app/services/jsonService');
3+
var csvService = require('../app/services/csvService');
24
var responseTransformer = require('../app/transformer/responseTransformer');
35

46
module.exports = {
@@ -8,10 +10,18 @@ module.exports = {
810
fetchChart: fetchChart
911
};
1012

11-
function fetchCurrentPrice(tickers) {
13+
function fetchCurrentPrice(tickers, options = {}) {
1214
yahooService.getCurrentPrice(tickers)
1315
.then((data) => {
14-
console.log(responseTransformer.transformCurrentPrice(data));
16+
if (options.export === 'json') {
17+
jsonService.jsonExport(data);
18+
console.log(responseTransformer.transformExportJsonSuccess())
19+
} else if (options.export === 'csv') {
20+
csvService.csvExport(data);
21+
console.log(responseTransformer.transformExportCsvSuccess())
22+
} else {
23+
console.log(responseTransformer.transformCurrentPrice(data));
24+
}
1525
}).catch((error) => {
1626
console.log(responseTransformer.transformError(error));
1727
});
@@ -20,16 +30,32 @@ function fetchCurrentPrice(tickers) {
2030
function fetchHistoricalPrices(ticker, options) {
2131
yahooService.getHistoricalPrices(ticker, options)
2232
.then((data) => {
23-
console.log(responseTransformer.transformHistoricalPrices(data));
33+
if (options.export === 'json') {
34+
jsonService.jsonExport(data);
35+
console.log(responseTransformer.transformExportJsonSuccess())
36+
} else if (options.export === 'csv') {
37+
csvService.csvExport(data);
38+
console.log(responseTransformer.transformExportCsvSuccess())
39+
} else {
40+
console.log(responseTransformer.transformHistoricalPrices(data));
41+
}
2442
}).catch((error) => {
2543
console.log(responseTransformer.transformError(error));
2644
});
2745
}
2846

29-
function fetchMarketSummary() {
30-
yahooService.getMarketSummary()
47+
function fetchMarketSummary(options) {
48+
yahooService.getMarketSummary(options)
3149
.then((data) => {
32-
console.log(responseTransformer.transformMarketSummary(data));
50+
if (options.export === 'json') {
51+
jsonService.jsonExport(data);
52+
console.log(responseTransformer.transformExportJsonSuccess())
53+
} else if (options.export === 'csv') {
54+
csvService.csvExport(data);
55+
console.log(responseTransformer.transformExportCsvSuccess())
56+
} else {
57+
console.log(responseTransformer.transformMarketSummary(data));
58+
}
3359
}).catch((error) => {
3460
console.log(responseTransformer.transformError(error));
3561
});

docs/cli.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ Example:
8282
terminal-stocks --ticker ^BSENS
8383
```
8484

85+
## Export the data
86+
You can export the data as json and csv file.
87+
- To export as json
88+
```sh
89+
terminal-stocks --ticker <TICKER> --json
90+
```
91+
92+
- To export as csv
93+
```sh
94+
terminal-stocks --ticker <TICKER> --csv
95+
```
96+
97+
This works with other data also like --historical, --market or --tickers
98+
8599
---
86100

87101
?> DISCLAIMER: Use data provided by termnal-stocks for information purpose. Do not use for trading.

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "terminal-stocks",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "Fetch stock price information on terminal",
55
"main": "index.js",
66
"bin": {
7-
"terminal-stocks": "./bin/index.js"
7+
"terminal-stocks": "bin/index.js"
88
},
99
"dependencies": {
1010
"ansi_up": "^4.0.4",
@@ -14,13 +14,15 @@
1414
"cli-table3": "^0.6.0",
1515
"colors": "^1.4.0",
1616
"express": "^4.15.4",
17+
"jsonexport": "^3.2.0",
1718
"request": "^2.88.0",
1819
"yargs": "^16.1.1"
1920
},
2021
"scripts": {
2122
"test": "echo \"Error: no test specified\" && exit 1",
2223
"serve": "node server.js",
23-
"watch": "nodemon server.js"
24+
"watch": "nodemon server.js",
25+
"start": "node server.js"
2426
},
2527
"author": "Shashi Prakash Gautam <contactmespg@gmail.com>",
2628
"license": "ISC",
@@ -34,5 +36,8 @@
3436
"bugs": {
3537
"url": "https://github.com/shweshi/terminal-stocks/issues"
3638
},
37-
"homepage": "https://github.com/shweshi/terminal-stocks#readme"
39+
"homepage": "https://github.com/shweshi/terminal-stocks#readme",
40+
"directories": {
41+
"doc": "docs"
42+
}
3843
}

0 commit comments

Comments
 (0)