feat: 最新公告点击时触发爬取并优化查询逻辑
- 点击时先爬取一次(60秒防重入),爬取期间发送提示 - 查询改为:广西政采网关键词匹配 + 大化县政府网全部,按时间倒序取6条 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+24
-2
@@ -13,6 +13,10 @@ logger = logging.getLogger(__name__)
|
||||
_last_crawl_time: float = 0.0
|
||||
_crawl_lock = asyncio.Lock()
|
||||
|
||||
# 防重入:最新公告爬取,60秒内不重复执行
|
||||
_last_latest_time: float = 0.0
|
||||
_latest_lock = asyncio.Lock()
|
||||
|
||||
|
||||
class WeChatMessageHandler:
|
||||
def __init__(self):
|
||||
@@ -237,14 +241,32 @@ class WeChatMessageHandler:
|
||||
await self.client.send_text("查询失败,请稍后再试", from_user)
|
||||
|
||||
async def _handle_latest_announcements(self, from_user: str) -> str | None:
|
||||
from app.api.deps import get_db
|
||||
from sqlalchemy import select, desc
|
||||
global _last_latest_time
|
||||
from app.api.deps import get_db, get_crawl_service
|
||||
from sqlalchemy import select, desc, or_
|
||||
from app.models.announcement import Announcement
|
||||
|
||||
try:
|
||||
# 防重入:60秒内只爬取一次
|
||||
async with _latest_lock:
|
||||
now = time.monotonic()
|
||||
do_crawl = (now - _last_latest_time) >= 60
|
||||
if do_crawl:
|
||||
_last_latest_time = now
|
||||
|
||||
if do_crawl:
|
||||
await self.client.send_text("正在获取最新公告,请稍候...", from_user)
|
||||
service = get_crawl_service()
|
||||
await service.run_all()
|
||||
|
||||
# 查:广西政采网关键词匹配 + 大化县政府网全部
|
||||
async for db in get_db():
|
||||
result = await db.execute(
|
||||
select(Announcement)
|
||||
.where(or_(
|
||||
Announcement.keyword_matched == True, # noqa: E712
|
||||
Announcement.source_code == "dahuagov",
|
||||
))
|
||||
.order_by(desc(Announcement.publish_date))
|
||||
.limit(6)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user