fix(douyin login): 二维码提取失败不致命 + 登录成功判定放宽
- _save_douyin_qrcode 定位不到二维码元素时不再抛错中止(有头浏览器可直接扫码),仅警告并继续轮询等登录跳转 - _is_douyin_login_completed 放宽到 creator-micro 任意页(不止 /home) - Path(image_path) 对空路径做守卫
This commit is contained in:
@@ -106,7 +106,12 @@ async def _extract_douyin_qrcode_src(page: Page) -> str:
|
||||
|
||||
|
||||
async def _save_douyin_qrcode(page: Page, account_file: str, previous_qrcode_path: Path | None = None, qrcode_callback=None) -> dict:
|
||||
qrcode_src = await _extract_douyin_qrcode_src(page)
|
||||
# 提取二维码 src 仅为了保存/终端显示;定位不到时不致命——有头浏览器里二维码可见,直接扫码即可
|
||||
try:
|
||||
qrcode_src = await _extract_douyin_qrcode_src(page)
|
||||
except Exception as exc:
|
||||
douyin_logger.warning(_msg("😵", f"没定位到二维码元素({str(exc)[:50]})——请直接在弹出的浏览器里扫码,小人继续等登录跳转"))
|
||||
return {"image_path": "", "image_data_url": ""}
|
||||
qrcode_path = save_data_url_image(qrcode_src, build_login_qrcode_path(account_file))
|
||||
if previous_qrcode_path and previous_qrcode_path != qrcode_path:
|
||||
if remove_qrcode_file(previous_qrcode_path):
|
||||
@@ -126,7 +131,8 @@ async def _save_douyin_qrcode(page: Page, account_file: str, previous_qrcode_pat
|
||||
|
||||
|
||||
async def _is_douyin_login_completed(page: Page) -> bool:
|
||||
if not page.url.startswith("https://creator.douyin.com/creator-micro/home"):
|
||||
# 登录后会跳到 creator-micro 下任意页(home/content 等);登录页是 creator.douyin.com/ 根路径
|
||||
if "creator.douyin.com/creator-micro" not in page.url:
|
||||
return False
|
||||
|
||||
login_markers = [
|
||||
@@ -149,7 +155,7 @@ async def _is_douyin_login_completed(page: Page) -> bool:
|
||||
|
||||
|
||||
async def _wait_for_douyin_login(page: Page, account_file: str, qrcode_info: dict, qrcode_callback=None, poll_interval: int = 3, max_checks: int = 100) -> dict:
|
||||
qrcode_path = Path(qrcode_info["image_path"])
|
||||
qrcode_path = Path(qrcode_info["image_path"]) if qrcode_info.get("image_path") else None
|
||||
for _ in range(max_checks):
|
||||
if await _is_douyin_login_completed(page):
|
||||
douyin_logger.info(_msg("🥳", f"扫码成功,已经跳转到登录后页面: {page.url}"))
|
||||
@@ -161,7 +167,7 @@ async def _wait_for_douyin_login(page: Page, account_file: str, qrcode_info: dic
|
||||
await expired_box.click()
|
||||
await asyncio.sleep(1)
|
||||
qrcode_info = await _save_douyin_qrcode(page, account_file, qrcode_path, qrcode_callback=qrcode_callback)
|
||||
qrcode_path = Path(qrcode_info["image_path"])
|
||||
qrcode_path = Path(qrcode_info["image_path"]) if qrcode_info.get("image_path") else None
|
||||
|
||||
await asyncio.sleep(poll_interval)
|
||||
|
||||
@@ -185,7 +191,7 @@ async def douyin_cookie_gen(
|
||||
page = await context.new_page()
|
||||
await page.goto("https://creator.douyin.com/")
|
||||
qrcode_info = await _save_douyin_qrcode(page, account_file, qrcode_callback=qrcode_callback)
|
||||
qrcode_path = Path(qrcode_info["image_path"])
|
||||
qrcode_path = Path(qrcode_info["image_path"]) if qrcode_info.get("image_path") else None
|
||||
douyin_logger.info(_msg("🧍", "请扫码,小人正在耐心等待登录完成"))
|
||||
result = await _wait_for_douyin_login(
|
||||
page,
|
||||
|
||||
Reference in New Issue
Block a user