-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqclaw-skill.js
More file actions
339 lines (295 loc) ยท 13.2 KB
/
Copy pathqclaw-skill.js
File metadata and controls
339 lines (295 loc) ยท 13.2 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
'use strict';
/**
* qclaw-skill.js โ QClaw ๆ่ฝๅ
ฅๅฃ
*
* QClaw ้่ฟไผไธๅพฎไฟกๆถๅฐๆถๆฏๅ๏ผ่ฐ็จๆฌ่ๆฌๆฅ่ฏข Alpha-Radar ๆ
ๆฅๆฐๆฎใ
*
* ็จๆณ๏ผQClaw Skills ้
็ฝฎ้ๅกซๅๅฝไปค๏ผ๏ผ
* node C:/Users/lenovo/alpha-radar/qclaw-skill.js "ๆฅ่ฏขๅ่งๅจๆ"
* node C:/Users/lenovo/alpha-radar/qclaw-skill.js "ๆ็ดข BTC"
* node C:/Users/lenovo/alpha-radar/qclaw-skill.js "ๆฅๆฅ"
* node C:/Users/lenovo/alpha-radar/qclaw-skill.js "็ป่ฎก"
* node C:/Users/lenovo/alpha-radar/qclaw-skill.js "ๆๅ"
*
* ่พๅบ๏ผ็บฏๆๆฌ๏ผQClaw ่ฏปๅ stdout ๅ้ๅฐไผๅพฎ็พค๏ผ
*/
// ้้ปๅ ่ฝฝ .env๏ผ้ฟๅ
dotenv ็ๆ็คบไฟกๆฏๆฑกๆ่พๅบ็ปไผๅพฎ๏ผ
process.env.DOTENV_KEY = process.env.DOTENV_KEY || '';
require('dotenv').config({ path: require('path').join(__dirname, '.env'), quiet: true });
const Database = require('better-sqlite3');
const path = require('path');
const { execSync } = require('child_process');
const DB_PATH = path.join(__dirname, 'alpha_radar.db');
const query = (process.argv[2] || '').trim();
// โโ ไธปๅ
ฅๅฃ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function main() {
if (!query) {
print(HELP_TEXT);
return;
}
const q = query.toLowerCase();
// ่ทฏ็ฑๅฝไปค
if (matchAny(q, ['ๆฅๆฅ', 'daily', 'ไปๆฅๆฅๅ', 'ไปๅคฉๆฅๅ'])) {
await runDailyReport();
} else if (matchAny(q, ['ๅจๆฅ', 'weekly', 'ๆฌๅจๆฅๅ'])) {
await runWeeklyReport();
} else if (matchAny(q, ['ๆๅ', 'scrape', 'ๆๅ', '็ซๅณๆๅ', 'ๆๅจๆๅ'])) {
await runScrape();
} else if (matchAny(q, ['็ป่ฎก', 'stats', 'ๆฐๆฎ็ป่ฎก', 'ๆฆๅต'])) {
await showStats();
} else if (q.startsWith('ๆ็ดข') || q.startsWith('search ') || q.startsWith('ๆฅๆพ') || q.startsWith('ๆพ')) {
const keyword = q.replace(/^(ๆ็ดข|search|ๆฅๆพ|ๆพ)\s*/, '').trim();
await searchNews(keyword);
} else if (matchAny(q, ['ๅธฎๅฉ', 'help', '?', '๏ผ', 'ๅฝไปค'])) {
print(HELP_TEXT);
} else {
// ้ป่ฎค๏ผๆๅ็ฑป/ๅ
ณ้ฎ่ฏๆฅๆๆฐๆ
ๆฅ
await queryLatestNews(query);
}
}
// โโ ๅธฎๅฉๆๆฌ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const HELP_TEXT = `๐ก Alpha-Radar ๆ
ๆฅๅฉๆ
ๅฏ็จๅฝไปค๏ผ
โข ๆฅๆๆฐๆ
ๆฅ / ๅ่ง / ๅธๅบ / ๆๆฏ โ ๆๅ็ฑปๆฅ็ๆๆฐ้ซๅๆ
ๆฅ
โข ๆ็ดข <ๅ
ณ้ฎ่ฏ> โ ๅ
จๆๆ็ดขๆ
ๆฅ
โข ็ป่ฎก โ ๆฅ็ๆฐๆฎๆฆๅต
โข ๆฅๆฅ โ ็ๆไปๆฅๆ
ๆฅๆฅๆฅ
โข ๅจๆฅ โ ็ๆๆฌๅจๆ
ๆฅๅจๆฅ
โข ๆๅ โ ็ซๅณๆๅๆๆฐๆ
ๆฅ
็คบไพ๏ผ
ๅ่งๅจๆ
ๆ็ดข SFC ็็ฎก
็ป่ฎก
ๆฅๆฅ`;
// โโ ๆฅๆๆฐๆ
ๆฅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function queryLatestNews(rawQuery) {
const db = openDB();
// ไผๅ
ๆฅ24h๏ผๆ ๆฐๆฎๅๆฉๅฑๅฐ72h
let since = Date.now() - 24 * 3600 * 1000;
let timeLabel = '24ๅฐๆถ';
// ๅคๆญๆฏๅฆๆฏๅ็ฑป่ฏ
const categoryMap = {
'ๅ่ง': 'ๅ่ง', '็็ฎก': 'ๅ่ง', '็็
ง': 'ๅ่ง', 'sfc': 'ๅ่ง',
'ๅธๅบ': 'ๅธๅบ่กๆ
', 'ไปทๆ ผ': 'ๅธๅบ่กๆ
', '่กๆ
': 'ๅธๅบ่กๆ
',
'ๆๆฏ': 'ๆๆฏ', 'ๅผๅ': 'ๆๆฏ', '้พไธ': 'ๆๆฏ',
'ไบงๅ': 'ไบงๅ', 'ไธ็บฟ': 'ไบงๅ',
'็ซไบ': '็ซไบๅจๆ', 'ไบคๆๆ': '็ซไบๅจๆ',
};
const matchedCat = Object.entries(categoryMap).find(([k]) =>
rawQuery.toLowerCase().includes(k)
);
let rows;
if (matchedCat) {
rows = db.prepare(`
SELECT title, alpha_score, detail, source, business_category, impact, url, timestamp
FROM news
WHERE timestamp >= ? AND business_category LIKE ?
ORDER BY alpha_score DESC, timestamp DESC
LIMIT 5
`).all(since, `%${matchedCat[1]}%`);
} else {
// ๆ ๅน้
ๅ็ฑป โ ่ฟๅ็ปผๅ้ซๅๆ
ๆฅ
rows = db.prepare(`
SELECT title, alpha_score, detail, source, business_category, impact, url, timestamp
FROM news
WHERE timestamp >= ? AND alpha_score >= 60
ORDER BY alpha_score DESC, timestamp DESC
LIMIT 5
`).all(since);
}
// 24h ๆ ๆฐๆฎๆถๆฉๅฑๅฐ 72h
if (!rows.length) {
since = Date.now() - 72 * 3600 * 1000;
timeLabel = '72ๅฐๆถ';
if (matchedCat) {
rows = db.prepare(`
SELECT title, alpha_score, detail, source, business_category, impact, url, timestamp
FROM news WHERE timestamp >= ? AND business_category LIKE ?
ORDER BY alpha_score DESC, timestamp DESC LIMIT 5
`).all(since, `%${matchedCat[1]}%`);
} else {
rows = db.prepare(`
SELECT title, alpha_score, detail, source, business_category, impact, url, timestamp
FROM news WHERE timestamp >= ? AND alpha_score >= 60
ORDER BY alpha_score DESC, timestamp DESC LIMIT 5
`).all(since);
}
}
db.close();
if (!rows.length) {
print('๐ญ ่ฟๆๆๆ ็ธๅ
ณๆ
ๆฅใ\nๅ้ใๆๅใ็ซๅณๆๅๆๆฐๆฐๆฎใ');
return;
}
const label = matchedCat ? `ใ${matchedCat[1]}ใๅ็ฑป` : '็ปผๅ้ซๅ';
let out = `๐ก ่ฟๅป${timeLabel} ${label} ๆ
ๆฅ๏ผTop ${rows.length}๏ผ\n${'โ'.repeat(30)}\n\n`;
rows.forEach((r, i) => {
const emoji = r.alpha_score >= 90 ? '๐ฅ' : r.alpha_score >= 70 ? 'โญ' : '๐';
const impact = r.impact === 'ๅฉๅฅฝ' ? '๐' : r.impact === 'ๅฉ็ฉบ' ? '๐' : 'โ';
const time = formatTime(r.timestamp);
out += `${emoji} [${r.alpha_score}ๅ] ${r.title}\n`;
if (r.detail) out += ` ${r.detail}\n`;
out += ` ${impact} ${r.impact || 'ไธญๆง'} | ๆฅๆบ: ${r.source} | ${time}\n`;
if (i < rows.length - 1) out += '\n';
});
print(out);
}
// โโ ๆ็ดขๆ
ๆฅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function searchNews(keyword) {
if (!keyword) {
print('่ฏทๆไพๆ็ดขๅ
ณ้ฎ่ฏ๏ผไพๅฆ๏ผๆ็ดข SFC ็็ฎก');
return;
}
const db = openDB();
const rows = db.prepare(`
SELECT title, alpha_score, detail, source, business_category, impact, timestamp
FROM news
WHERE (title LIKE ? OR content LIKE ? OR detail LIKE ?)
AND alpha_score >= 40
ORDER BY alpha_score DESC, timestamp DESC
LIMIT 8
`).all(`%${keyword}%`, `%${keyword}%`, `%${keyword}%`);
if (!rows.length) {
db.close();
print(`๐ ๆชๆพๅฐๅ
ๅซใ${keyword}ใ็ๆ
ๆฅใ\n\nๅปบ่ฎฎ๏ผ\nโข ๆฃๆฅๅ
ณ้ฎ่ฏๆผๅ\nโข ๅฐ่ฏๆด็ญ็ๅ
ณ้ฎ่ฏ\nโข ๅ้ใๆๅใ่ทๅๆๆฐๆฐๆฎๅๅๆ็ดข`);
return;
}
db.close();
let out = `๐ ใ${keyword}ใๆ็ดข็ปๆ๏ผๅ
ฑ ${rows.length} ๆก๏ผ\n${'โ'.repeat(30)}\n\n`;
rows.forEach((r, i) => {
const emoji = r.alpha_score >= 90 ? '๐ฅ' : r.alpha_score >= 70 ? 'โญ' : '๐';
const time = formatTime(r.timestamp);
out += `${emoji} [${r.alpha_score}ๅ] ${r.title}\n`;
if (r.detail) out += ` ${r.detail}\n`;
out += ` ๅ็ฑป: ${r.business_category || 'ๆชๅ็ฑป'} | ๆฅๆบ: ${r.source} | ${time}\n`;
if (i < rows.length - 1) out += '\n';
});
print(out);
}
// โโ ็ป่ฎกๆฆๅต โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function showStats() {
const db = openDB();
const now = Date.now();
const day1 = now - 24 * 3600 * 1000;
const day7 = now - 7 * 24 * 3600 * 1000;
const total24h = db.prepare('SELECT COUNT(*) as n FROM news WHERE timestamp >= ?').get(day1).n;
const important24h = db.prepare('SELECT COUNT(*) as n FROM news WHERE timestamp >= ? AND alpha_score >= 70').get(day1).n;
const critical24h = db.prepare('SELECT COUNT(*) as n FROM news WHERE timestamp >= ? AND alpha_score >= 90').get(day1).n;
const total7d = db.prepare('SELECT COUNT(*) as n FROM news WHERE timestamp >= ?').get(day7).n;
// ่ทๅๆฐๆฎๅบๆๆฐไธๆก็ๆถ้ด๏ผๆ็คบๆฐๆฎๆฐ้ฒๅบฆ
const latest = db.prepare('SELECT timestamp FROM news ORDER BY timestamp DESC LIMIT 1').get();
const latestAge = latest ? Math.floor((now - latest.timestamp) / 3600000) : null;
const topSources = db.prepare(`
SELECT source, COUNT(*) as n
FROM news WHERE timestamp >= ?
GROUP BY source ORDER BY n DESC LIMIT 5
`).all(day1);
const topCats = db.prepare(`
SELECT business_category, COUNT(*) as n, AVG(alpha_score) as avg_score
FROM news WHERE timestamp >= ? AND business_category IS NOT NULL AND business_category != ''
GROUP BY business_category ORDER BY n DESC LIMIT 5
`).all(day1);
const topItems = db.prepare(`
SELECT title, alpha_score, source FROM news
WHERE timestamp >= ? AND alpha_score >= 70
ORDER BY alpha_score DESC LIMIT 3
`).all(day1);
db.close();
let out = `๐ Alpha-Radar ๆ
ๆฅ็ป่ฎก\n${'โ'.repeat(30)}\n`;
if (latestAge !== null) {
out += latestAge < 2 ? `๐ข ๆฐๆฎๆฐ้ฒ๏ผ${latestAge}ๅฐๆถๅๆดๆฐ๏ผ\n\n` :
latestAge < 12 ? `๐ก ๆฐๆฎ่พๆฐ๏ผ${latestAge}ๅฐๆถๅๆดๆฐ๏ผ\n\n` :
`๐ด ๆฐๆฎ่พๆง๏ผ${latestAge}ๅฐๆถๅๆดๆฐ๏ผ๏ผๅปบ่ฎฎๅใๆๅใ\n\n`;
} else {
out += '\n';
}
out += `๐
่ฟๅป 24 ๅฐๆถ\n`;
out += ` ๆปๆ
ๆฅ: ${total24h} ๆก\n`;
out += ` ้่ฆ (โฅ70ๅ): ${important24h} ๆก\n`;
out += ` ็ดงๆฅ (โฅ90ๅ): ${critical24h} ๆก\n\n`;
out += `๐
่ฟๅป 7 ๅคฉ: ${total7d} ๆกๆ
ๆฅ\n\n`;
if (topItems.length) {
out += `๐ฅ ไปๆฅๆ้ซๅๆ
ๆฅ\n`;
topItems.forEach(r => {
out += ` [${r.alpha_score}] ${r.title.slice(0, 40)}${r.title.length > 40 ? 'โฆ' : ''}\n`;
});
out += '\n';
}
if (topCats.length) {
out += `๐ ไปๆฅๅ็ฑปๅๅธ\n`;
topCats.forEach(c => {
out += ` ${c.business_category}: ${c.n}ๆก (ๅๅ${Math.round(c.avg_score)})\n`;
});
out += '\n';
}
if (topSources.length) {
out += `๐ฐ ไปๆฅๆดป่ทๆฅๆบ\n`;
topSources.forEach(s => out += ` ${s.source}: ${s.n}ๆก\n`);
}
print(out);
}
// โโ ็ๆๆฅๆฅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function runDailyReport() {
print('๐ ๆญฃๅจ็ๆไปๆฅๆ
ๆฅๆฅๆฅ๏ผ่ฏท็จๅ...');
try {
execSync('node run_daily_report.js', {
cwd: __dirname,
timeout: 120000,
stdio: 'pipe',
});
print('โ
ๆฅๆฅๅทฒ็ๆๅนถๆจ้ๅฐไผๅพฎ๏ผ่ฏทๆฅ็็พคๆถๆฏใ');
} catch (err) {
print(`โ ๆฅๆฅ็ๆๅคฑ่ดฅ: ${err.message.slice(0, 100)}\n\n่ฏทๆฃๆฅ DEEPSEEK_API_KEY ๆฏๅฆ้
็ฝฎๆญฃ็กฎใ`);
}
}
// โโ ็ๆๅจๆฅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function runWeeklyReport() {
print('๐
ๆญฃๅจ็ๆๆฌๅจๆ
ๆฅๅจๆฅ๏ผ่ฏท็จๅ๏ผๅฏ่ฝ้่ฆ1-2ๅ้๏ผ...');
try {
execSync('node run_weekly_report.js', {
cwd: __dirname,
timeout: 180000,
stdio: 'pipe',
});
print('โ
ๅจๆฅๅทฒ็ๆๅนถๆจ้ๅฐไผๅพฎ๏ผ่ฏทๆฅ็็พคๆถๆฏใ');
} catch (err) {
print(`โ ๅจๆฅ็ๆๅคฑ่ดฅ: ${err.message.slice(0, 100)}`);
}
}
// โโ ๆๅจๆๅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function runScrape() {
print('๐ท๏ธ ๆญฃๅจ่งฆๅๆ
ๆฅๆๅ๏ผ้ข่ฎก้่ฆ2-3ๅ้...\nๆๅๅฎๆๅ้่ฆๆ
ๆฅไผ็ดๆฅๆจ้ๅฐๆฌ็พคใ');
try {
execSync('node scrapers/index.js --tier=high', {
cwd: __dirname,
timeout: 180000,
stdio: 'pipe',
});
print('โ
ๆๅๅฎๆ๏ผ้่ฆๆ
ๆฅๅทฒๆจ้ใ\nๅ้ใ็ป่ฎกใๆฅ็ๆฌๆฌกๆดๆฐๆฐๆฎใ');
} catch (err) {
print(`โ ๏ธ ๆๅไปปๅกๅทฒๅฏๅจ๏ผๅๅฐ่ฟ่กไธญ๏ผ๏ผ้ข่ฎก2-3ๅ้ๅๆ
ๆฅไผ้็ปญๆจๅ
ฅ็พคใ`);
}
}
// โโ ๅทฅๅ
ทๅฝๆฐ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
function openDB() {
return new Database(DB_PATH, { readonly: true });
}
function print(text) {
process.stdout.write(text + '\n');
}
function matchAny(str, keywords) {
return keywords.some(k => str.includes(k));
}
function formatTime(ts) {
if (!ts) return 'ๆช็ฅๆถ้ด';
const d = new Date(ts);
const now = new Date();
const diffH = Math.floor((now - d) / 3600000);
if (diffH < 1) return 'ๅๅ';
if (diffH < 24) return `${diffH}ๅฐๆถๅ`;
return `${Math.floor(diffH / 24)}ๅคฉๅ`;
}
// โโ ๆง่ก โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
main().catch(err => {
print(`โ ๆง่กๅบ้: ${err.message}`);
process.exit(1);
});