Skip to content

Commit

Permalink
Merge pull request #85 from Xewdy444/dev
Browse files Browse the repository at this point in the history
Miscellaneous Improvements
  • Loading branch information
Xewdy444 authored Mar 15, 2024
2 parents f32d257 + 0ac573a commit 866001a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
22 changes: 17 additions & 5 deletions playwright_recaptcha/recaptchav2/async_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from concurrent.futures import ThreadPoolExecutor
from io import BytesIO
from json import JSONDecodeError
from typing import Any, BinaryIO, Dict, Iterable, List, Optional, Union
from typing import Any, BinaryIO, Dict, List, Optional, Union

import speech_recognition
from playwright.async_api import Locator, Page, Response
Expand Down Expand Up @@ -213,7 +213,7 @@ async def _get_capsolver_response(
return response_json

async def _solve_tiles(
self, recaptcha_box: AsyncRecaptchaBox, indexes: Iterable[int]
self, recaptcha_box: AsyncRecaptchaBox, indexes: List[int]
) -> None:
"""
Solve the tiles in the reCAPTCHA image challenge.
Expand All @@ -222,7 +222,7 @@ async def _solve_tiles(
----------
recaptcha_box : AsyncRecaptchaBox
The reCAPTCHA box.
indexes : Iterable[int]
indexes : List[int]
The indexes of the tiles that contain the task object.
Raises
Expand All @@ -231,6 +231,8 @@ async def _solve_tiles(
If the CapSolver API returned an error.
"""
changing_tiles: List[Locator] = []
indexes = indexes.copy()
random.shuffle(indexes)

for index in indexes:
tile = recaptcha_box.tile_selector.nth(index)
Expand All @@ -242,6 +244,8 @@ async def _solve_tiles(
await self._random_delay()

while changing_tiles:
random.shuffle(changing_tiles)

for tile in changing_tiles.copy():
if "rc-imageselect-dynamic-selected" in await tile.get_attribute(
"class"
Expand Down Expand Up @@ -520,13 +524,21 @@ async def _solve_audio_challenge(self, recaptcha_box: AsyncRecaptchaBox) -> None
if text is not None:
break

self._payload_response = None

async with self._page.expect_response(
re.compile("/recaptcha/(api2|enterprise)/payload")
re.compile("/recaptcha/(api2|enterprise)/reload")
) as response:
await recaptcha_box.new_challenge_button.click()

await response.value

while self._payload_response is None:
if await recaptcha_box.rate_limit_is_visible():
raise RecaptchaRateLimitError

await self._page.wait_for_timeout(250)

await self._submit_audio_text(recaptcha_box, text)

async def recaptcha_is_visible(self) -> bool:
Expand Down Expand Up @@ -624,7 +636,7 @@ async def solve_recaptcha(
):
await recaptcha_box.audio_challenge_button.click(force=True)

if image_challenge and self._payload_response is None:
if image_challenge:
image = recaptcha_box.image_challenge.locator("img").first
image_url = await image.get_attribute("src")
self._payload_response = await self._page.request.get(image_url)
Expand Down
24 changes: 17 additions & 7 deletions playwright_recaptcha/recaptchav2/sync_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
from io import BytesIO
from json import JSONDecodeError
from typing import Any, Dict, Iterable, List, Optional
from typing import Any, Dict, List, Optional

import speech_recognition
from playwright.sync_api import Locator, Page, Response
Expand Down Expand Up @@ -174,17 +174,15 @@ def _get_capsolver_response(

return response_json

def _solve_tiles(
self, recaptcha_box: SyncRecaptchaBox, indexes: Iterable[int]
) -> None:
def _solve_tiles(self, recaptcha_box: SyncRecaptchaBox, indexes: List[int]) -> None:
"""
Solve the tiles in the reCAPTCHA image challenge.
Parameters
----------
recaptcha_box : SyncRecaptchaBox
The reCAPTCHA box.
indexes : Iterable[int]
indexes : List[int]
The indexes of the tiles that contain the task object.
Raises
Expand All @@ -193,6 +191,8 @@ def _solve_tiles(
If the CapSolver API returned an error.
"""
changing_tiles: List[Locator] = []
indexes = indexes.copy()
random.shuffle(indexes)

for index in indexes:
tile = recaptcha_box.tile_selector.nth(index)
Expand All @@ -204,6 +204,8 @@ def _solve_tiles(
self._random_delay()

while changing_tiles:
random.shuffle(changing_tiles)

for tile in changing_tiles.copy():
if "rc-imageselect-dynamic-selected" in tile.get_attribute("class"):
continue
Expand Down Expand Up @@ -458,11 +460,19 @@ def _solve_audio_challenge(self, recaptcha_box: SyncRecaptchaBox) -> None:
if text is not None:
break

self._payload_response = None

with self._page.expect_response(
re.compile("/recaptcha/(api2|enterprise)/payload")
re.compile("/recaptcha/(api2|enterprise)/reload")
):
recaptcha_box.new_challenge_button.click()

while self._payload_response is None:
if recaptcha_box.rate_limit_is_visible():
raise RecaptchaRateLimitError

self._page.wait_for_timeout(250)

self._submit_audio_text(recaptcha_box, text)

def recaptcha_is_visible(self) -> bool:
Expand Down Expand Up @@ -557,7 +567,7 @@ def solve_recaptcha(
if not image_challenge and recaptcha_box.audio_challenge_button.is_visible():
recaptcha_box.audio_challenge_button.click(force=True)

if image_challenge and self._payload_response is None:
if image_challenge:
image = recaptcha_box.image_challenge.locator("img").first
image_url = image.get_attribute("src")
self._payload_response = self._page.request.get(image_url)
Expand Down

0 comments on commit 866001a

Please sign in to comment.