-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
228 lines (197 loc) · 6.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import axios from "axios";
import chalk from "chalk";
import Table from "cli-table3";
import { program } from "commander";
import ora from "ora";
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Configuration
const OUTPUT_DIR = path.join(__dirname, "dataset");
// Helper Functions
const getTimer = () =>
new Date().toISOString().replace(/T/, " ").replace(/\..+/, "");
const formatFloat = (x, precision = 8) =>
Number(x)
.toFixed(precision)
.replace(/\.?0+$/, "");
async function ensureOutputDir() {
try {
await fs.access(OUTPUT_DIR);
} catch {
await fs.mkdir(OUTPUT_DIR, { recursive: true });
}
// Clean up old files
const files = await fs.readdir(OUTPUT_DIR);
for (const file of files) {
await fs.unlink(path.join(OUTPUT_DIR, file));
}
}
async function getMarketData(coins) {
try {
if (coins.length <= 5) {
// Use individual API calls for better performance with fewer coins
const promises = coins.map((coin) =>
axios.get(
`https://api.kucoin.com/api/v1/market/stats?symbol=${coin}-USDT`
)
);
const responses = await Promise.all(promises);
return responses.map((response) => ({
symbol: response.data.data.symbol,
last: response.data.data.last,
high: response.data.data.high,
low: response.data.data.low,
changeRate: response.data.data.changeRate,
}));
} else {
// Use allTickers endpoint for many coins
const { data } = await axios.get(
"https://api.kucoin.com/api/v1/market/allTickers"
);
return data.data.ticker;
}
} catch (error) {
console.error(chalk.red("Error fetching market data:", error.message));
return null;
}
}
function getCoinDetails(coin, tickers) {
const ticker = Array.isArray(tickers)
? tickers.find((t) => t.symbol === `${coin}-USDT`)
: tickers;
if (!ticker) return null;
return {
symbol: `${coin}-USDT`,
lastPrice: formatFloat(ticker.last),
highPrice24h: formatFloat(ticker.high),
lowPrice24h: formatFloat(ticker.low),
changeRate: formatFloat(ticker.changeRate * 100, 2),
lastUpdated: getTimer(),
};
}
async function writeToCSV(data, coin) {
const date = new Date().toISOString().split("T")[0];
const filename = path.join(OUTPUT_DIR, `${coin}_${date}.csv`);
const content = `${data.symbol},${data.lastPrice},${data.highPrice24h},${data.lowPrice24h},${data.changeRate},${data.lastUpdated}\n`;
try {
await fs.appendFile(filename, content);
} catch (error) {
console.error(
chalk.red(`Error writing to CSV for ${coin}:`, error.message)
);
}
}
async function displayPrices(coins, delay = 30) {
console.log(chalk.yellow("Press Ctrl+C to stop the tracker\n"));
const spinner = ora("Fetching market data...");
while (true) {
try {
spinner.start();
const tickers = await getMarketData(coins);
if (!tickers) {
spinner.fail("Failed to fetch data. Retrying...");
await new Promise((resolve) => setTimeout(resolve, 5000));
continue;
}
console.clear();
const table = new Table({
head: [
"Coin",
"Price",
"High (24h)",
"Low (24h)",
"Change %",
"Updated",
],
style: { head: ["cyan"] },
chars: {
top: "─",
"top-mid": "┬",
"top-left": "┌",
"top-right": "┐",
bottom: "─",
"bottom-mid": "┴",
"bottom-left": "└",
"bottom-right": "┘",
left: "│",
"left-mid": "├",
mid: "─",
"mid-mid": "┼",
right: "│",
"right-mid": "┤",
middle: "│",
},
});
const selectedCoins = coins.map((coin) => coin.toUpperCase());
for (const coin of selectedCoins) {
const data = getCoinDetails(coin, tickers);
if (!data) continue;
const changeColor =
parseFloat(data.changeRate) >= 0 ? chalk.green : chalk.red;
table.push([
chalk.yellow(data.symbol),
data.lastPrice,
chalk.green(data.highPrice24h),
chalk.red(data.lowPrice24h),
changeColor(data.changeRate + "%"),
chalk.gray(data.lastUpdated),
]);
await writeToCSV(data, coin);
}
spinner.stop();
console.log(table.toString());
// Add countdown timer and optimization message
console.log(
chalk.gray(
"\nOptimized API calls: Using individual endpoints for better performance"
)
);
for (let remaining = delay; remaining > 0; remaining--) {
process.stdout.write(
`\r${chalk.cyan(`Next update in ${remaining} seconds...`)}`
);
await new Promise((resolve) => setTimeout(resolve, 1000));
}
process.stdout.write("\r" + " ".repeat(50) + "\r");
console.log(chalk.gray("\nPress Ctrl+C to stop the tracker"));
} catch (error) {
spinner.fail(chalk.red("Error:", error.message));
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}
}
// CLI Setup
program
.version("1.0.0")
.description("Cryptocurrency price tracker with real-time updates")
.option(
"-c, --coins <symbols>",
"Comma-separated list of coin symbols (e.g., BTC,ETH,XRP)"
)
.option("-d, --delay <seconds>", "Update delay in seconds", "30")
.option("-o, --output <dir>", "Output directory for CSV files", "dataset")
.addHelpText(
"after",
`
Examples:
$ node index.js -c BTC,ETH # Track only Bitcoin and Ethereum
$ node index.js -d 60 # Update every 60 seconds
$ node index.js -o custom_dir # Custom output directory
`
);
program.parse();
const options = program.opts();
if (!options.coins) {
console.error(
chalk.red("Error: Please specify coins to track using -c or --coins option")
);
console.log("Example: node index.js -c BTC,ETH");
process.exit(1);
}
const selectedCoins = options.coins.split(",");
const delay = parseInt(options.delay);
// Main execution
await ensureOutputDir();
await displayPrices(selectedCoins, delay);