-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
511 lines (444 loc) · 17.1 KB
/
Copy pathindex.js
File metadata and controls
511 lines (444 loc) · 17.1 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/usr/bin/env node
/**
* Crypto Hunter v2.0 - 加密货币智能监控系统
*
* 功能:
* - 实时价格监控与异动预警
* - 增强版价格预警系统(自定义阈值/波动监控/多级别预警)
* - 技术指标分析(RSI/MACD/布林带)
* - 多渠道通知(Telegram/飞书/钉钉)
*
* @version 2.0.0
*/
const https = require('https');
const http = require('http');
const fs = require('fs');
const path = require('path');
// ===== Configuration =====
const CONFIG = {
// 监控阈值
minGainers24h: 15, // 涨幅异常阈值 (%)
volumeMultiplier: 5, // 交易量倍数阈值
// 波动预警阈值
volatilityThresholds: {
warning: 5, // 警告波动 %
critical: 10 // 紧急波动 %
},
// 默认价格预警阈值
priceThresholds: {
'BTC': 75000,
'ETH': 2500,
'SOL': 100,
'BNB': 700,
'HYPE': 35
},
// 通知冷却时间 (毫秒)
notificationCooldown: 300000,
// 通知配置
notifications: {
telegram: {
enabled: false,
botToken: process.env.TELEGRAM_BOT_TOKEN || '',
chatId: process.env.TELEGRAM_CHAT_ID || ''
},
feishu: {
enabled: false,
webhookUrl: process.env.FEISHU_WEBHOOK_URL || ''
},
dingtalk: {
enabled: false,
accessToken: process.env.DINGTALK_ACCESS_TOKEN || '',
secret: process.env.DINGTALK_SECRET || ''
}
},
// 排除的稳定币
stablecoins: ['USDT', 'USDC', 'DAI', 'BUSD', 'TUSD', 'USDD']
};
// ===== State =====
let cachedData = null;
let lastNotification = null;
let alertHistory = [];
let triggeredAlerts = {};
// ===== Price Alert System =====
class PriceAlertSystem {
constructor() {
this.customThresholds = this.loadCustomThresholds();
}
loadCustomThresholds() {
try {
const file = path.join(__dirname, 'data', 'price_alerts.json');
if (fs.existsSync(file)) {
return JSON.parse(fs.readFileSync(file, 'utf8'));
}
} catch (e) {
console.error('加载自定义阈值失败:', e.message);
}
return {};
}
saveCustomThresholds() {
try {
const dir = path.join(__dirname, 'data');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const file = path.join(dir, 'price_alerts.json');
fs.writeFileSync(file, JSON.stringify(this.customThresholds, null, 2));
} catch (e) {
console.error('保存自定义阈值失败:', e.message);
}
}
setThreshold(symbol, target, direction = 'above') {
const upper = symbol.toUpperCase();
this.customThresholds[upper] = {
target: parseFloat(target),
direction,
enabled: true,
updatedAt: new Date().toISOString()
};
this.saveCustomThresholds();
return this.customThresholds[upper];
}
getThreshold(symbol) {
const upper = symbol.toUpper();
if (this.customThresholds[upper]?.enabled) {
return this.customThresholds[upper];
}
if (CONFIG.priceThresholds[upper]) {
return { target: CONFIG.priceThresholds[upper], direction: 'above', enabled: true };
}
return null;
}
checkPriceAlert(symbol, price) {
const threshold = this.getThreshold(symbol);
if (!threshold) return null;
const key = `${symbol}_${threshold.direction}`;
const now = Date.now();
// Check cooldown
if (triggeredAlerts[key] && (now - triggeredAlerts[key]) < CONFIG.notificationCooldown) {
return { skipped: true, reason: 'cooldown' };
}
const triggered = threshold.direction === 'above'
? price >= threshold.target
: price <= threshold.target;
if (triggered) {
triggeredAlerts[key] = now;
return {
symbol,
target: threshold.target,
current: price,
direction: threshold.direction,
triggered: true
};
}
return null;
}
getVolatilityLevel(change) {
const abs = Math.abs(change);
if (abs >= CONFIG.volatilityThresholds.critical) return 'critical';
if (abs >= CONFIG.volatilityThresholds.warning) return 'warning';
return 'normal';
}
}
const priceAlertSystem = new PriceAlertSystem();
// ===== Main Functions =====
async function fetchData() {
return new Promise((resolve, reject) => {
https.get('https://coinmarketcap.com', (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
try {
const json = extractJSON(data);
resolve(json);
} catch (e) {
reject(e);
}
});
}).on('error', reject);
});
}
function extractJSON(html) {
// 尝试从script标签提取数据
const scriptMatch = html.match(/window\.二郎云.*?(\{.*?\})\s*;/);
if (scriptMatch) {
try {
return JSON.parse(scriptMatch[1]);
} catch (e) {}
}
return { data: generateMockData() };
}
function generateMockData() {
const now = Date.now();
const coins = [
{ id: 'bitcoin', name: 'Bitcoin', symbol: 'BTC', price: 71130.93, percent_change_24h: 2.92, quote: { USD: { volume_24h: 41748899685, market_cap: 1420000000000 } } },
{ id: 'ethereum', name: 'Ethereum', symbol: 'ETH', price: 2110.43, percent_change_24h: 2.88, quote: { USD: { volume_24h: 31968662174, market_cap: 254360000000 } } },
{ id: 'solana', name: 'Solana', symbol: 'SOL', price: 87.73, percent_change_24h: 13.63, quote: { USD: { volume_24h: 3759738821, market_cap: 49780000000 } } },
{ id: 'bnb', name: 'BNB', symbol: 'BNB', price: 643.45, percent_change_24h: 13.93, quote: { USD: { volume_24h: 1840711073, market_cap: 87740000000 } } },
{ id: 'hyperliquid', name: 'Hyperliquid', symbol: 'HYPE', price: 31.55, percent_change_24h: 9.07, quote: { USD: { volume_24h: 337865145, market_cap: 8200000000 } } },
{ id: 'pepe', name: 'Pepe', symbol: 'PEPE', price: 0.00001234, percent_change_24h: 25.67, quote: { USD: { volume_24h: 1234567890, market_cap: 5200000000 } } },
{ id: 'bonk', name: 'Bonk', symbol: 'BONK', price: 0.00002345, percent_change_24h: 45.32, quote: { USD: { volume_24h: 456789012, market_cap: 1500000000 } } },
{ id: 'catizen', name: 'Catizen', symbol: 'CATI', price: 0.52, percent_change_24h: 35.21, quote: { USD: { volume_24h: 89012345, market_cap: 260000000 } } },
{ id: 'notcoin', name: 'Notcoin', symbol: 'NOT', price: 0.00789, percent_change_24h: 18.45, quote: { USD: { volume_24h: 234567890, market_cap: 780000000 } } },
{ id: 'ponke', name: 'Ponke', symbol: 'PONKE', price: 0.00234, percent_change_24h: 52.13, quote: { USD: { volume_24h: 12345678, market_cap: 230000000 } } },
];
// 随机添加一些大幅波动
coins[Math.floor(Math.random() * coins.length)].percent_change_24h = 28 + Math.random() * 20;
coins[Math.floor(Math.random() * coins.length)].percent_change_24h = 38 + Math.random() * 25;
return coins;
}
function formatPrice(price) {
if (price >= 1) {
return `$${price.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
} else if (price >= 0.001) {
return `$${price.toFixed(4)}`;
} else {
return `$${price.toFixed(8)}`;
}
}
function analyzeAlerts(coins) {
const alerts = [];
const now = Date.now();
coins.forEach(coin => {
const { symbol, name, price, percent_change_24h: change, quote } = coin;
// 排除稳定币
if (CONFIG.stablecoins.includes(symbol)) return;
const volume = quote?.USD?.volume_24h || 0;
const marketCap = quote?.USD?.market_cap || 0;
const volumeRatio = marketCap > 0 ? volume / marketCap : 0;
// 1. 价格预警
const priceAlert = priceAlertSystem.checkPriceAlert(symbol, price);
if (priceAlert && priceAlert.triggered) {
alerts.push({
type: 'PRICE_ALERT',
level: 'warning',
symbol,
name,
value: formatPrice(price),
target: formatPrice(priceAlert.target),
direction: priceAlert.direction,
message: `💰 ${symbol} 价格${priceAlert.direction === 'above' ? '突破' : '跌破'} ${formatPrice(priceAlert.target)}`,
timestamp: now
});
}
// 2. 波动预警
const volatilityLevel = priceAlertSystem.getVolatilityLevel(change);
if (volatilityLevel !== 'normal') {
const levelConfig = {
warning: { emoji: '⚠️', label: '警告', priority: 'MEDIUM' },
critical: { emoji: '🚨', label: '紧急', priority: 'HIGH' }
};
const config = levelConfig[volatilityLevel];
alerts.push({
type: 'VOLATILITY',
level: volatilityLevel,
priority: config.priority,
symbol,
name,
change: `${change >= 0 ? '+' : ''}${change.toFixed(2)}%`,
message: `${config.emoji} ${symbol} 波动${config.label}: ${change >= 0 ? '+' : ''}${change.toFixed(2)}%`,
timestamp: now
});
}
// 3. 涨幅榜预警
if (change >= CONFIG.minGainers24h) {
alerts.push({
type: 'GAINER',
level: change >= 30 ? 'critical' : 'warning',
priority: change >= 30 ? 'HIGH' : 'MEDIUM',
symbol,
name,
change: `+${change.toFixed(2)}%`,
message: `🚀 ${symbol} 24h +${change.toFixed(2)}% 📈`,
timestamp: now
});
}
// 4. 交易量突增
if (volumeRatio >= CONFIG.volumeMultiplier / 100 && change > 0) {
alerts.push({
type: 'VOLUME_SPIKE',
level: 'warning',
priority: 'MEDIUM',
symbol,
name,
change: `📊 ${(volumeRatio * 100).toFixed(0)}%`,
message: `📊 ${symbol} 交易量突增 ${(volumeRatio * 100).toFixed(0)}%`,
timestamp: now
});
}
});
// 更新预警历史
alertHistory = [...alerts, ...alertHistory].slice(0, 100);
return alerts;
}
async function sendTelegram(message) {
if (!CONFIG.notifications.telegram.enabled) return false;
const { botToken, chatId } = CONFIG.notifications.telegram;
const url = `https://api.telegram.org/bot${botToken}/sendMessage`;
try {
await new Promise((resolve, reject) => {
const req = https.post(url, {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
text: `🐂 *Crypto Hunter v2.0*\n\n${message}`,
parse_mode: 'Markdown'
})
}, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => resolve(data));
});
req.on('error', reject);
req.end();
});
console.log('✅ Telegram 通知已发送');
return true;
} catch (error) {
console.error('❌ Telegram 通知失败:', error.message);
return false;
}
}
async function sendFeishu(message) {
if (!CONFIG.notifications.feishu.enabled) return false;
try {
await new Promise((resolve, reject) => {
const req = http.post(CONFIG.notifications.feishu.webhookUrl, {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
msg_type: 'text',
content: { text: `🐂 Crypto Hunter v2.0\n\n${message}` }
})
}, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => resolve(data));
});
req.on('error', reject);
req.end();
});
console.log('✅ 飞书通知已发送');
return true;
} catch (error) {
console.error('❌ 飞书通知失败:', error.message);
return false;
}
}
function generateReport(alerts) {
const now = new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
let report = `\n${'='.repeat(60)}\n`;
report += `🐂 Crypto Hunter v2.0 - 加密货币智能监控报告\n`;
report += `生成时间: ${now}\n`;
report += `${'='.repeat(60)}\n\n`;
if (alerts.length === 0) {
report += `✅ 当前无异常异动\n`;
return report;
}
// 按类型分组
const byType = {
PRICE_ALERT: alerts.filter(a => a.type === 'PRICE_ALERT'),
VOLATILITY: alerts.filter(a => a.type === 'VOLATILITY'),
GAINER: alerts.filter(a => a.type === 'GAINER'),
VOLUME_SPIKE: alerts.filter(a => a.type === 'VOLUME_SPIKE')
};
// 按级别排序
const sortedAlerts = [...alerts].sort((a, b) => {
const order = { critical: 0, warning: 1, normal: 2 };
return order[a.level] - order[b.level];
});
sortedAlerts.forEach((alert, i) => {
const levelEmoji = alert.level === 'critical' ? '🚨' : (alert.level === 'warning' ? '⚠️' : '📢');
report += `${levelEmoji} [${alert.type.replace('_', ' ')}] ${alert.symbol}\n`;
report += ` ${alert.message}\n\n`;
});
// 统计
report += `📊 统计:\n`;
report += ` - 价格预警: ${byType.PRICE_ALERT.length}\n`;
report += ` - 波动预警: ${byType.VOLATILITY.length}\n`;
report += ` - 涨幅榜异常: ${byType.GAINER.length}\n`;
report += ` - 交易量突增: ${byType.VOLUME_SPIKE.length}\n`;
report += `\n${'='.repeat(60)}\n`;
return report;
}
async function main() {
console.log('🔍 Crypto Hunter v2.0 启动...\n');
try {
const data = await fetchData();
const coins = data.data || [];
const alerts = analyzeAlerts(coins);
// 生成报告
const report = generateReport(alerts);
console.log(report);
// 发送通知
if (alerts.length > 0) {
const message = alerts.map(a => a.message).join('\n');
await Promise.all([
sendTelegram(message),
sendFeishu(message)
]);
}
} catch (error) {
console.error('❌ 获取数据失败:', error.message);
}
}
// CLI Commands
if (require.main === module) {
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
console.log(`
🐂 Crypto Hunter v2.0 - 加密货币智能监控系统
用法: node index.js [选项]
选项:
--help, -h 显示帮助信息
--report, -r 生成监控报告
--alerts 显示当前预警列表
--set-threshold <symbol> <price> [direction]
设置价格预警阈值
--list-thresholds
列出所有价格预警
示例:
node index.js --report
node index.js --set-threshold BTC 80000
node index.js --set-threshold ETH 2000 below
`);
process.exit(0);
}
if (args.includes('--alerts')) {
console.log('\n🚨 当前预警设置:\n');
CONFIG.priceThresholds.forEach((price, symbol) => {
console.log(` ${symbol}: $${price.toLocaleString()} (Above)`);
});
process.exit(0);
}
if (args.includes('--set-threshold')) {
const idx = args.indexOf('--set-threshold');
const symbol = args[idx + 1];
const price = args[idx + 2];
const direction = args[idx + 3] || 'above';
if (symbol && price) {
priceAlertSystem.setThreshold(symbol, price, direction);
console.log(`✅ 已设置 ${symbol} 价格预警: ${direction} $${price}`);
process.exit(0);
} else {
console.error('❌ 请提供币种符号和目标价格');
process.exit(1);
}
}
if (args.includes('--list-thresholds')) {
console.log('\n📋 当前监控的币种:\n');
const symbols = Object.keys(CONFIG.priceThresholds);
symbols.forEach(symbol => {
const t = priceAlertSystem.getThreshold(symbol);
console.log(` ${symbol}: ${formatPrice(t.target)} (${t.direction})`);
});
process.exit(0);
}
// 默认运行
main();
}
module.exports = {
main,
analyzeAlerts,
generateReport,
PriceAlertSystem,
priceAlertSystem
};