From 662633bb3d52fdf5469861bcfc46f7d6f4456412 Mon Sep 17 00:00:00 2001 From: snow-moonlight1 Date: Thu, 23 Jul 2026 00:32:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(tencent):=20cookie=5Fauth=20=E8=AF=AF?= =?UTF-8?q?=E6=8A=A5=E6=9C=89=E6=95=88=20+=20=E4=B8=8A=E4=BC=A0=E9=A1=B5?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E8=B7=B3=E8=BD=AC=E5=89=8D=E7=BD=AE=E6=A3=80?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 视频号 cookie 失效时, post/create 页面先正常加载, 数秒后才被前端 JS 跳转到 login.html。原 cookie_auth 在跳转前检查, 误报 'cookie 有效', 放行后在上传页报出无关错误(如 '未找到视频号文件上传框')。 - cookie_auth: 等待 8 秒确认是否被踢到 login.html, 外加 qrconnect 登录 iframe 双保险, 消除误报 - open_upload_page: 同样的跳转检测前置, cookie 失效时报明确错误 '请重新扫码登录后再发布' --- uploader/tencent_uploader/main.py | 35 +++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/uploader/tencent_uploader/main.py b/uploader/tencent_uploader/main.py index 0143760..c053970 100644 --- a/uploader/tencent_uploader/main.py +++ b/uploader/tencent_uploader/main.py @@ -113,18 +113,22 @@ async def cookie_auth(account_file): context = await browser.new_context(storage_state=account_file) context = await set_init_script(context) page = await context.new_page() - await page.goto(TENCENT_UPLOAD_URL) - await page.wait_for_url(TENCENT_UPLOAD_URL, timeout=5000) + await page.goto(TENCENT_UPLOAD_URL, wait_until="domcontentloaded") - login_markers = [ - page.get_by_text("扫码登录", exact=True).first, - page.get_by_text("发表视频", exact=True).first, - page.get_by_role("button", name="发表").first, - ] - - if await login_markers[0].count(): - tencent_logger.info(_msg("🥹", "cookie 已失效,得重新登录一下")) + # cookie 失效时, 页面先停在 post/create, 随后由前端 JS 跳转到登录页; + # 必须等待跳转完成再判断, 否则会误报"cookie 有效" + try: + await page.wait_for_url("**/login.html**", timeout=8000) + tencent_logger.info(_msg("🥹", "cookie 已失效(页面跳转到登录页),得重新登录一下")) return False + except Exception: + pass # 8 秒内未跳转, 大概率已登录 + + # 双保险: 页面里出现微信扫码登录 iframe 也视为失效 + for fr in page.frames: + if "open.weixin.qq.com/connect/qrconnect" in fr.url: + tencent_logger.info(_msg("🥹", "cookie 已失效(页面出现扫码登录框),得重新登录一下")) + return False tencent_logger.success(_msg("🥳", "cookie 有效")) return True @@ -541,7 +545,16 @@ class TencentBaseUploader(BaseVideoUploader): async def open_upload_page(self, page: Page) -> None: await page.goto(TENCENT_UPLOAD_URL, timeout=120000, wait_until="domcontentloaded") - await page.wait_for_url(TENCENT_UPLOAD_URL, timeout=120000) + # cookie 失效时前端 JS 会跳转到登录页, 提前发现并报明确的错误 + redirected = True + try: + await page.wait_for_url("**/login.html**", timeout=8000) + except Exception: + redirected = False # 8 秒内未跳转, 正常 + if redirected or any( + "open.weixin.qq.com/connect/qrconnect" in fr.url for fr in page.frames + ): + raise RuntimeError("视频号 cookie 已失效(被跳转到登录页),请重新扫码登录后再发布") async def upload_video_file(self, page: Page, file_path: str) -> None: async def find_file_input():