|
5 | 5 | from typing import Optional, Any
|
6 | 6 | import json
|
7 | 7 |
|
| 8 | +from io import BytesIO |
| 9 | +import base64 |
| 10 | + |
8 | 11 | import backoff
|
9 | 12 | import requests
|
10 | 13 | # pylint: disable=unused-import
|
|
13 | 16 | from bs4 import BeautifulSoup
|
14 | 17 |
|
15 | 18 | from selenium.common.exceptions import NoSuchElementException, TimeoutException
|
16 |
| -from selenium.webdriver import Chrome |
| 19 | +from selenium.webdriver import Chrome, Keys |
17 | 20 | from selenium.webdriver.common.by import By
|
18 | 21 | from selenium.webdriver.support import expected_conditions as EC
|
19 | 22 | from selenium.webdriver.support.wait import WebDriverWait
|
| 23 | +from selenium.webdriver.common.action_chains import ActionChains |
20 | 24 |
|
21 | 25 | from flathunter import proxies
|
22 | 26 | from flathunter.captcha.captcha_solver import CaptchaUnsolvableError
|
@@ -196,6 +200,7 @@ def resolve_geetest(self, driver):
|
196 | 200 | driver.refresh()
|
197 | 201 | raise
|
198 | 202 |
|
| 203 | + # pylint: disable=too-many-locals |
199 | 204 | @backoff.on_exception(wait_gen=backoff.constant,
|
200 | 205 | exception=CaptchaUnsolvableError,
|
201 | 206 | max_tries=3)
|
@@ -268,6 +273,62 @@ def log_filter(log_):
|
268 | 273 | driver.refresh()
|
269 | 274 | raise
|
270 | 275 |
|
| 276 | + @backoff.on_exception(wait_gen=backoff.constant, |
| 277 | + exception=CaptchaUnsolvableError, |
| 278 | + max_tries=3) |
| 279 | + def resolve_amazon(self, driver): |
| 280 | + """Resolve Amazon Captcha""" |
| 281 | + try: |
| 282 | + driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") |
| 283 | + sleep(3) |
| 284 | + shadowelement = driver.execute_script( |
| 285 | + "return document.querySelector('awswaf-captcha').shadowRoot" |
| 286 | + ) |
| 287 | + my_img = shadowelement.find_element(By.ID, "root") |
| 288 | + size = my_img.size |
| 289 | + select_l = my_img.find_element(By.TAG_NAME, "select") |
| 290 | + select_l.click() |
| 291 | + sleep(1) |
| 292 | + select_l.send_keys(Keys.DOWN) |
| 293 | + sleep(3) |
| 294 | + shadowelement = driver.execute_script( |
| 295 | + "return document.querySelector('awswaf-captcha').shadowRoot" |
| 296 | + ) |
| 297 | + my_img = shadowelement.find_element(By.ID, "root") |
| 298 | + screenshot = my_img.screenshot_as_png |
| 299 | + screenshot_bytes = BytesIO(screenshot) |
| 300 | + base64_screenshot = base64.b64encode(screenshot_bytes.getvalue()).decode('utf-8') |
| 301 | + # Send image in 2captcha service |
| 302 | + result = self.captcha_solver.solve_amazon(base64_screenshot) |
| 303 | + logger.info(result.token) |
| 304 | + l = result.token.split(':')[1].split(';') |
| 305 | + l = [[int(val.split('=')[1]) for val in coord.split(',')] for coord in l] |
| 306 | + button_coord = [size['width'] - 30, size['height'] - 30] |
| 307 | + l.append(button_coord) |
| 308 | + actions = ActionChains(driver) |
| 309 | + for i in l: |
| 310 | + actions.move_to_element_with_offset(my_img, i[0] - 160, i[1] - 211).click() |
| 311 | + actions.perform() |
| 312 | + sleep(0.5) |
| 313 | + actions.reset_actions() |
| 314 | + sleep(1) |
| 315 | + try: |
| 316 | + confirm_button = my_img.find_element(By.ID, "amzn-btn-verify-internal") |
| 317 | + actions.move_to_element_with_offset(confirm_button, 40, 15).click() |
| 318 | + actions.perform() |
| 319 | + sleep(4) |
| 320 | + except NoSuchElementException: |
| 321 | + pass |
| 322 | + try: |
| 323 | + driver.find_element(By.TAG_NAME, "awswaf-captcha") |
| 324 | + except NoSuchElementException: |
| 325 | + logger.info("Captcha solved") |
| 326 | + else: |
| 327 | + raise CaptchaUnsolvableError() |
| 328 | + except Exception as ex: |
| 329 | + driver.refresh() |
| 330 | + raise CaptchaUnsolvableError() from ex |
| 331 | + |
271 | 332 | @backoff.on_exception(wait_gen=backoff.constant,
|
272 | 333 | exception=CaptchaUnsolvableError,
|
273 | 334 | max_tries=3)
|
|
0 commit comments