-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
169 lines (139 loc) · 4.83 KB
/
Copy pathexample.js
File metadata and controls
169 lines (139 loc) · 4.83 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
#!/usr/bin/env node
/**
* MCP实时新闻工具使用示例
* 演示各种功能的调用方法
*/
const { mcpNewsTool, getLatestNews, searchNews, getNewsCategories } = require('./realtime-news-mcp.js');
async function runExamples() {
console.log('🚀 MCP实时新闻工具功能演示\n');
try {
// 示例1: 查看所有新闻分类
console.log('📋 示例1: 查看新闻分类');
console.log('='.repeat(50));
const categories = await mcpNewsTool('categories');
console.log(categories);
console.log('\n');
// 示例2: 获取头条新闻
console.log('📰 示例2: 获取最新头条新闻');
console.log('='.repeat(50));
const headlines = await mcpNewsTool('latest', 'top', 3);
console.log(headlines);
console.log('\n');
// 示例3: 获取科技新闻
console.log('💻 示例3: 获取科技新闻');
console.log('='.repeat(50));
const techNews = await mcpNewsTool('latest', 'keji', 2);
console.log(techNews);
console.log('\n');
// 示例4: 搜索关键词
console.log('🔍 示例4: 搜索关键词新闻');
console.log('='.repeat(50));
const searchResults = await mcpNewsTool('search', '科技', 2);
console.log(searchResults);
console.log('\n');
// 示例5: 直接调用函数
console.log('⚡ 示例5: 直接函数调用');
console.log('='.repeat(50));
const directCall = await getLatestNews('caijing', 2);
console.log('✅ 直接调用结果:\n', directCall);
} catch (error) {
console.error('❌ 示例执行失败:', error.message);
console.log('\n💡 可能的解决方案:');
console.log('1. 检查网络连接是否正常');
console.log('2. 确认API密钥是否有效');
console.log('3. 稍后重试或联系技术支持');
}
}
// 批量获取多分类新闻的示例函数
async function batchGetNews() {
console.log('\n📊 批量获取示例: 多分类新闻');
console.log('='.repeat(50));
const categories = ['top', 'keji', 'tiyu'];
const results = {};
for (const category of categories) {
try {
console.log(`正在获取 ${category} 新闻...`);
const news = await mcpNewsTool('latest', category, 1);
results[category] = '✅ 获取成功';
} catch (error) {
results[category] = `❌ 获取失败: ${error.message}`;
}
}
console.log('\n批量获取结果:');
Object.entries(results).forEach(([cat, result]) => {
console.log(`• ${cat}: ${result}`);
});
}
// 错误处理示例
async function errorHandlingExample() {
console.log('\n🛡️ 错误处理示例');
console.log('='.repeat(50));
try {
// 测试无效分类
const invalidCategory = await mcpNewsTool('latest', 'invalid_category');
console.log(invalidCategory);
// 测试空搜索关键词
const emptySearch = await mcpNewsTool('search', '');
console.log(emptySearch);
// 测试无效操作
const invalidAction = await mcpNewsTool('invalid_action');
console.log(invalidAction);
} catch (error) {
console.log(`捕获到错误: ${error.message}`);
}
}
// 自定义工具函数示例
async function customToolExample() {
console.log('\n🔧 自定义功能示例');
console.log('='.repeat(50));
// 自定义函数:获取今日要闻摘要
async function getTodayBrief() {
try {
const headlines = await getLatestNews('top', 5);
if (headlines.includes('✅')) {
return `📄 今日要闻摘要 (${new Date().toLocaleDateString('zh-CN')})\n${headlines}`;
} else {
return '📄 今日暂无要闻更新';
}
} catch (error) {
return `📄 获取今日要闻失败: ${error.message}`;
}
}
const brief = await getTodayBrief();
console.log(brief);
}
// 主执行函数
async function main() {
console.log('⏰ 开始时间:', new Date().toLocaleString('zh-CN'));
console.log('='.repeat(80));
// 运行基础示例
await runExamples();
// 运行批量获取示例
await batchGetNews();
// 运行错误处理示例
await errorHandlingExample();
// 运行自定义功能示例
await customToolExample();
console.log('\n='.repeat(80));
console.log('⏰ 结束时间:', new Date().toLocaleString('zh-CN'));
console.log('🎉 所有示例执行完成!');
// 使用提示
console.log('\n💡 使用提示:');
console.log('• 可以修改示例中的参数来测试不同功能');
console.log('• 建议在实际项目中添加适当的错误处理');
console.log('• 注意API调用频率限制,避免过度调用');
console.log('• 查看 README.md 获取更多使用说明');
}
// 如果直接运行此文件,则执行示例
if (require.main === module) {
main().catch(error => {
console.error('💥 示例程序执行失败:', error);
process.exit(1);
});
}
module.exports = {
runExamples,
batchGetNews,
errorHandlingExample,
customToolExample
};