Repository navigation
Expand file tree
/
Copy pathupdate_rss.py
More file actions
397 lines (332 loc) · 17 KB
/
Copy pathupdate_rss.py
File metadata and controls
397 lines (332 loc) · 17 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
import os
import logging
import xml.etree.ElementTree as ET
from datetime import datetime, timedelta
import re
from persistence import GitRepository
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('update_rss')
class RSSUpdater:
"""RSS更新器,用于更新feed.xml文件"""
def __init__(self, feed_path='feed.xml', articles_dir='latepost_articles'):
"""初始化RSS更新器"""
self.feed_path = feed_path
self.articles_dir = articles_dir
self.max_items = 50 # 最大保留文章数量
def get_latest_article_id(self):
"""从feed.xml中获取最新文章ID"""
try:
if not os.path.exists(self.feed_path):
logger.error(f"feed.xml文件不存在: {self.feed_path}")
return None
# 解析XML文件
tree = ET.parse(self.feed_path)
root = tree.getroot()
channel = root.find('channel')
# 优先检查自定义的 latestArticleId 标签
latest_id_elem = channel.find('latestArticleId')
if latest_id_elem is not None and latest_id_elem.text:
try:
latest_id = int(latest_id_elem.text)
logger.info(f"从feed.xml元数据获取到的最新文章ID: {latest_id}")
return latest_id
except ValueError:
logger.warning("latestArticleId格式错误")
# 查找所有链接
links = root.findall('.//item/link')
if not links:
logger.warning("feed.xml中没有找到任何文章链接")
return None
# 提取所有文章ID并找出最大值
article_ids = []
for link in links:
url = link.text
# 使用正则表达式提取ID
match = re.search(r'id=(\d+)', url)
if match:
article_ids.append(int(match.group(1)))
if not article_ids:
logger.warning("未能从feed.xml中提取到任何文章ID")
return None
# 返回最大ID
max_id = max(article_ids)
logger.info(f"从feed.xml中获取到的最大文章ID: {max_id}")
return max_id
except Exception as e:
logger.error(f"获取最新文章ID时出错: {str(e)}")
return None
def count_items(self, root):
"""计算feed.xml中的文章数量"""
items = root.findall('.//item')
return len(items)
def update_feed(self, new_article_ids):
"""更新feed.xml,添加新文章"""
try:
if not os.path.exists(self.feed_path):
logger.error(f"feed.xml文件不存在: {self.feed_path}")
return False
# 解析XML文件
tree = ET.parse(self.feed_path)
root = tree.getroot()
channel = root.find('channel')
# 更新lastBuildDate
last_build_date = channel.find('lastBuildDate')
now = datetime.now().strftime('%a, %d %b %Y %H:%M:%S +0000')
if last_build_date is not None:
last_build_date.text = now
else:
last_build_date = ET.SubElement(channel, 'lastBuildDate')
last_build_date.text = now
# 维护自定义的 latestArticleId 标签
latest_id_elem = channel.find('latestArticleId')
current_max_id = 0
if new_article_ids:
current_max_id = max(new_article_ids)
# 如果存在现有标签,读取并比较
if latest_id_elem is not None and latest_id_elem.text:
try:
existing_id = int(latest_id_elem.text)
current_max_id = max(current_max_id, existing_id)
except ValueError:
pass
elif not latest_id_elem:
# 创建新标签
latest_id_elem = ET.SubElement(channel, 'latestArticleId')
# 确保最新ID也会考虑现有RSS中的ID(以防回滚)
# 但既然我们用这个标签作为High Water Mark,就只增不减
# 如果RSS里还有比current_max_id更大的ID? (比如 manual edit)
# 我们通过get_latest_article_id读取过
# 更新标签值
if current_max_id > 0:
latest_id_elem.text = str(current_max_id)
# 添加新文章
articles_added = 0
for article_id in new_article_ids:
article_path = os.path.join(self.articles_dir, f"latepost_article_{article_id}.md")
if not os.path.exists(article_path):
logger.warning(f"文章文件不存在: {article_path}")
continue
# 读取文章内容
with open(article_path, 'r', encoding='utf-8') as f:
content = f.read()
# 提取标题、日期和作者
title_match = re.search(r'# (.+)', content)
date_match = re.search(r'\*\*发布日期\*\*: (.+)', content)
author_match = re.search(r'\*\*作者\*\*: (.+)', content)
if not title_match:
logger.warning(f"无法从文章中提取标题: {article_path}")
continue
title = title_match.group(1).strip()
publish_date = date_match.group(1).strip() if date_match else "未知日期"
author = author_match.group(1).strip() if author_match else "未知作者"
# 创建新的item元素
item = ET.SubElement(channel, 'item')
# 添加标题
title_elem = ET.SubElement(item, 'title')
title_elem.text = title
# 添加链接
link_elem = ET.SubElement(item, 'link')
link_elem.text = f"https://www.latepost.com/news/dj_detail?id={article_id}"
# 添加描述(HTML格式)
desc_elem = ET.SubElement(item, 'description')
# 创建HTML格式的描述
html_content = self._create_html_description(content, title, publish_date, author)
desc_elem.text = html_content
# 添加发布日期
pubdate_elem = ET.SubElement(item, 'pubDate')
try:
dt = self._parse_date_str(publish_date)
if dt:
pubdate_elem.text = dt.strftime('%a, %d %b %Y %H:%M:%S +0000')
else:
logger.warning(f"无法解析日期: {publish_date},使用当前时间")
pubdate_elem.text = now
except Exception as e:
logger.warning(f"日期解析失败: {str(e)},使用当前时间")
pubdate_elem.text = now
# 添加GUID
guid_elem = ET.SubElement(item, 'guid')
guid_elem.text = f"https://www.latepost.com/news/dj_detail?id={article_id}"
articles_added += 1
logger.info(f"已添加文章: {title} (ID: {article_id})")
# 检查文章总数,如果超过最大数量,删除最旧的文章
items = channel.findall('item')
if len(items) > self.max_items:
# 按发布日期排序
items_with_dates = []
for item in items:
pubdate = item.find('pubDate')
title = item.find('title')
title_text = title.text if title is not None else "未知标题"
if pubdate is not None and pubdate.text:
try:
# 尝试解析RSS标准格式
dt = datetime.strptime(pubdate.text, '%a, %d %b %Y %H:%M:%S +0000')
items_with_dates.append((dt, item))
except Exception as e:
logger.warning(f"排序时解析日期失败: {pubdate.text} ({title_text}), 错误: {e}")
# 如果解析失败,为了安全起见,假设它是现在的(保留它),除非它真的很旧
items_with_dates.append((datetime.now(), item))
else:
# 如果没有日期,假设是最新的文章
items_with_dates.append((datetime.now(), item))
# 按日期排序 (Oldest first)
items_with_dates.sort(key=lambda x: x[0])
# 删除最旧的文章
items_to_remove = len(items) - self.max_items
if items_to_remove > 0:
logger.info(f"需要删除 {items_to_remove} 篇旧文章 (总数: {len(items)}, 最大: {self.max_items})")
for dt, item in items_with_dates[:items_to_remove]:
title_elem = item.find('title')
title_text = title_elem.text if title_elem is not None else "未知标题"
logger.info(f"删除旧文章: {title_text} (发布日期: {dt})")
channel.remove(item)
# 保存更新后的feed.xml
tree.write(self.feed_path, encoding='utf-8', xml_declaration=True)
logger.info(f"成功更新feed.xml,添加了{articles_added}篇新文章")
# 同步到Git仓库
self._sync_to_git_repository()
return True
except Exception as e:
logger.error(f"更新feed.xml时出错: {str(e)}")
return False
def _create_html_description(self, markdown_content, title, publish_date, author):
"""从Markdown内容创建HTML描述"""
# 创建基本的HTML结构
html = f"""
<style>
.article-container {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; max-width: 800px; margin: 0 auto; line-height: 1.8; padding: 20px; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,0.1); border-radius: 8px; }}
.article-meta {{ color: #666; font-size: 14px; margin-bottom: 30px; padding-bottom: 20px; border-bottom: 1px solid #eee; display: flex; flex-wrap: wrap; gap: 16px; }}
.article-meta span {{ display: inline-flex; align-items: center; }}
.article-meta span::before {{ content: ''; display: inline-block; width: 4px; height: 4px; background: #666; border-radius: 50%; margin-right: 8px; }}
.article-content {{ font-size: 16px; color: #2c3e50; }}
.article-content p {{ margin-bottom: 20px; line-height: 1.8; }}
.article-content img {{ max-width: 100%; height: auto; margin: 24px 0; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }}
.article-content blockquote {{ background: #f8f9fa; border-left: 4px solid #4a90e2; margin: 20px 0; padding: 20px; font-style: italic; border-radius: 0 8px 8px 0; }}
.article-content h1 {{ font-size: 24px; margin: 32px 0 16px; color: #2c3e50; }}
.article-content h2 {{ font-size: 20px; margin: 28px 0 14px; color: #2c3e50; }}
</style>
<div class='article-container'>
<div class='article-meta'>
<span>作者:{author}</span>
<span>发布日期:{publish_date}</span>
</div>
<div class='article-content'>
"""
# 提取正文内容(跳过标题和元信息)
content_lines = markdown_content.split('\n')
content_start = False
processed_content = []
for line in content_lines:
if line.strip() == '---':
content_start = True
continue
if content_start:
# 处理Markdown格式转HTML
# 处理标题
if line.startswith('# '):
processed_line = f"<h1>{line[2:]}</h1>"
elif line.startswith('## '):
processed_line = f"<h2>{line[3:]}</h2>"
# 处理引用
elif line.startswith('> '):
processed_line = f"<blockquote>{line[2:]}</blockquote>"
# 处理图片
elif line.startswith('
if len(img_parts) > 1:
img_url = img_parts[1].rstrip(')')
processed_line = f"<img src=\"{img_url}\" alt=\"图片\">"
else:
processed_line = f"<p>{line}</p>"
# 处理普通段落
elif line.strip():
processed_line = f"<p>{line}</p>"
else:
processed_line = ""
processed_content.append(processed_line)
html += '\n'.join(processed_content)
html += "\n </div>\n </div>"
return html
def _sync_to_git_repository(self):
"""将更新后的feed.xml同步到Git仓库"""
try:
git_repo = GitRepository()
if git_repo.push_feed_to_repository(self.feed_path):
logger.info("成功将feed.xml同步到Git仓库")
return True
else:
logger.error("同步feed.xml到Git仓库失败")
return False
except Exception as e:
logger.error(f"同步到Git仓库时出错: {str(e)}")
return False
def _parse_date_str(self, date_str):
"""解析多种格式的日期字符串"""
try:
now = datetime.now()
current_year = now.year
if not date_str or date_str == "未知日期":
return None
# 处理 "昨天 HH:MM"
if '昨天' in date_str:
dt = now - timedelta(days=1)
time_part = date_str.replace('昨天', '').strip()
if time_part:
try:
t = datetime.strptime(time_part, "%H:%M").time()
dt = datetime.combine(dt.date(), t)
except ValueError:
pass
return dt
# 处理 "今天 HH:MM"
if '今天' in date_str:
dt = now
time_part = date_str.replace('今天', '').strip()
if time_part:
try:
t = datetime.strptime(time_part, "%H:%M").time()
dt = datetime.combine(dt.date(), t)
except ValueError:
pass
return dt
# 尝试标准格式
# 移除 '月', '日' 等字符
clean_str = date_str.replace('月', '/').replace('日', '').strip()
formats = [
"%m/%d %H:%M",
"%Y/%m/%d %H:%M",
"%Y-%m-%d %H:%M",
"%Y-%m-%d",
"%H:%M" # 仅时间?
]
for fmt in formats:
try:
dt = datetime.strptime(clean_str, fmt)
# 如果只有月日,加上年份
if dt.year == 1900:
dt = dt.replace(year=current_year)
# 如果算出的是未来时间(比如12月在1月跑), 可能是去年的
if dt > now + timedelta(days=1):
dt = dt.replace(year=current_year - 1)
# 如果只有时间,加上年月日
if fmt == "%H:%M":
dt = datetime.combine(now.date(), dt.time())
return dt
except ValueError:
continue
return None
except Exception as e:
logger.warning(f"日期解析内部错误: {e}")
return None
# 如果直接运行此脚本
if __name__ == "__main__":
# 创建RSS更新器
updater = RSSUpdater()
# 获取最新文章ID
latest_id = updater.get_latest_article_id()
if latest_id:
print(f"当前feed.xml中的最新文章ID: {latest_id}")
else:
print("无法获取最新文章ID")