| 43 | |
| 44 | |
| 45 | class SyncChallenger: |
| 46 | def __init__( |
| 47 | self, |
| 48 | page: Union[PlaywrightPage, PatchrightPage], |
| 49 | click_timeout: Optional[int] = None, |
| 50 | retry_times: int = 15, |
| 51 | optimize_click_order: Optional[bool] = True, |
| 52 | ) -> None: |
| 53 | """ |
| 54 | Initialize a reCognizer AsyncChallenger instance with specified configurations. |
| 55 | |
| 56 | Args: |
| 57 | page (Page): The Playwright Page to initialize on. |
| 58 | click_timeout (int, optional): Click Timeouts between captcha-clicks. |
| 59 | retry_times (int, optional): Maximum amount of retries before raising an Exception. Defaults to 15. |
| 60 | optimize_click_order (bool, optional): Whether to optimize the click order with the Travelling Salesman Problem. Defaults to True. |
| 61 | """ |
| 62 | self.page: Union[PlaywrightPage, PatchrightPage] = page |
| 63 | self.routed_page = False |
| 64 | self.detector = Detector(optimize_click_order=optimize_click_order) |
| 65 | |
| 66 | self.click_timeout = click_timeout |
| 67 | self.retry_times = retry_times |
| 68 | self.retried = 0 |
| 69 | |
| 70 | self.dynamic: bool = False |
| 71 | self.captcha_token: Optional[str] = None |
| 72 | |
| 73 | def route_handler( |
| 74 | self, |
| 75 | route: Union[PlaywrightRoute, PatchrightRoute], |
| 76 | request: Union[PlaywrightRequest, PatchrightRequest], |
| 77 | ) -> None: |
| 78 | # Instant Fulfillment to save Time |
| 79 | response = route.fetch() |
| 80 | route.fulfill(response=response) # type: ignore[arg-type] |
| 81 | response_text = response.text() |
| 82 | assert response_text |
| 83 | |
| 84 | self.dynamic = "dynamic" in response_text |
| 85 | |
| 86 | # Checking if captcha succeeded |
| 87 | if "userverify" in request.url and "rresp" not in response_text and "bgdata" not in response_text: |
| 88 | match = re.search(r'"uvresp"\s*,\s*"([^"]+)"', response_text) |
| 89 | assert match |
| 90 | self.captcha_token = match.group(1) |
| 91 | |
| 92 | def check_result(self) -> Union[str, None]: |
| 93 | if self.captcha_token: |
| 94 | return self.captcha_token |
| 95 | |
| 96 | with suppress(PlaywrightError, PatchrightError): |
| 97 | captcha_token: str = self.page.evaluate("grecaptcha.getResponse()") |
| 98 | return captcha_token |
| 99 | |
| 100 | with suppress(PlaywrightError, PatchrightError): |
| 101 | enterprise_captcha_token: str = self.page.evaluate("grecaptcha.enterprise.getResponse()") |
| 102 | return enterprise_captcha_token |
no outgoing calls