-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
217 lines (192 loc) · 5.85 KB
/
server.js
File metadata and controls
217 lines (192 loc) · 5.85 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
// server.js - AI News Backend
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const { Buffer } = require('buffer');
const app = express();
app.use(cors());
app.use(express.json());
// AI 新闻搜索关键词
const AI_KEYWORDS = [
'AI artificial intelligence',
'machine learning',
'deep learning',
'OpenAI ChatGPT',
'Google Gemini',
'Anthropic Claude',
'Llama LLM',
'AI regulation'
];
// 缓存文件路径
const CACHE_FILE = path.join(__dirname, 'news_cache.json');
const CACHE_EXPIRY = 24 * 60 * 60 * 1000; // 24小时
// 获取新闻缓存
function getNewsCache() {
try {
if (fs.existsSync(CACHE_FILE)) {
const data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf8'));
if (Date.now() - data.timestamp < CACHE_EXPIRY) {
return data.news;
}
}
} catch (e) {
console.error('Cache read error:', e);
}
return null;
}
// 保存新闻缓存
function saveNewsCache(news) {
try {
fs.writeFileSync(CACHE_FILE, JSON.stringify({
timestamp: Date.now(),
news: news
}));
} catch (e) {
console.error('Cache write error:', e);
}
}
// 搜索 AI 新闻 (使用 RSS 源)
async function searchAINews() {
const news = [];
const rssUrls = [
'https://feeds.feedburner.com/TechCrunch/',
'https://www.theverge.com/rss/index.xml',
'https://wired.com/feed/rss'
];
for (const url of rssUrls) {
try {
const response = await axios.get(url, {
timeout: 10000,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
});
// 简单解析 RSS - 更强的 description 提取
const items = response.data.match(/<item[^>]*>[\s\S]*?<\/item>/gi) || [];
for (const item of items.slice(0, 5)) {
const title = item.match(/<title[^>]*>([^<]+)<\/title>/i)?.[1] || '';
const link = item.match(/<link[^>]*>([^<]+)<\/link>/i)?.[1] || '';
// 匹配 description,支持 CDATA 和多行内容
const descMatch = item.match(/<description[^>]*>([\s\S]*?)<\/description>/i);
let desc = descMatch ? descMatch[1] : '';
// 移除 CDATA 包装
desc = desc.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/i, '$1');
// 清理并处理描述
desc = cleanText(desc).slice(0, 500);
// 检查是否与 AI 相关
const isAIRelated = AI_KEYWORDS.some(kw =>
title.toLowerCase().includes(kw.toLowerCase()) ||
desc.toLowerCase().includes(kw.toLowerCase())
);
if (isAIRelated || news.length < 10) {
// 去重
if (!news.find(n => n.title === cleanText(title))) {
news.push({
id: Buffer.from(cleanText(title)).toString('base64').slice(0, 16),
title: cleanText(title).slice(0, 100),
description: cleanText(desc).slice(0, 200),
summary: generateSummary(cleanText(desc)),
content: cleanText(desc),
link: link,
source: new URL(url).hostname,
time: new Date().toLocaleDateString('zh-CN'),
image: 'https://picsum.photos/300/200?random=' + Math.random()
});
}
}
}
} catch (e) {
console.error(`Failed to fetch ${url}:`, e.message);
}
}
return news.slice(0, 15);
}
// 生成摘要
function generateSummary(text) {
if (!text) return '暂无摘要';
// 简单摘要:取前200字
return text.slice(0, 200) + '...';
}
// 清理文本:移除HTML标签并解码HTML实体
function cleanText(text) {
if (!text) return '';
return text
.replace(/<[^>]+>/g, '') // 移除HTML标签
.replace(/ /g, ' ')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/&#(\d+);/g, (_, num) => String.fromCharCode(num))
.replace(/&#x([0-9a-fA-F]+);/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)))
.trim();
}
// 解码 HTML 实体(不删除标签,用于需要保留格式的地方)
function decodeHTML(html) {
if (!html) return '';
return html
.replace(/ /g, ' ')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/&#(\d+);/g, (_, num) => String.fromCharCode(num))
.replace(/&#x([0-9a-fA-F]+);/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)));
}
// 生成每日摘要
function generateDailySummary(news) {
if (!news || news.length === 0) return '暂无资讯';
const summary = `今日共收集 ${news.length} 条 AI 资讯。`;
const topNews = news.slice(0, 3).map((n, i) => `${i + 1}. ${cleanText(n.title)}`).join(';');
return summary + '主要动态:' + topNews;
}
// API 路由
app.get('/api/news', async (req, res) => {
try {
// 检查缓存
let news = getNewsCache();
if (!news) {
console.log('Fetching fresh news...');
news = await searchAINews();
saveNewsCache(news);
}
const dailySummary = generateDailySummary(news);
res.json({
success: true,
data: news,
summary: dailySummary,
updateTime: new Date().toISOString()
});
} catch (e) {
console.error('News API error:', e);
res.json({
success: false,
message: e.message
});
}
});
// 手动刷新新闻
app.post('/api/refresh', async (req, res) => {
try {
const news = await searchAINews();
saveNewsCache(news);
res.json({
success: true,
message: '刷新成功',
count: news.length
});
} catch (e) {
res.json({
success: false,
message: e.message
});
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`AI News Server running on port ${PORT}`);
});