Implement manual crawling feature in GXGPMonitorApp, allowing users to filter announcements by date and keywords. Update Markdown generation to include time period and keyword details. Remove outdated onu.md file. Enhance WeChat message handling to support manual crawl results and improve logging for better traceability.
This commit is contained in:
+67
-8
@@ -89,10 +89,17 @@ class GXGPMonitorApp:
|
||||
print(f"应用初始化失败: {str(e)}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
def run_crawl(self, keywords: list = None, sources: list = None, max_pages: int = None):
|
||||
"""执行爬取任务"""
|
||||
def run_crawl(self, keywords: list = None, sources: list = None, max_pages: int = None, manual_crawl: bool = False):
|
||||
"""执行爬取任务
|
||||
|
||||
Args:
|
||||
keywords: 关键词列表
|
||||
sources: 来源列表
|
||||
max_pages: 最大页数
|
||||
manual_crawl: 是否为手动爬取(只筛选今天的数据)
|
||||
"""
|
||||
try:
|
||||
logger.info("开始执行爬取任务")
|
||||
logger.info(f"开始执行爬取任务 (手动爬取: {manual_crawl})")
|
||||
|
||||
# 执行爬取
|
||||
crawl_results = crawl_announcements(keywords, sources)
|
||||
@@ -115,21 +122,69 @@ class GXGPMonitorApp:
|
||||
logger.info(f"保存所有公告完成:共保存 {all_saved_count} 条,按来源统计: {all_saved_stats}")
|
||||
|
||||
# 筛选公告
|
||||
filter_obj = filter_from_config()
|
||||
filtered_announcements, filter_stats = filter_obj.filter(all_announcements)
|
||||
if manual_crawl:
|
||||
# 手动爬取:只筛选今天的公告和用户指定的关键词,不进行去重
|
||||
from .filters.filters import KeywordFilter, DateFilter, SourceFilter
|
||||
from datetime import date
|
||||
|
||||
# 1. 日期筛选:只保留今天的公告
|
||||
date_filter = DateFilter()
|
||||
date_filtered = date_filter.filter_announcements(all_announcements, start_date=date.today(), end_date=date.today())
|
||||
|
||||
# 2. 关键词筛选
|
||||
keyword_filter = KeywordFilter()
|
||||
keyword_filtered = keyword_filter.filter_announcements(date_filtered, keywords=keywords or [])
|
||||
|
||||
# 3. 来源筛选
|
||||
source_filter = SourceFilter()
|
||||
filtered_announcements = source_filter.filter_announcements(keyword_filtered, sources or list(self.config.sources.keys()))
|
||||
|
||||
# 计算统计信息
|
||||
filter_stats = type('FilterResult', (), {
|
||||
"keyword_filtered": len(date_filtered) - len(keyword_filtered),
|
||||
"date_filtered": len(all_announcements) - len(date_filtered),
|
||||
"duplicate_filtered": 0, # 手动爬取不进行去重
|
||||
"source_filtered": len(keyword_filtered) - len(filtered_announcements)
|
||||
})()
|
||||
else:
|
||||
# 自动爬取:筛选出新公告并应用关键词筛选
|
||||
# 1. 筛选出数据库中没有的新公告
|
||||
new_announcements = [ann for ann in all_announcements if ann.is_new]
|
||||
logger.info(f"筛选出 {len(new_announcements)} 条新公告")
|
||||
|
||||
# 2. 对新公告应用关键词筛选等
|
||||
if new_announcements:
|
||||
filter_obj = filter_from_config()
|
||||
filtered_announcements, filter_stats = filter_obj.filter(new_announcements)
|
||||
# 更新统计信息,加上未筛选的新公告数量
|
||||
filter_stats.keyword_filtered += len(new_announcements) - len(filtered_announcements)
|
||||
else:
|
||||
filtered_announcements = []
|
||||
filter_stats = type('FilterResult', (), {
|
||||
"keyword_filtered": 0,
|
||||
"date_filtered": 0,
|
||||
"duplicate_filtered": len(all_announcements),
|
||||
"source_filtered": 0
|
||||
})()
|
||||
|
||||
logger.info(f"筛选后剩余 {len(filtered_announcements)} 条公告")
|
||||
|
||||
# 保存筛选后的公告(用于标记关键词匹配等)
|
||||
saved_count = save_announcements_to_storage(filtered_announcements)
|
||||
|
||||
# 生成Markdown文件
|
||||
md_success = generate_onu_md(filtered_announcements)
|
||||
# 生成Markdown文件(只在自动爬取时生成)
|
||||
md_success = False
|
||||
if not manual_crawl:
|
||||
md_success = generate_onu_md(filtered_announcements)
|
||||
|
||||
# 发送通知
|
||||
notify_success = False
|
||||
if filtered_announcements and self.config.wechat_app.enabled:
|
||||
if not manual_crawl and filtered_announcements and self.config.wechat_app.enabled:
|
||||
# 自动爬取时发送卡片消息
|
||||
notify_success = send_announcements_notification(filtered_announcements)
|
||||
elif manual_crawl and filtered_announcements and self.config.wechat_app.enabled:
|
||||
# 手动爬取时不在这里发送消息,由消息处理器负责发送markdown消息
|
||||
notify_success = True # 标记为成功,因为消息会通过其他方式发送
|
||||
|
||||
result = {
|
||||
"success": True,
|
||||
@@ -146,6 +201,10 @@ class GXGPMonitorApp:
|
||||
}
|
||||
}
|
||||
|
||||
# 对于手动爬取,额外返回筛选后的公告列表
|
||||
if manual_crawl:
|
||||
result["filtered_announcements"] = filtered_announcements
|
||||
|
||||
logger.info(f"爬取任务完成: {result}")
|
||||
return result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user