Files
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

321 lines
11 KiB
Python
Executable File

#!/usr/bin/env python3
"""
验证配置文件
确保所有配置都正确设置
"""
import os
import json
import sys
def verify_config():
"""验证配置文件"""
config_path = "/home/ubuntu/nanocode/it_automation_enhanced/config/config.json"
print("🔍 验证IT资讯自动化系统配置")
print("=" * 60)
if not os.path.exists(config_path):
print(f"❌ 配置文件不存在: {config_path}")
return False
try:
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
print("✅ 配置文件加载成功")
print("")
# 1. 检查系统信息
print("1️⃣ 系统信息:")
system = config.get('system', {})
print(f" 名称: {system.get('name', '未设置')}")
print(f" 版本: {system.get('version', '未设置')}")
print(f" 描述: {system.get('description', '未设置')}")
print("")
# 2. 检查路径配置
print("2️⃣ 路径配置:")
paths = config.get('paths', {})
for key, path in paths.items():
if os.path.exists(path):
print(f" ✅ {key}: {path}")
else:
# 检查是否是文件路径
dir_path = os.path.dirname(path) if '.' in os.path.basename(path) else path
if os.path.exists(dir_path):
print(f" ⚠️ {key}: {path} (目录存在,文件可能不存在)")
else:
print(f" ❌ {key}: {path} (路径不存在)")
print("")
# 3. 检查抓取配置
print("3️⃣ 抓取配置:")
crawler = config.get('crawler', {})
print(f" 最小字数: {crawler.get('min_word_count', '未设置')}")
print(f" 最大天数: {crawler.get('max_days_old', '未设置')}")
websites = config.get('websites', [])
enabled_count = sum(1 for w in websites if w.get('enabled', True))
print(f" 网站数量: {len(websites)} (启用: {enabled_count})")
for website in websites:
status = "✅" if website.get('enabled', True) else "⏸️"
print(f" {status} {website.get('name', '未命名')}: {website.get('url', '无URL')}")
print("")
# 4. 检查DeepSeek配置
print("4️⃣ DeepSeek配置:")
deepseek = config.get('deepseek', {})
api_key = deepseek.get('api_key', '')
if api_key and not api_key.startswith('{{'):
print(f" ✅ API密钥: 已设置 ({api_key[:10]}...{api_key[-4:]})")
else:
print(f" ❌ API密钥: 未设置")
processing = config.get('processing', {})
print(f" 翻译启用: {processing.get('enable_translation', False)}")
print(f" 总结启用: {processing.get('enable_summarization', False)}")
print("")
# 5. 检查邮件配置
print("5️⃣ 邮件配置:")
email = config.get('email', {})
smtp_host = email.get('smtp_host', '')
if smtp_host and not smtp_host.startswith('{{'):
print(f" ✅ SMTP服务器: {smtp_host}:{email.get('smtp_port', '未设置')}")
else:
print(f" ❌ SMTP服务器: 未设置")
username = email.get('username', '')
if username and not username.startswith('{{'):
print(f" ✅ 用户名: {username}")
else:
print(f" ❌ 用户名: 未设置")
password = email.get('password', '')
if password and not password.startswith('{{'):
print(f" ✅ 密码: 已设置 ({password[:2]}...{password[-2:]})")
else:
print(f" ❌ 密码: 未设置")
from_email = email.get('from_email', '')
to_email = email.get('to_email', '')
print(f" 发件人: {from_email}")
print(f" 收件人: {to_email}")
print(f" 每日摘要: {email.get('enable_daily_digest', False)}")
print("")
# 6. 检查小红书配置
print("6️⃣ 小红书配置:")
xhs = config.get('xiaohongshu', {})
print(f" 发布启用: {xhs.get('enable_publish', False)}")
cookie_file = xhs.get('cookie_file', '')
if os.path.exists(cookie_file):
size = os.path.getsize(cookie_file)
print(f" ✅ Cookie文件: {cookie_file} ({size:,} 字节)")
else:
print(f" ❌ Cookie文件: {cookie_file} (不存在)")
xhs_cli = xhs.get('xhs_cli_path', '')
if os.path.exists(xhs_cli):
print(f" ✅ xhs-cli: {xhs_cli}")
else:
print(f" ⚠️ xhs-cli: {xhs_cli} (可能未安装)")
tags = xhs.get('default_tags', [])
print(f" 默认标签: {', '.join(tags[:3])}...")
print("")
# 7. 检查调度配置
print("7️⃣ 调度配置:")
scheduler = config.get('scheduler', {})
print(f" Cron表达式: {scheduler.get('cron_expression', '未设置')}")
print(f" 时区: {scheduler.get('timezone', '未设置')}")
print(f" 日志保留: {scheduler.get('log_retention_days', '未设置')} 天")
print("")
# 8. 综合评估
print("8️⃣ 综合评估:")
issues = []
# 检查关键配置
if not api_key or api_key.startswith('{{'):
issues.append("DeepSeek API密钥未设置")
if not smtp_host or smtp_host.startswith('{{'):
issues.append("邮件SMTP服务器未设置")
if not username or username.startswith('{{'):
issues.append("邮件用户名未设置")
if not password or password.startswith('{{'):
issues.append("邮件密码未设置")
if not os.path.exists(cookie_file):
issues.append("小红书Cookie文件不存在")
if issues:
print(" ⚠️ 发现以下问题:")
for issue in issues:
print(f" • {issue}")
else:
print(" ✅ 所有关键配置都已正确设置")
print("")
print("=" * 60)
if issues:
print("❌ 配置验证失败,请修复上述问题")
return False
else:
print("✅ 配置验证通过,系统可以正常运行")
return True
except Exception as e:
print(f"❌ 配置验证失败: {e}")
return False
def test_email_config():
"""测试邮件配置"""
print("📧 测试邮件配置...")
try:
import smtplib
from email.mime.text import MIMEText
config_path = "/home/ubuntu/nanocode/it_automation_enhanced/config/config.json"
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
email = config.get('email', {})
smtp_host = email.get('smtp_host')
smtp_port = email.get('smtp_port', 465)
username = email.get('username')
password = email.get('password')
if not all([smtp_host, username, password]):
print(" ⚠️ 邮件配置不完整,跳过测试")
return False
print(f" 连接 {smtp_host}:{smtp_port}...")
try:
if smtp_port == 465:
server = smtplib.SMTP_SSL(smtp_host, smtp_port)
else:
server = smtplib.SMTP(smtp_host, smtp_port)
server.starttls()
server.login(username, password)
server.quit()
print(" ✅ 邮件服务器连接成功")
return True
except Exception as e:
print(f" ❌ 邮件服务器连接失败: {e}")
return False
except Exception as e:
print(f" ❌ 邮件配置测试失败: {e}")
return False
def test_xhs_config():
"""测试小红书配置"""
print("📱 测试小红书配置...")
try:
import subprocess
config_path = "/home/ubuntu/nanocode/it_automation_enhanced/config/config.json"
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
xhs = config.get('xiaohongshu', {})
cookie_file = xhs.get('cookie_file', '')
xhs_cli = xhs.get('xhs_cli_path', '/home/ubuntu/.local/bin/xhs')
# 检查xhs-cli
if not os.path.exists(xhs_cli):
print(f" ❌ xhs-cli不存在: {xhs_cli}")
return False
# 检查版本
try:
result = subprocess.run([xhs_cli, '--version'], capture_output=True, text=True, timeout=5)
if result.returncode == 0:
print(f" ✅ xhs-cli版本: {result.stdout.strip()}")
else:
print(f" ⚠️ xhs-cli版本检查失败: {result.stderr}")
except:
print(f" ⚠️ xhs-cli版本检查失败")
# 检查Cookie文件
if os.path.exists(cookie_file):
size = os.path.getsize(cookie_file)
print(f" ✅ Cookie文件存在: {cookie_file} ({size:,} 字节)")
# 检查Cookie格式
try:
with open(cookie_file, 'r', encoding='utf-8') as f:
content = f.read()
if 'xiaohongshu.com' in content:
print(f" ✅ Cookie包含小红书域名")
else:
print(f" ⚠️ Cookie可能不包含小红书域名")
except:
print(f" ⚠️ 无法读取Cookie文件内容")
return True
else:
print(f" ❌ Cookie文件不存在: {cookie_file}")
return False
except Exception as e:
print(f" ❌ 小红书配置测试失败: {e}")
return False
def main():
"""主函数"""
print("🚀 IT资讯自动化系统配置验证")
print("=" * 60)
# 验证配置
config_ok = verify_config()
if config_ok:
print("")
print("🧪 开始功能测试...")
print("")
# 测试邮件配置
email_ok = test_email_config()
print("")
# 测试小红书配置
xhs_ok = test_xhs_config()
print("")
# 总结
print("=" * 60)
print("📋 测试结果总结:")
print(f" 配置验证: {'✅ 通过' if config_ok else '❌ 失败'}")
print(f" 邮件配置: {'✅ 正常' if email_ok else '⚠️ 需要检查'}")
print(f" 小红书配置: {'✅ 正常' if xhs_ok else '⚠️ 需要检查'}")
print("")
if config_ok and email_ok and xhs_ok:
print("🎉 所有测试通过,系统可以正常运行!")
return 0
else:
print("⚠️ 部分测试失败,请检查配置")
return 1
else:
print("❌ 配置验证失败,请修复配置问题")
return 1
if __name__ == "__main__":
sys.exit(main())