- Refactor codebase into src/ (preprocessing, translation, assembly) - Add pipeline/ scripts for individual stages - Externalize configuration to config/config.yaml - Fix Cover Image preservation - Update documentation and manuals
102 lines
3.1 KiB
Python
102 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
快速修复和测试脚本
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# 添加 src 目录到路径
|
|
sys.path.insert(0, str(Path(__file__).parent / "src"))
|
|
|
|
from rich.console import Console
|
|
|
|
|
|
def check_api_key():
|
|
"""检查 API Key 设置"""
|
|
console = Console()
|
|
|
|
# 检查环境变量
|
|
env_key = os.environ.get('OPENROUTER_API_KEY')
|
|
if env_key and env_key != 'YOUR_OPENROUTER_API_KEY':
|
|
console.print(f"[green]✓ 环境变量中找到 API Key: {env_key[:10]}...[/green]")
|
|
return True
|
|
|
|
# 检查 .env 文件
|
|
env_file = Path('.env')
|
|
if env_file.exists():
|
|
with open(env_file, 'r') as f:
|
|
content = f.read()
|
|
if 'OPENROUTER_API_KEY=' in content and 'YOUR_OPENROUTER_API_KEY' not in content:
|
|
console.print("[green]✓ .env 文件中找到 API Key[/green]")
|
|
return True
|
|
|
|
# 检查配置文件
|
|
config_file = Path('config/config.json')
|
|
if config_file.exists():
|
|
import json
|
|
try:
|
|
with open(config_file, 'r') as f:
|
|
config = json.load(f)
|
|
api_key = config.get('openrouter', {}).get('api_key', '')
|
|
if api_key and api_key != 'YOUR_OPENROUTER_API_KEY':
|
|
console.print(f"[green]✓ 配置文件中找到 API Key: {api_key[:10]}...[/green]")
|
|
return True
|
|
except Exception as e:
|
|
console.print(f"[red]配置文件读取错误: {e}[/red]")
|
|
|
|
console.print("[red]✗ 未找到有效的 API Key[/red]")
|
|
console.print("\n请设置 OpenRouter API Key:")
|
|
console.print("1. 环境变量: export OPENROUTER_API_KEY='your_key'")
|
|
console.print("2. .env 文件: OPENROUTER_API_KEY=your_key")
|
|
console.print("3. 配置文件: 编辑 config/config.json")
|
|
|
|
return False
|
|
|
|
|
|
def quick_fix():
|
|
"""快速修复常见问题"""
|
|
console = Console()
|
|
console.print("[bold blue]EPUB 翻译器 - 快速修复[/bold blue]\n")
|
|
|
|
# 检查 API Key
|
|
if not check_api_key():
|
|
return False
|
|
|
|
# 检查依赖
|
|
console.print("\n[cyan]检查依赖...[/cyan]")
|
|
|
|
required_modules = [
|
|
'ebooklib', 'bs4', 'lxml', 'openai',
|
|
'aiohttp', 'pydantic', 'loguru', 'rich'
|
|
]
|
|
|
|
missing_modules = []
|
|
for module in required_modules:
|
|
try:
|
|
if module == 'bs4':
|
|
import bs4
|
|
else:
|
|
__import__(module)
|
|
console.print(f"[green]✓ {module}[/green]")
|
|
except ImportError:
|
|
console.print(f"[red]✗ {module}[/red]")
|
|
missing_modules.append(module)
|
|
|
|
if missing_modules:
|
|
console.print(f"\n[red]缺少依赖: {', '.join(missing_modules)}[/red]")
|
|
console.print("请运行: uv pip install -r requirements.txt")
|
|
return False
|
|
|
|
console.print("\n[green]✓ 所有检查通过[/green]")
|
|
return True
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if quick_fix():
|
|
print("\n可以开始使用翻译器了!")
|
|
print("运行: python main.py your_book.epub --test")
|
|
else:
|
|
print("\n请先修复上述问题")
|
|
sys.exit(1) |