crawler.js
12.3 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
#!/usr/bin/env node
/**
* 热点监控爬虫 - RSS 采集脚本
* 负责采集、清洗、分类、评分并输出结构化热点数据
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// 配置
const CONFIG_PATH = '/Users/yibo/Documents/LemonAI/lemonboss/data/workspace/wechatai/config/hotspot_sources.json';
const OUTPUT_DIR = '/Users/yibo/Documents/LemonAI/lemonboss/data/workspace/wechatai/hotspots';
const DATA_CONTRACT_VERSION = '1.0';
// AI 关键词分类配置
const CATEGORY_KEYWORDS = {
llm: {
name: '大模型',
keywords: ['GPT', '大模型', 'LLM', 'Claude', 'Gemini', 'Llama', '大语言模型', 'Transformer', 'GPT-5', 'GPT-4', 'GPT-3', '多模态']
},
ai_app: {
name: 'AI应用',
keywords: ['AI应用', 'AI工具', 'ChatGPT', 'Agent', 'AI产品', '生成式AI', 'AIGC', 'Copilot', 'AI助手', '智能体', 'AI Agent', 'Vibe Coding']
},
ai_hardware: {
name: 'AI硬件',
keywords: ['AI芯片', 'GPU', 'NPU', 'TPU', 'AI手机', 'AI PC', '机器人', '具身智能', '人形机器人', '算力', '英伟达', 'NVIDIA']
},
industry: {
name: '行业动态',
keywords: ['OpenAI', 'Google', 'Microsoft', 'Meta', '字节', '阿里', '腾讯', '百度', 'Anthropic', 'DeepSeek', 'xAI', 'Mistral', 'AI投资', '融资', '收购', 'IPO', '财报', '裁员', '招聘']
},
research: {
name: '论文/研究',
keywords: ['论文', 'arXiv', '研究', 'Paper', 'SOTA', 'benchmark', '数据集', '算法', '模型架构', 'CVPR', 'ICML', 'NeurIPS', 'ACL']
}
};
// 数据源可信度评分
const SOURCE_SCORES = {
'jiqizhixin': 85,
'qbitai': 85,
'openai_blog': 95,
'36kr': 75,
'hackernews': 80
};
/**
* 生成唯一 ID
*/
function generateId(prefix = 'hs') {
return `${prefix}-${Date.now()}-${Math.random().toString(36).substr(2, 6)}`;
}
/**
* 计算热度评分
*/
function calculateHotScore(item, sourceId) {
// 基础分数:数据源可信度 (0-100)
const sourceScore = SOURCE_SCORES[sourceId] || 50;
// 时效性分数:越新越高 (0-100)
const now = new Date();
const published = new Date(item.published_at);
const hoursDiff = (now - published) / (1000 * 60 * 60);
let timelinessScore = Math.max(0, 100 - hoursDiff * 5); // 每小时衰减 5 分
// 内容质量分数:基于标题长度和内容丰富度 (0-100)
let contentScore = 50;
if (item.title) {
contentScore += Math.min(30, item.title.length / 2);
}
if (item.summary) {
contentScore += Math.min(20, item.summary.length / 50);
}
// 关键词热度加分
let keywordBonus = 0;
const text = `${item.title || ''} ${item.summary || ''}`.toLowerCase();
if (/gpt-?5|claude|openai/i.test(text)) keywordBonus += 10;
if (/agent|智能体/i.test(text)) keywordBonus += 5;
if (/融资|收购|ipo/i.test(text)) keywordBonus += 5;
// 加权计算总分
const finalScore = Math.min(100, Math.round(
sourceScore * 0.35 +
timelinessScore * 0.30 +
Math.min(100, contentScore) * 0.20 +
keywordBonus
));
return finalScore;
}
/**
* 分类 AI 内容
*/
function classifyContent(title, summary = '') {
const text = `${title} ${summary}`.toLowerCase();
const categories = [];
for (const [catId, config] of Object.entries(CATEGORY_KEYWORDS)) {
const matched = config.keywords.some(kw => text.includes(kw.toLowerCase()));
if (matched) {
categories.push(catId);
}
}
// 默认分类
if (categories.length === 0) {
categories.push('industry');
}
return categories;
}
/**
* 提取关键词
*/
function extractKeywords(title, summary = '') {
const text = `${title} ${summary}`;
const keywords = new Set();
// 提取所有分类关键词
for (const config of Object.values(CATEGORY_KEYWORDS)) {
for (const kw of config.keywords) {
if (text.toLowerCase().includes(kw.toLowerCase())) {
keywords.add(kw);
}
}
}
// 提取公司/产品名(首字母大写的单词)
const properNouns = text.match(/[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*/g) || [];
for (const noun of properNouns.slice(0, 5)) {
if (noun.length > 2 && !keywords.has(noun)) {
keywords.add(noun);
}
}
return Array.from(keywords).slice(0, 10);
}
/**
* 解析 RSS 内容
*/
function parseRSSContent(content, sourceId, sourceName) {
const items = [];
const now = new Date().toISOString();
// 简单的 RSS item 提取(基于正则)
const itemRegex = /<item>[\s\S]*?<\/item>/gi;
const itemsMatch = content.match(itemRegex) || [];
for (const itemXml of itemsMatch.slice(0, 20)) { // 限制每个源 20 条
try {
// 提取标题
const titleMatch = itemXml.match(/<title>(?:<!\[CDATA\[)?([\s\S]*?)(?:\]\]>)?<\/title>/i);
const title = titleMatch ? titleMatch[1].replace(/<!\[CDATA\[|\]\]>/g, '').trim() : '';
// 提取链接
const linkMatch = itemXml.match(/<link>(?:<!\[CDATA\[)?([\s\S]*?)(?:\]\]>)?<\/link>/i);
const url = linkMatch ? linkMatch[1].replace(/<!\[CDATA\[|\]\]>/g, '').trim() : '';
// 提取描述/摘要
const descMatch = itemXml.match(/<description>(?:<!\[CDATA\[)?([\s\S]*?)(?:\]\]>)?<\/description>/i);
let summary = descMatch ? descMatch[1].replace(/<!\[CDATA\[|\]\]>/g, '').trim() : '';
// 去除 HTML 标签
summary = summary.replace(/<[^>]+>/g, '').substring(0, 500);
// 提取发布时间
const pubDateMatch = itemXml.match(/<pubDate>([\s\S]*?)<\/pubDate>/i);
let published_at = now;
if (pubDateMatch) {
const date = new Date(pubDateMatch[1]);
if (!isNaN(date)) published_at = date.toISOString();
}
// 提取作者
const authorMatch = itemXml.match(/<author>(?:<!\[CDATA\[)?([\s\S]*?)(?:\]\]>)?<\/author>/i);
const author = authorMatch ? authorMatch[1].replace(/<!\[CDATA\[|\]\]>/g, '').trim() : sourceName;
if (title && url) {
const categories = classifyContent(title, summary);
const hot_score = calculateHotScore({ title, summary, published_at }, sourceId);
items.push({
id: generateId(),
title,
summary: summary || title,
url,
source: sourceName,
author,
published_at,
categories,
hot_score,
keywords: extractKeywords(title, summary),
fetched_at: now
});
}
} catch (e) {
console.error(`解析 item 失败: ${e.message}`);
}
}
return items;
}
/**
* 直接获取 RSS XML 并解析
*/
async function fetchRSSDirectly(rssUrl, sourceId, sourceName) {
try {
const result = execSync(`curl -sL "${rssUrl}" --max-time 30 2>/dev/null`, { encoding: 'utf-8', maxBuffer: 10 * 1024 * 1024 });
if (!result.includes('<rss') && !result.includes('<feed')) {
console.log(`${sourceName}: 直接获取失败,返回内容不是 RSS`);
return [];
}
return parseRSSContent(result, sourceId, sourceName);
} catch (e) {
console.error(`${sourceName} 直接获取失败: ${e.message}`);
return [];
}
}
/**
* 通过 jina.ai 获取并解析 RSS(作为 fallback)
*/
async function fetchViaJina(rssUrl, sourceId, sourceName) {
try {
const jinaUrl = `https://r.jina.ai/http://${rssUrl.replace(/^https?:\/\//, '')}`;
const result = execSync(`curl -sL "${jinaUrl}" --max-time 30 2>/dev/null`, { encoding: 'utf-8', maxBuffer: 10 * 1024 * 1024 });
// 如果返回的是 HTML 重定向页面,说明失败了
if (result.includes('You are being') || result.includes('redirected') || result.trim().length < 100) {
return [];
}
const items = [];
const now = new Date().toISOString();
let baseTime = new Date();
// 提取发布时间(在 Markdown 头部)
const timeMatch = result.match(/Published Time:\s*(.+)/);
if (timeMatch) {
const t = new Date(timeMatch[1]);
if (!isNaN(t)) baseTime = t;
}
// 标准格式: [title](url)date - 用于中文 RSS 源
const linkRegex = /\[([^\]]+)\]\((https?:\/\/[^\)]+)\)(?:\s*([A-Z][a-z]{2},?\s+\d{1,2}[a-z]*\s*\d{4}[\s\d:]*\s*[A-Z]{3,4})?)?/g;
let match;
let count = 0;
while ((match = linkRegex.exec(result)) !== null && count < 20) {
const title = match[1].trim();
const url = match[2].trim();
const dateStr = match[3];
// 过滤掉标题为空的条目(这是之前的问题根源)
if (!title || title === url || title.startsWith('http')) continue;
const itemDate = dateStr ? new Date(dateStr) : baseTime;
const published_at = !isNaN(itemDate) ? itemDate.toISOString() : baseTime.toISOString();
const categories = classifyContent(title);
const hot_score = calculateHotScore({ title, published_at }, sourceId);
items.push({
id: generateId(),
title,
summary: title, // fallback 模式下摘要用标题
url,
source: sourceName,
author: sourceName,
published_at,
categories,
hot_score,
keywords: extractKeywords(title),
fetched_at: now
});
count++;
}
return items;
} catch (e) {
console.error(`${sourceName} jina.ai 采集失败: ${e.message}`);
return [];
}
}
/**
* 主采集函数
*/
async function crawl() {
console.log('🤖 热点监控爬虫启动...');
console.log(`⏰ 采集时间: ${new Date().toISOString()}`);
// 读取配置
const config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'));
// 采集所有源
const allItems = [];
const sourceStats = {};
for (const source of config.sources.rss) {
if (source.priority === 'P0' || source.priority === 'P1') {
console.log(`\n📡 正在采集: ${source.name} (${source.url})`);
let items = [];
let usedFallback = false;
try {
// 首先尝试直接获取 RSS XML
items = await fetchRSSDirectly(source.url, source.id, source.name);
// 如果直接获取失败,尝试 jina.ai fallback
if (items.length === 0) {
console.log(` 🔄 直接获取失败,尝试 jina.ai fallback...`);
items = await fetchViaJina(source.url, source.id, source.name);
usedFallback = true;
}
allItems.push(...items);
sourceStats[source.id] = {
name: source.name,
count: items.length,
success: items.length > 0,
fallback: usedFallback
};
console.log(` ✅ 成功采集 ${items.length} 条${usedFallback ? ' (via fallback)' : ''}`);
} catch (e) {
sourceStats[source.id] = {
name: source.name,
count: 0,
success: false,
error: e.message
};
console.log(` ❌ 采集失败: ${e.message}`);
}
}
}
// 数据去重(基于 URL)
const seenUrls = new Set();
const uniqueItems = [];
for (const item of allItems) {
if (!seenUrls.has(item.url)) {
seenUrls.add(item.url);
uniqueItems.push(item);
}
}
// 按热度排序
uniqueItems.sort((a, b) => b.hot_score - a.hot_score);
// 生成输出
const today = new Date().toISOString().split('T')[0];
const outputDir = path.join(OUTPUT_DIR, today);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const output = {
meta: {
version: DATA_CONTRACT_VERSION,
created_at: new Date().toISOString(),
created_by: 'wechatai:crawler',
data_contract: 'raw_feeds_v1'
},
stats: {
total_count: uniqueItems.length,
sources_count: Object.keys(sourceStats).length,
categories_count: Object.keys(CATEGORY_KEYWORDS).length,
fetch_duration_ms: Date.now() - new Date(allItems[0]?.fetched_at || Date.now()).getTime()
},
sources: sourceStats,
hotspots: uniqueItems
};
// 写入文件
const outputPath = path.join(outputDir, 'raw_feeds.json');
fs.writeFileSync(outputPath, JSON.stringify(output, null, 2), 'utf-8');
console.log(`\n✅ 采集完成!`);
console.log(`📊 总计: ${uniqueItems.length} 条 (去重前: ${allItems.length} 条)`);
console.log(`📁 输出: ${outputPath}`);
console.log('\n📈 数据源统计:');
for (const [id, stat] of Object.entries(sourceStats)) {
const status = stat.success ? '✅' : '❌';
console.log(` ${status} ${stat.name}: ${stat.count} 条`);
}
return output;
}
// 执行采集
if (require.main === module) {
crawl().catch(console.error);
}
module.exports = { crawl, parseRSSContent, calculateHotScore, classifyContent, fetchRSSDirectly, fetchViaJina };