Skip to content

Commit

Permalink
feat: add TimeoutError
Browse files Browse the repository at this point in the history
  • Loading branch information
pradishb committed Sep 16, 2024
1 parent a81f959 commit 69351e4
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]" }]
Expand Down
4 changes: 3 additions & 1 deletion ucaptcha/anticaptcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -81,3 +82,4 @@ def solve_anticaptcha(
return data["solution"].get("gRecaptchaResponse")
time.sleep(10)
continue
raise TimeoutError
4 changes: 3 additions & 1 deletion ucaptcha/capmonster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -82,3 +83,4 @@ def solve_capmonster(
return data["solution"].get("gRecaptchaResponse")
time.sleep(10)
continue
raise TimeoutError
4 changes: 4 additions & 0 deletions ucaptcha/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ class KeyDoesNotExistError(UCaptchaError):

class ProxyFormatError(UCaptchaError):
pass


class TimeoutError(UCaptchaError):
pass
4 changes: 3 additions & 1 deletion ucaptcha/nocaptchaai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -79,3 +80,4 @@ def solve_nocaptchaai(
logger.info("Captcha ready.")
return data["token"]
time.sleep(10)
raise TimeoutError
4 changes: 3 additions & 1 deletion ucaptcha/nopecha.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -68,3 +69,4 @@ def solve_nopecha(
return data["data"]

raise UCaptchaError(f"{res.status_code}, {res.text}")
raise TimeoutError
7 changes: 3 additions & 4 deletions ucaptcha/solver.py
Original file line number Diff line number Diff line change
@@ -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"
]
Expand Down
4 changes: 3 additions & 1 deletion ucaptcha/twocaptcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -67,3 +68,4 @@ def solve_twocaptcha(
logger.info("Captcha ready.")
return data["solution"]["token"]
time.sleep(10)
raise TimeoutError

0 comments on commit 69351e4

Please sign in to comment.