Skip to content

Commit 3add3f6

Browse files
authored
Merge pull request #199 from AndreiDrang/main
Added callbackUrl param
2 parents 2da0ead + 732fca4 commit 3add3f6

15 files changed

Lines changed: 106 additions & 70 deletions

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ Check our other projects here - `RedPandaDev group <https://red-panda-dev.xyz/bl
3838
:maxdepth: 2
3939
:caption: Additional modules
4040

41+
modules/core/info.rst
4142
modules/enum/info.rst
4243
modules/serializer/info.rst

docs/modules/core/info.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Core
2+
====
3+
4+
To import this module:
5+
6+
.. code-block:: python
7+
8+
from python3_anticaptcha.core import base
9+
10+
11+
.. autoclass:: core.base.CaptchaParams
12+
:members:

src/python3_anticaptcha/config.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44

55
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
66

7-
# Адрес для создания задачи
8-
create_task_url = "https://api.anti-captcha.com/createTask"
9-
# Адрес для получения ответа
10-
get_result_url = "https://api.anti-captcha.com/getTaskResult"
11-
# ключ приложения
12-
app_key = "867"
13-
147

158
# Connection retry generator
169
def attempts_generator(amount: int = 5) -> Generator:

src/python3_anticaptcha/control.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ async def aio_get_balance(self) -> dict:
158158
Notes:
159159
https://anti-captcha.com/apidoc/methods/getBalance
160160
"""
161-
self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self)
162-
return await self._captcha_handling_instrument.send_post_request(
161+
return await AIOCaptchaInstrument.send_post_request(
163162
url_postfix=ControlPostfixEnm.GET_BALANCE, payload={"clientKey": self.create_task_payload.clientKey}
164163
)
165164

@@ -346,8 +345,7 @@ async def aio_get_spending_stats(self, **kwargs) -> dict:
346345
Notes:
347346
https://anti-captcha.com/apidoc/methods/getSpendingStats
348347
"""
349-
self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self)
350-
return await self._captcha_handling_instrument.send_post_request(
348+
return await AIOCaptchaInstrument.send_post_request(
351349
url_postfix=ControlPostfixEnm.GET_SPENDING_STATS,
352350
payload={"clientKey": self.create_task_payload.clientKey, **kwargs},
353351
)
@@ -431,8 +429,7 @@ async def aio_get_app_stats(self, softId: int, mode: Optional[str] = None) -> di
431429
Notes:
432430
https://anti-captcha.com/apidoc/methods/getAppStats
433431
"""
434-
self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self)
435-
return await self._captcha_handling_instrument.send_post_request(
432+
return await AIOCaptchaInstrument.send_post_request(
436433
url_postfix=ControlPostfixEnm.GET_APP_STATS,
437434
payload={"clientKey": self.create_task_payload.clientKey, "softId": softId, "mode": mode},
438435
)
@@ -480,8 +477,7 @@ async def aio_report_incorrect_image(self, taskId: int) -> dict:
480477
Notes:
481478
https://anti-captcha.com/apidoc/methods/reportIncorrectImageCaptcha
482479
"""
483-
self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self)
484-
return await self._captcha_handling_instrument.send_post_request(
480+
return await AIOCaptchaInstrument.send_post_request(
485481
url_postfix=ControlPostfixEnm.REPORT_INCORRECT_IMAGE_CAPTCHA,
486482
payload={"clientKey": self.create_task_payload.clientKey, "taskId": taskId},
487483
)
@@ -529,8 +525,7 @@ async def aio_report_incorrect_recaptcha(self, taskId: int) -> dict:
529525
Notes:
530526
https://anti-captcha.com/apidoc/methods/reportIncorrectRecaptcha
531527
"""
532-
self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self)
533-
return await self._captcha_handling_instrument.send_post_request(
528+
return await AIOCaptchaInstrument.send_post_request(
534529
url_postfix=ControlPostfixEnm.REPORT_INCORRECT_RECAPTCHA,
535530
payload={"clientKey": self.create_task_payload.clientKey, "taskId": taskId},
536531
)
@@ -578,8 +573,7 @@ async def aio_report_correct_recaptcha(self, taskId: int) -> dict:
578573
Notes:
579574
https://anti-captcha.com/apidoc/methods/reportCorrectRecaptcha
580575
"""
581-
self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self)
582-
return await self._captcha_handling_instrument.send_post_request(
576+
return await AIOCaptchaInstrument.send_post_request(
583577
url_postfix=ControlPostfixEnm.REPORT_CORRECT_RECAPTCHA,
584578
payload={"clientKey": self.create_task_payload.clientKey, "taskId": taskId},
585579
)
@@ -627,8 +621,7 @@ async def aio_report_incorrect_hcaptcha(self, taskId: int) -> dict:
627621
Notes:
628622
https://anti-captcha.com/apidoc/methods/reportIncorrectHcaptcha
629623
"""
630-
self._captcha_handling_instrument = AIOCaptchaInstrument(captcha_params=self)
631-
return await self._captcha_handling_instrument.send_post_request(
624+
return await AIOCaptchaInstrument.send_post_request(
632625
url_postfix=ControlPostfixEnm.REPORT_INCORRECT_HCAPTCHA,
633626
payload={"clientKey": self.create_task_payload.clientKey, "taskId": taskId},
634627
)

src/python3_anticaptcha/core/aio_captcha_instrument.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,6 @@ async def __body_file_processing(
101101
self.result.errorId = 12
102102
self.result.errorCode = self.NO_CAPTCHA_ERR
103103

104-
async def _url_read(self, url: str, **kwargs) -> bytes:
105-
"""
106-
Async method read bytes from link
107-
"""
108-
async with aiohttp.ClientSession() as session:
109-
async for attempt in ASYNC_RETRIES:
110-
with attempt:
111-
async with session.get(url=url, **kwargs) as resp:
112-
return await resp.content.read()
113-
114104
async def _create_task(self, url_postfix: str = CREATE_TASK_POSTFIX) -> CreateTaskResponseSer:
115105
"""
116106
Function send SYNC request to service and wait for result
@@ -128,23 +118,6 @@ async def _create_task(self, url_postfix: str = CREATE_TASK_POSTFIX) -> CreateTa
128118
logging.exception(error)
129119
raise
130120

131-
@staticmethod
132-
async def send_post_request(payload: Optional[dict] = None, url_postfix: str = CREATE_TASK_POSTFIX) -> dict:
133-
"""
134-
Function send ASYNC request to service and wait for result
135-
"""
136-
137-
async with aiohttp.ClientSession() as session:
138-
try:
139-
async with session.post(parse.urljoin(BASE_REQUEST_URL, url_postfix), json=payload) as resp:
140-
if resp.status == 200:
141-
return await resp.json()
142-
else:
143-
raise ValueError(resp.reason)
144-
except Exception as error:
145-
logging.exception(error)
146-
raise
147-
148121
async def _get_result(self, url_response: str = GET_RESULT_POSTFIX) -> dict:
149122
attempts = attempts_generator()
150123
# Send request for status of captcha solution.
@@ -166,3 +139,31 @@ async def _get_result(self, url_response: str = GET_RESULT_POSTFIX) -> dict:
166139
else:
167140
json_result.update({"taskId": self.captcha_params.get_result_params.taskId})
168141
return json_result
142+
143+
@staticmethod
144+
async def _url_read(url: str, **kwargs) -> bytes:
145+
"""
146+
Async method read bytes from link
147+
"""
148+
async with aiohttp.ClientSession() as session:
149+
async for attempt in ASYNC_RETRIES:
150+
with attempt:
151+
async with session.get(url=url, **kwargs) as resp:
152+
return await resp.content.read()
153+
154+
@staticmethod
155+
async def send_post_request(payload: Optional[dict] = None, url_postfix: str = CREATE_TASK_POSTFIX) -> dict:
156+
"""
157+
Function send ASYNC request to service and wait for result
158+
"""
159+
160+
async with aiohttp.ClientSession() as session:
161+
try:
162+
async with session.post(parse.urljoin(BASE_REQUEST_URL, url_postfix), json=payload) as resp:
163+
if resp.status == 200:
164+
return await resp.json()
165+
else:
166+
raise ValueError(resp.reason)
167+
except Exception as error:
168+
logging.exception(error)
169+
raise

src/python3_anticaptcha/core/base.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
from .context_instr import AIOContextManager, SIOContextManager
33
from .captcha_instrument import CaptchaInstrument
44
from .aio_captcha_instrument import AIOCaptchaInstrument
5+
from .sio_captcha_instrument import SIOCaptchaInstrument
56

67
__all__ = ("CaptchaParams",)
78

8-
from .sio_captcha_instrument import SIOCaptchaInstrument
9-
109

1110
class CaptchaParams(SIOContextManager, AIOContextManager):
1211
"""
13-
Basic Captcha solving class
12+
Basic Captcha params class
1413
1514
Args:
1615
api_key: Capsolver API key
@@ -30,6 +29,20 @@ def __init__(self, api_key: str, sleep_time: int = 15, *args, **kwargs):
3029

3130
self._captcha_handling_instrument = CaptchaInstrument()
3231

32+
def set_callback_url(self, callbackUrl: str) -> None:
33+
"""
34+
Method for `callbackUrl` param set.
35+
36+
Args:
37+
callbackUrl: Optional web address where we can send the results of captcha task processing.
38+
Contents are sent by AJAX POST request and are identical
39+
to the contents of getTaskResult method.
40+
41+
Notes:
42+
https://anti-captcha.com/apidoc/methods/createTask
43+
"""
44+
self.create_task_payload.callbackUrl = callbackUrl
45+
3346
def captcha_handler(self, **additional_params) -> dict:
3447
"""
3548
Synchronous method for captcha solving

src/python3_anticaptcha/core/serializer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BaseAPIRequestSer(MyBaseModel):
2323
class CreateTaskBaseSer(BaseAPIRequestSer):
2424
task: Dict = {}
2525
softId: Literal[APP_KEY] = APP_KEY
26+
callbackUrl: str = ""
2627

2728

2829
class BaseAPIResponseSer(MyBaseModel):

src/python3_anticaptcha/core/sio_captcha_instrument.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,6 @@ def _create_task(self, url_postfix: str = CREATE_TASK_POSTFIX) -> CreateTaskResp
123123
logging.exception(error)
124124
raise
125125

126-
@staticmethod
127-
def send_post_request(
128-
payload: Optional[dict] = None,
129-
session: requests.Session = requests.Session(),
130-
url_postfix: str = CREATE_TASK_POSTFIX,
131-
) -> dict:
132-
"""
133-
Function send SYNC request to service and wait for result
134-
"""
135-
try:
136-
resp = session.post(parse.urljoin(BASE_REQUEST_URL, url_postfix), json=payload)
137-
if resp.status_code == 200:
138-
return resp.json()
139-
else:
140-
raise ValueError(resp.raise_for_status())
141-
except Exception as error:
142-
logging.exception(error)
143-
raise
144-
145126
def _url_read(self, url: str, **kwargs):
146127
"""
147128
Method open links
@@ -166,3 +147,22 @@ def _get_result(self, url_response: str = GET_RESULT_POSTFIX) -> dict:
166147
else:
167148
self.session.close()
168149
return captcha_response.to_dict()
150+
151+
@staticmethod
152+
def send_post_request(
153+
payload: Optional[dict] = None,
154+
session: requests.Session = requests.Session(),
155+
url_postfix: str = CREATE_TASK_POSTFIX,
156+
) -> dict:
157+
"""
158+
Function send SYNC request to service and wait for result
159+
"""
160+
try:
161+
resp = session.post(parse.urljoin(BASE_REQUEST_URL, url_postfix), json=payload)
162+
if resp.status_code == 200:
163+
return resp.json()
164+
else:
165+
raise ValueError(resp.raise_for_status())
166+
except Exception as error:
167+
logging.exception(error)
168+
raise

src/python3_anticaptcha/custom_task.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from .core.base import CaptchaParams
44
from .core.enum import ProxyTypeEnm, CaptchaTypeEnm
55

6+
__all__ = ("CustomTask",)
7+
68

79
class CustomTask(CaptchaParams):
810
def __init__(

src/python3_anticaptcha/fun_captcha.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from .core.base import CaptchaParams
44
from .core.enum import ProxyTypeEnm, CaptchaTypeEnm
55

6+
__all__ = ("FunCaptcha",)
7+
68

79
class FunCaptcha(CaptchaParams):
810
def __init__(

0 commit comments

Comments
 (0)