md文档更新

This commit is contained in:
2025-08-05 17:13:19 +08:00
parent e5da1203c0
commit fad2b2d1c8
6 changed files with 361 additions and 48 deletions
+42 -46
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
情报收集系统主程序
情报收集系统主程序(明文配置版)
功能:
1. 调度数据采集、处理、存储流程
2. 生成日报/月报
@@ -10,42 +10,32 @@
import sys
import logging
from datetime import datetime, timedelta
from typing import List, Dict, Any
import time
import threading
from datetime import datetime
from typing import Dict, List, Any
# 自定义模块
from config.settings import API_KEYS, DATA_SOURCES
from collectors.news_api import NewsAPICollector
from collectors.complaint_spider import ComplaintSpider
from processors.data_processor import DataProcessor
from storage.database import IntelligenceDB
from applications.reporter import ReportGenerator
from applications.alert import AlertService
from utils.logger import setup_logging
from utils.mail import send_email
class IntelligenceSystem:
def __init__(self):
# 初始化核心组件
setup_logging()
self.logger = logging.getLogger(__name__)
self.db = IntelligenceDB()
self.processor = DataProcessor()
self.alert = AlertService()
# 数据采集器注册
self.collectors = {
"news": NewsAPICollector(API_KEYS['newsapi']),
"complaint": ComplaintSpider(
base_url=DATA_SOURCES['blackcat'],
rate_limit=30 # 30秒爬取间隔
)
}
def run_daily_pipeline(self):
"""每日数据采集处理流程"""
try:
self.logger.info("开始执行每日数据采集流程")
# 阶段1:数据采集
raw_data = self._collect_data()
@@ -61,6 +51,7 @@ class IntelligenceSystem:
# 阶段5:异常检测
self._check_alerts()
self.logger.info("每日流程执行完成")
except Exception as e:
self.logger.error(f"主流程执行失败: {str(e)}", exc_info=True)
self.alert.send_critical(f"系统异常: {str(e)}")
@@ -128,58 +119,63 @@ class IntelligenceSystem:
"""生成报告并发送"""
try:
# 日报生成
report_date = datetime.now().date()
report_html = ReportGenerator(self.db).generate_daily()
with open(f"reports/daily_{datetime.now().date()}.html", 'w') as f:
report_path = f"reports/daily_{report_date}.html"
with open(report_path, 'w', encoding='utf-8') as f:
f.write(report_html)
self.logger.info(f"日报已生成: {report_path}")
# 每月1号生成月报
if datetime.now().day == 1:
monthly_report = ReportGenerator(self.db).generate_monthly()
send_email(
to="team@example.com",
subject=f"{datetime.now().strftime('%Y-%m')} 情报月报",
content=monthly_report
)
# 这里替换为实际的邮件发送逻辑
self.logger.info("月度报告已生成(邮件发送功能需配置)")
except Exception as e:
self.logger.error(f"报告生成失败: {str(e)}")
def _check_alerts(self):
"""检查预警信息"""
# 负面舆情监测
negative_keywords = ['投诉', '造假', '违规']
alerts = self.alert.check_negative(negative_keywords)
if alerts:
self.alert.send_urgent(
"负面舆情警报",
"\n".join([f"[{a['source']}] {a['content']}" for a in alerts])
)
alert_msg = "\n".join([f"[{a['source']}] {a['content']}" for a in alerts])
self.logger.warning(f"发现负面舆情:\n{alert_msg}")
# 这里替换为实际的通知发送逻辑
self.alert.send_urgent("负面舆情警报", alert_msg)
def cleanup(self):
"""资源清理"""
self.db.close()
self.logger.info("系统资源已释放")
if __name__ == "__main__":
def run_scheduled():
"""定时任务执行入口"""
system = IntelligenceSystem()
try:
# 执行每日任务
if len(sys.argv) > 1 and sys.argv[1] == "--manual":
while True:
now = datetime.now()
if now.hour == 9 and now.minute == 0: # 每天9点执行
system.run_daily_pipeline()
time.sleep(60) # 避免重复执行
time.sleep(30)
except KeyboardInterrupt:
system.cleanup()
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "--manual":
# 手动执行模式
system = IntelligenceSystem()
try:
system.logger.info("手动执行模式启动")
system.run_daily_pipeline()
else:
# 定时任务模式(实际部署时改用crontab或APScheduler
system.logger.info("定时任务模式启动")
while True:
now = datetime.now()
if now.hour == 9 and now.minute == 0: # 每天9点执行
system.run_daily_pipeline()
time.sleep(60) # 避免重复执行
time.sleep(30)
except KeyboardInterrupt:
system.logger.info("用户中断执行")
finally:
system.cleanup()
finally:
system.cleanup()
else:
# 定时任务模式
print("情报收集系统已启动(定时模式)")
print("按 Ctrl+C 退出")
run_scheduled()