From dc3ca1f76c7d9fbc567476ef568f0a002e94f4c7 Mon Sep 17 00:00:00 2001 From: v6ole Date: Tue, 12 May 2026 17:38:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9C=80=E6=96=B0=E5=85=AC=E5=91=8A?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E6=97=B6=E8=A7=A6=E5=8F=91=E7=88=AC=E5=8F=96?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 点击时先爬取一次(60秒防重入),爬取期间发送提示 - 查询改为:广西政采网关键词匹配 + 大化县政府网全部,按时间倒序取6条 Co-Authored-By: Claude Sonnet 4.6 --- app/wechat/handler.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/app/wechat/handler.py b/app/wechat/handler.py index a9e0902..f756465 100644 --- a/app/wechat/handler.py +++ b/app/wechat/handler.py @@ -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) )