fix(tencent): wait for real-name verification

This commit is contained in:
tankztz
2026-08-10 02:09:51 +08:00
parent 8ee7bcdb8a
commit ad2419ef16
2 changed files with 71 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
import asyncio
from pathlib import Path
from patchright.async_api import async_playwright
from uploader.tencent_uploader.main import TencentBaseUploader
def test_wait_for_realtime_verification_saves_qr_and_resumes(tmp_path: Path):
async def scenario():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=True, channel="chrome")
page = await browser.new_page()
await page.set_content(
"""
<div class="weui-desktop-dialog__wrp">
<div>为保障账号安全,需用管理员微信扫码完成实名验证后才能继续发表</div>
<div class="qr">QR</div>
</div>
"""
)
uploader = TencentBaseUploader(publish_date=0, account_file="unused")
qr_path = tmp_path / "verification.png"
async def dismiss_dialog():
await page.wait_for_timeout(150)
await page.locator("div.weui-desktop-dialog__wrp").evaluate("el => el.remove()")
dismiss_task = asyncio.create_task(dismiss_dialog())
result = await uploader.wait_for_realtime_verification(
page,
qr_path=qr_path,
timeout_seconds=2,
poll_interval_seconds=0.05,
)
await dismiss_task
await browser.close()
assert result == qr_path
assert qr_path.is_file()
assert qr_path.stat().st_size > 0
asyncio.run(scenario())
+28
View File
@@ -513,6 +513,33 @@ class TencentBaseUploader(BaseVideoUploader):
else: else:
self.publish_date = 0 self.publish_date = 0
async def wait_for_realtime_verification(
self,
page: Page,
qr_path: str | Path | None = None,
timeout_seconds: float = 10 * 60,
poll_interval_seconds: float = 2,
) -> Path | None:
dialog = page.locator("div.weui-desktop-dialog__wrp:visible").filter(has_text="实名验证").first
if not await dialog.count() or not await dialog.is_visible():
return None
output_path = Path(qr_path) if qr_path else Path(self.account_file).with_name(
f"{Path(self.account_file).stem}_verification_qr.png"
)
output_path.parent.mkdir(parents=True, exist_ok=True)
await dialog.screenshot(path=str(output_path))
tencent_logger.warning(_msg("📱", f"需要管理员微信扫码完成实名验证: {output_path}"))
deadline = asyncio.get_running_loop().time() + timeout_seconds
while await dialog.count() and await dialog.is_visible():
if asyncio.get_running_loop().time() >= deadline:
raise TimeoutError("等待视频号管理员实名验证超时")
await asyncio.sleep(poll_interval_seconds)
tencent_logger.success(_msg("🥳", "管理员实名验证已完成,继续发表"))
return output_path
async def set_schedule_time_tencent(self, page: Page, publish_date: datetime): async def set_schedule_time_tencent(self, page: Page, publish_date: datetime):
label_element = page.locator("label").filter(has_text="定时").nth(1) label_element = page.locator("label").filter(has_text="定时").nth(1)
await label_element.click() await label_element.click()
@@ -946,6 +973,7 @@ class TencentVideo(TencentBaseUploader):
) )
async def prepare_video_for_publish(self, page: Page) -> None: async def prepare_video_for_publish(self, page: Page) -> None:
await self.wait_for_realtime_verification(page)
await self.fill_title_and_tags(page) await self.fill_title_and_tags(page)
await self.fill_description(page) await self.fill_description(page)
await self.apply_collection(page) await self.apply_collection(page)