novel-doomsday-resurgence/tools/fix_critical_titles.py

93 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
修复最关键的标题问题
"""
import os
import re
CHAPTERS_DIR = "/root/.openclaw/workspace/projects/末日重生_囤货/chapters"
# 最需要修复的标题评分C级或以下
CRITICAL_FIXES = {
'筹码_手动修复': '致命筹码',
'修复': '心灵修复',
'对峙2': '生死对峙',
}
def fix_critical_titles():
"""修复最关键的标题"""
print("🔧 修复最关键标题问题")
print("=" * 50)
# 获取所有章节文件
chapter_files = [f for f in os.listdir(CHAPTERS_DIR) if f.endswith('.md')]
# 查找需要修复的文件
files_to_fix = []
for filename in chapter_files:
# 提取原标题
match = re.search(r'ch\d+-第\d+章\s+(.+)\.md', filename)
if not match:
continue
old_title = match.group(1)
# 检查是否需要修复
if old_title in CRITICAL_FIXES:
new_title = CRITICAL_FIXES[old_title]
files_to_fix.append((filename, old_title, new_title))
if not files_to_fix:
print("✅ 没有需要修复的关键标题")
return
print(f"发现 {len(files_to_fix)} 个需要修复的关键标题:")
for filename, old_title, new_title in files_to_fix:
print(f" {old_title}{new_title}")
print("\n🔄 开始修复...")
# 执行修复
fixed_count = 0
for filename, old_title, new_title in files_to_fix:
filepath = os.path.join(CHAPTERS_DIR, filename)
# 读取文件内容
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# 提取章节号
match = re.search(r'ch(\d+)-第\d+章\s+(.+)\.md', filename)
chapter_num = match.group(1)
# 构建新文件名
new_filename = f"ch{chapter_num}-第{chapter_num}{new_title}.md"
new_filepath = os.path.join(CHAPTERS_DIR, new_filename)
# 更新标题
old_header = f"# 第{chapter_num}{old_title}"
new_header = f"# 第{chapter_num}{new_title}"
if old_header in content:
content = content.replace(old_header, new_header, 1)
# 写入文件
with open(new_filepath, 'w', encoding='utf-8') as f:
f.write(content)
# 如果文件名改变,删除旧文件
if new_filename != filename:
os.remove(filepath)
print(f"✅ 修复: {filename}{new_filename}")
else:
print(f"✅ 更新标题: {old_title}{new_title}")
fixed_count += 1
print(f"\n📊 修复完成! 共修复 {fixed_count} 个关键标题")
if __name__ == '__main__':
fix_critical_titles()