From 69351e40df637c2a9855800d6ed240fbafb7714a Mon Sep 17 00:00:00 2001 From: Pradish Bijukchhe Date: Mon, 16 Sep 2024 22:37:15 +0545 Subject: [PATCH] feat: add TimeoutError --- pyproject.toml | 2 +- ucaptcha/anticaptcha.py | 4 +++- ucaptcha/capmonster.py | 4 +++- ucaptcha/exceptions.py | 4 ++++ ucaptcha/nocaptchaai.py | 4 +++- ucaptcha/nopecha.py | 4 +++- ucaptcha/solver.py | 7 +++---- ucaptcha/twocaptcha.py | 4 +++- 8 files changed, 23 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e15c5b4..de8158b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "ucaptcha" -version = "1.1.1" +version = "1.1.2" dependencies = ["httpx"] requires-python = ">=3" authors = [{ name = "Pradish Bijukchhe", email = "pradish@sandbox.com.np" }] diff --git a/ucaptcha/anticaptcha.py b/ucaptcha/anticaptcha.py index 3c88aed..37ce20f 100644 --- a/ucaptcha/anticaptcha.py +++ b/ucaptcha/anticaptcha.py @@ -5,6 +5,7 @@ from ucaptcha import logger from ucaptcha.exceptions import KeyDoesNotExistError +from ucaptcha.exceptions import TimeoutError from ucaptcha.exceptions import UCaptchaError from ucaptcha.exceptions import WrongUserKeyError from ucaptcha.exceptions import ZeroBalanceError @@ -63,7 +64,7 @@ def solve_anticaptcha( raise UCaptchaError("Task ID not found in data.") task_id = data["taskId"] - while True: + for _ in range(10): data = {"clientKey": api_key, "taskId": task_id} request_url = f"https://api.anti-captcha.com/getTaskResult" res = httpx.post(request_url, json=data, timeout=300) @@ -81,3 +82,4 @@ def solve_anticaptcha( return data["solution"].get("gRecaptchaResponse") time.sleep(10) continue + raise TimeoutError diff --git a/ucaptcha/capmonster.py b/ucaptcha/capmonster.py index d3d909c..963c053 100644 --- a/ucaptcha/capmonster.py +++ b/ucaptcha/capmonster.py @@ -5,6 +5,7 @@ from ucaptcha import logger from ucaptcha.exceptions import KeyDoesNotExistError +from ucaptcha.exceptions import TimeoutError from ucaptcha.exceptions import UCaptchaError from ucaptcha.exceptions import WrongUserKeyError from ucaptcha.exceptions import ZeroBalanceError @@ -64,7 +65,7 @@ def solve_capmonster( raise UCaptchaError("Task ID not found.") task_id = data["taskId"] - while True: + for _ in range(10): data = {"clientKey": api_key, "taskId": task_id} request_url = f"https://api.capmonster.cloud/getTaskResult" res = httpx.post(request_url, json=data, timeout=300) @@ -82,3 +83,4 @@ def solve_capmonster( return data["solution"].get("gRecaptchaResponse") time.sleep(10) continue + raise TimeoutError diff --git a/ucaptcha/exceptions.py b/ucaptcha/exceptions.py index d2c6d4a..4eeea84 100644 --- a/ucaptcha/exceptions.py +++ b/ucaptcha/exceptions.py @@ -16,3 +16,7 @@ class KeyDoesNotExistError(UCaptchaError): class ProxyFormatError(UCaptchaError): pass + + +class TimeoutError(UCaptchaError): + pass diff --git a/ucaptcha/nocaptchaai.py b/ucaptcha/nocaptchaai.py index 42df27a..667e50c 100644 --- a/ucaptcha/nocaptchaai.py +++ b/ucaptcha/nocaptchaai.py @@ -5,6 +5,7 @@ from ucaptcha import logger from ucaptcha.exceptions import KeyDoesNotExistError +from ucaptcha.exceptions import TimeoutError from ucaptcha.exceptions import UCaptchaError from ucaptcha.exceptions import ZeroBalanceError from ucaptcha.proxies import get_proxy_parts @@ -64,7 +65,7 @@ def solve_nocaptchaai( task_url = data["url"] time.sleep(7) - while True: + for _ in range(10): res = httpx.get(task_url, headers=headers, timeout=300) res.raise_for_status() data = res.json() @@ -79,3 +80,4 @@ def solve_nocaptchaai( logger.info("Captcha ready.") return data["token"] time.sleep(10) + raise TimeoutError diff --git a/ucaptcha/nopecha.py b/ucaptcha/nopecha.py index f467c9c..c0acbe2 100644 --- a/ucaptcha/nopecha.py +++ b/ucaptcha/nopecha.py @@ -4,6 +4,7 @@ import httpx from ucaptcha import logger +from ucaptcha.exceptions import TimeoutError from ucaptcha.exceptions import UCaptchaError from ucaptcha.proxies import get_proxy_parts @@ -53,7 +54,7 @@ def solve_nopecha( task_url = f"https://api.nopecha.com/token/?key={api_key}&id={task_id}" - while True: + for _ in range(10): res = httpx.get(task_url, timeout=300) if res.status_code == 409: if "Incomplete job" in res.text: @@ -68,3 +69,4 @@ def solve_nopecha( return data["data"] raise UCaptchaError(f"{res.status_code}, {res.text}") + raise TimeoutError diff --git a/ucaptcha/solver.py b/ucaptcha/solver.py index 882a8a5..85eb0be 100644 --- a/ucaptcha/solver.py +++ b/ucaptcha/solver.py @@ -1,12 +1,11 @@ from typing import Literal +from ucaptcha.anticaptcha import solve_anticaptcha +from ucaptcha.capmonster import solve_capmonster +from ucaptcha.nocaptchaai import solve_nocaptchaai from ucaptcha.nopecha import solve_nopecha from ucaptcha.twocaptcha import solve_twocaptcha -from .anticaptcha import solve_anticaptcha -from .capmonster import solve_capmonster -from .nocaptchaai import solve_nocaptchaai - CaptchaService = Literal[ "anti-captcha", "capmonster", "nocaptchaai", "nopecha", "twocaptcha" ] diff --git a/ucaptcha/twocaptcha.py b/ucaptcha/twocaptcha.py index bb05155..a63aa54 100644 --- a/ucaptcha/twocaptcha.py +++ b/ucaptcha/twocaptcha.py @@ -4,6 +4,7 @@ import httpx from ucaptcha import logger +from ucaptcha.exceptions import TimeoutError from ucaptcha.exceptions import UCaptchaError from ucaptcha.proxies import get_proxy_parts @@ -52,7 +53,7 @@ def solve_twocaptcha( time.sleep(7) - while True: + for _ in range(10): data = {"clientKey": api_key, "taskId": task_id} res = httpx.post( "https://api.2captcha.com/getTaskResult", json=data, timeout=30 @@ -67,3 +68,4 @@ def solve_twocaptcha( logger.info("Captcha ready.") return data["solution"]["token"] time.sleep(10) + raise TimeoutError