获取reCAPTCHA token - 支持多种打码方式 Args: project_id: 项目ID action: reCAPTCHA action类型 - IMAGE_GENERATION: 图片生成和2K/4K图片放大 (默认) - VIDEO_GENERATION: 视频生成和视频放大 token_id: 当前业务 token id(browser 模式下用于读取 token 级打码代理)
(
self,
project_id: str,
action: str = "IMAGE_GENERATION",
token_id: Optional[int] = None
)
| 4510 | return max(12, min(base_timeout, target_timeout)) |
| 4511 | |
| 4512 | async def _get_recaptcha_token( |
| 4513 | self, |
| 4514 | project_id: str, |
| 4515 | action: str = "IMAGE_GENERATION", |
| 4516 | token_id: Optional[int] = None |
| 4517 | ) -> tuple[Optional[str], Optional[Union[int, str]]]: |
| 4518 | """获取reCAPTCHA token - 支持多种打码方式 |
| 4519 | |
| 4520 | Args: |
| 4521 | project_id: 项目ID |
| 4522 | action: reCAPTCHA action类型 |
| 4523 | - IMAGE_GENERATION: 图片生成和2K/4K图片放大 (默认) |
| 4524 | - VIDEO_GENERATION: 视频生成和视频放大 |
| 4525 | token_id: 当前业务 token id(browser 模式下用于读取 token 级打码代理) |
| 4526 | |
| 4527 | Returns: |
| 4528 | (token, browser_id) 元组。 |
| 4529 | - browser 模式: browser_id 为本地浏览器 ID |
| 4530 | - remote_browser 模式: browser_id 为远程 session_id |
| 4531 | - 其他模式: browser_id 为 None |
| 4532 | """ |
| 4533 | captcha_method = config.captcha_method |
| 4534 | debug_logger.log_info(f"[reCAPTCHA] 开始获取 token: method={captcha_method}, project_id={project_id}, action={action}") |
| 4535 | |
| 4536 | if captcha_method == "extension": |
| 4537 | try: |
| 4538 | from .browser_captcha_extension import ExtensionCaptchaService |
| 4539 | service = await ExtensionCaptchaService.get_instance(self.db) |
| 4540 | extension_timeout = 45 if action == "VIDEO_GENERATION" else 25 |
| 4541 | token = await service.get_token( |
| 4542 | project_id, |
| 4543 | action, |
| 4544 | timeout=extension_timeout, |
| 4545 | token_id=token_id |
| 4546 | ) |
| 4547 | self._set_request_fingerprint(None) |
| 4548 | return token, None |
| 4549 | except Exception as e: |
| 4550 | debug_logger.log_error(f"[reCAPTCHA Extension] 错误: {str(e)}") |
| 4551 | self._set_request_fingerprint(None) |
| 4552 | return None, None |
| 4553 | |
| 4554 | # 内置浏览器打码 (nodriver) |
| 4555 | if captcha_method == "personal": |
| 4556 | debug_logger.log_info(f"[reCAPTCHA] 使用 personal 模式") |
| 4557 | try: |
| 4558 | from .browser_captcha_personal import BrowserCaptchaService |
| 4559 | debug_logger.log_info(f"[reCAPTCHA] 导入 BrowserCaptchaService 成功") |
| 4560 | service = await BrowserCaptchaService.get_instance(self.db) |
| 4561 | debug_logger.log_info(f"[reCAPTCHA] 获取服务实例成功,准备调用 get_token") |
| 4562 | solve_bundle = None |
| 4563 | get_token_bundle = getattr(service, "get_token_bundle", None) |
| 4564 | if callable(get_token_bundle): |
| 4565 | solve_bundle = await get_token_bundle( |
| 4566 | project_id, |
| 4567 | action, |
| 4568 | token_id=token_id, |
| 4569 | ) |
no test coverage detected