diff --git a/yunke-server/scripts/scrape_via_page.py b/yunke-server/scripts/scrape_via_page.py index a6f29fc..ebac360 100644 --- a/yunke-server/scripts/scrape_via_page.py +++ b/yunke-server/scripts/scrape_via_page.py @@ -1890,25 +1890,28 @@ async def scrape_chatroom_stats(cookie_str: str, day: str | None = None): pruned = 0 if day_key is None and group_recs and declared and len(group_recs) >= declared: import sqlalchemy as sa - from sqlalchemy import tuple_ as sa_tuple from app.db.base import async_session_maker from app.db.models import ChatGroup keep = {(r["owner_account_id"], r["room_id"]) for r in group_recs} - keep_list = list(keep) async with async_session_maker() as session: - for i in range(0, len(keep_list), 500): - chunk = keep_list[i:i + 500] - res = await session.execute( + local = (await session.execute( + sa.select(ChatGroup.owner_account_id, ChatGroup.room_id) + .where(ChatGroup.channel == "wechat") + )).all() + stale = [(o, r) for (o, r) in local if (o, r) not in keep] + for owner, room in stale: + # 逐行删(差集常态 0~几行;不用 tuple IN——asyncmy 对其参数 + # 形态挑剔,实测 TypeError) + await session.execute( sa.delete(ChatGroup).where( ChatGroup.channel == "wechat", - sa.tuple_( - ChatGroup.owner_account_id, ChatGroup.room_id - ).not_in(sa_tuple(chunk)), + ChatGroup.owner_account_id == owner, + ChatGroup.room_id == room, ) ) - pruned += int(res.rowcount or 0) + pruned = len(stale) await session.commit() logger.info(