-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrss-reader.js
More file actions
341 lines (291 loc) · 9.33 KB
/
rss-reader.js
File metadata and controls
341 lines (291 loc) · 9.33 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
/**
* RSS/Atom 订阅阅读器
* 支持解析和展示 RSS/Atom feed 内容
*/
class RSSReader {
constructor() {
this.cacheKey = 'rss-feed-cache';
this.cacheExpiry = 30 * 60 * 1000; // 30分钟缓存
this.feedUrls = [
{
name: '技术博客',
url: './blog/feed.xml',
enabled: true
},
{
name: '精选文章',
url: './blog/featured-feed.xml',
enabled: false
}
];
}
/**
* 获取缓存的 RSS 数据
*/
getCachedData(url) {
try {
const cached = localStorage.getItem(`${this.cacheKey}-${url}`);
if (!cached) return null;
const data = JSON.parse(cached);
const now = Date.now();
if (now - data.timestamp > this.cacheExpiry) {
localStorage.removeItem(`${this.cacheKey}-${url}`);
return null;
}
return data.content;
} catch (error) {
console.warn('读取 RSS 缓存失败:', error);
return null;
}
}
/**
* 缓存 RSS 数据
*/
cacheData(url, data) {
try {
const cacheEntry = {
timestamp: Date.now(),
content: data
};
localStorage.setItem(`${this.cacheKey}-${url}`, JSON.stringify(cacheEntry));
} catch (error) {
console.warn('缓存 RSS 数据失败:', error);
}
}
/**
* 解析 XML 字符串为 DOM 对象
*/
parseXML(xmlString) {
const parser = new DOMParser();
return parser.parseFromString(xmlString, 'text/xml');
}
/**
* 解析 RSS feed
*/
parseRSS(xmlDoc) {
const items = xmlDoc.querySelectorAll('item');
const articles = [];
items.forEach(item => {
const title = item.querySelector('title')?.textContent || '';
const link = item.querySelector('link')?.textContent || '';
const description = item.querySelector('description')?.textContent || '';
const pubDate = item.querySelector('pubDate')?.textContent || '';
const author = item.querySelector('author')?.textContent || '';
const category = item.querySelector('category')?.textContent || '';
// 从描述中提取纯文本
const plainDescription = this.stripHTML(description);
articles.push({
title: title.trim(),
link: link.trim(),
description: plainDescription.trim(),
pubDate: pubDate ? new Date(pubDate) : new Date(),
author: author.trim(),
category: category.trim(),
type: 'rss'
});
});
return articles;
}
/**
* 解析 Atom feed
*/
parseAtom(xmlDoc) {
const entries = xmlDoc.querySelectorAll('entry');
const articles = [];
entries.forEach(entry => {
const title = entry.querySelector('title')?.textContent || '';
const link = entry.querySelector('link')?.getAttribute('href') || '';
const content = entry.querySelector('content')?.textContent || entry.querySelector('summary')?.textContent || '';
const published = entry.querySelector('published')?.textContent || entry.querySelector('updated')?.textContent || '';
const author = entry.querySelector('author name')?.textContent || '';
const category = entry.querySelector('category')?.getAttribute('term') || '';
const plainDescription = this.stripHTML(content);
articles.push({
title: title.trim(),
link: link.trim(),
description: plainDescription.trim(),
pubDate: published ? new Date(published) : new Date(),
author: author.trim(),
category: category.trim(),
type: 'atom'
});
});
return articles;
}
/**
* 去除 HTML 标签,提取纯文本
*/
stripHTML(html) {
const temp = document.createElement('div');
temp.innerHTML = html;
return temp.textContent || temp.innerText || '';
}
/**
* 截取文本
*/
truncate(text, maxLength = 150) {
if (text.length <= maxLength) return text;
return text.substring(0, maxLength).trim() + '...';
}
/**
* 格式化日期
*/
formatDate(date) {
const now = new Date();
const diffTime = Math.abs(now - date);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays === 0) {
const diffHours = Math.floor(diffTime / (1000 * 60 * 60));
if (diffHours === 0) {
const diffMinutes = Math.floor(diffTime / (1000 * 60));
return diffMinutes <= 1 ? '刚刚' : `${diffMinutes}分钟前`;
}
return `${diffHours}小时前`;
}
if (diffDays === 1) return '昨天';
if (diffDays < 7) return `${diffDays}天前`;
if (diffDays < 30) return `${Math.floor(diffDays / 7)}周前`;
if (diffDays < 365) return `${Math.floor(diffDays / 30)}月前`;
return `${Math.floor(diffDays / 365)}年前`;
}
/**
* 获取单个 feed
*/
async fetchFeed(feedConfig) {
try {
// 检查缓存
const cached = this.getCachedData(feedConfig.url);
if (cached) {
console.log(`使用缓存的 RSS 数据: ${feedConfig.name}`);
return cached;
}
// 获取新数据
console.log(`从 URL 获取 RSS 数据: ${feedConfig.url}`);
const response = await fetch(feedConfig.url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const xmlText = await response.text();
const xmlDoc = this.parseXML(xmlText);
// 检测 feed 类型
const isRSS = xmlDoc.querySelector('rss');
const isAtom = xmlDoc.querySelector('feed');
let articles = [];
if (isRSS) {
articles = this.parseRSS(xmlDoc);
} else if (isAtom) {
articles = this.parseAtom(xmlDoc);
} else {
console.warn('无法识别的 feed 格式');
return [];
}
// 缓存数据
this.cacheData(feedConfig.url, articles);
return articles;
} catch (error) {
console.error(`获取 RSS feed 失败 (${feedConfig.name}):`, error);
return [];
}
}
/**
* 获取所有 feeds
*/
async fetchAllFeeds() {
const promises = this.feedUrls
.filter(feed => feed.enabled)
.map(feed => this.fetchFeed(feed));
const results = await Promise.all(promises);
// 合并所有文章并按日期排序
const allArticles = results.flat().sort((a, b) => b.pubDate - a.pubDate);
return allArticles;
}
/**
* 生成 feed XML (用于创建 RSS feed)
*/
generateFeedXML(articles, config = {}) {
const { title = 'CodeClub Blog', description = '技术博客', link = 'https://jacksoncode.github.io/blog/' } = config;
let xml = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${title}</title>
<description>${description}</description>
<link>${link}</link>
<atom:link href="${link}feed.xml" rel="self" type="application/rss+xml"/>
<language>zh-CN</language>
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
`;
articles.forEach(article => {
xml += ` <item>
<title><![CDATA[${article.title}]]></title>
<link>${article.link}</link>
<description><![CDATA[${article.description}]]></description>
<pubDate>${article.pubDate.toUTCString()}</pubDate>
<author>${article.author}</author>
<category><![CDATA[${article.category}]]></category>
</item>
`;
});
xml += ` </channel>
</rss>`;
return xml;
}
/**
* 从 HTML 页面提取文章信息
*/
extractArticlesFromHTML(htmlContent) {
const parser = new DOMParser();
const doc = parser.parseFromString(htmlContent, 'text/html');
const articles = [];
// 提取文章卡片
const articleCards = doc.querySelectorAll('.post-card, .blog-post, article');
articleCards.forEach(card => {
const titleEl = card.querySelector('h1, h2, h3, .post-title, .blog-title');
const linkEl = card.querySelector('a[href]');
const excerptEl = card.querySelector('.post-excerpt, .blog-excerpt, p');
const dateEl = card.querySelector('.post-date, .blog-date, time');
const categoryEl = card.querySelector('.post-category, .blog-category');
if (titleEl && linkEl) {
articles.push({
title: titleEl.textContent.trim(),
link: linkEl.getAttribute('href'),
description: excerptEl ? excerptEl.textContent.trim() : '',
pubDate: dateEl ? new Date(dateEl.textContent.trim()) : new Date(),
author: 'Jackson',
category: categoryEl ? categoryEl.textContent.trim() : '技术博客',
type: 'html'
});
}
});
return articles;
}
/**
* 自动生成 RSS feed
*/
async generateRSSFromBlogPages() {
try {
const response = await fetch('./blog.html');
const htmlContent = await response.text();
const articles = this.extractArticlesFromHTML(htmlContent);
if (articles.length === 0) {
console.warn('未找到文章信息');
return;
}
// 生成 RSS XML
const rssXML = this.generateFeedXML(articles, {
title: 'CodeClub 技术博客',
description: '分享编程心得、技术文章和学习笔记',
link: 'https://jacksoncode.github.io/blog/'
});
return rssXML;
} catch (error) {
console.error('生成 RSS feed 失败:', error);
return null;
}
}
}
// 创建全局实例
const rssReader = new RSSReader();
// 导出供其他模块使用
if (typeof module !== 'undefined' && module.exports) {
module.exports = RSSReader;
}