Skip to content

Commit

Permalink
Added Imperva
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDrang committed Sep 12, 2023
1 parent fcabd72 commit fbd84d7
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/python3_capsolver/core/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class EndpointPostfixEnm(str, MyEnum):
GET_BALANCE = "getBalance"
CREATE_TASK = "createTask"
GET_TASK_RESULT = "getTaskResult"
AKAMAI_BMP_INVOKE = "/akamaibmp/invoke"
AKAMAI_WEB_INVOKE = "/akamaiweb/invoke"
AKAMAI_BMP_INVOKE = "akamaibmp/invoke"
AKAMAI_WEB_INVOKE = "akamaiweb/invoke"


class ImageToTextTaskTypeEnm(str, MyEnum):
Expand Down Expand Up @@ -117,6 +117,10 @@ class AntiAkamaiTaskEnm(str, MyEnum):
AntiAkamaiWebTask = "AntiAkamaiWebTask"


class AntiImpervaTaskEnm(str, MyEnum):
AntiImpervaTask = "AntiImpervaTask"


class ResponseStatusEnm(str, MyEnum):
"""
Enum store results `status` field variants
Expand Down
13 changes: 11 additions & 2 deletions src/python3_capsolver/core/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,20 @@ class CyberSiAraSer(WebsiteDataOptionsSer):
UserAgent: str = Field(..., description="Browser userAgent, you need submit your userAgent")


class AntiAkamaiBMPTaskSer(BaseModel):
class AntiAkamaiBMPTaskSer(TaskSer):
packageName: str = Field("de.zalando.iphone", description="Package name of AkamaiBMP mobile APP")
version: str = Field("3.2.6", description="AKAMAI BMP Version number")
country: str = Field("US", description="AKAMAI BMP country")


class AntiAkamaiWebTaskSer(BaseModel):
class AntiAkamaiWebTaskSer(TaskSer):
url: str = Field(..., description="Browser url address")


class AntiImpervaTaskSer(TaskSer):
websiteUrl: str = Field(..., description="The website url")
userAgent: str = Field(..., description="Browser userAgent")
utmvc: bool = Field(
True, description="If cookie contains `incap_see_xxx`, `nlbi_xxx`, `visid_inap_xxx`, mean is true"
)
reese84: bool = Field(True, description="if cookie conains `reese84`, set it true")
120 changes: 120 additions & 0 deletions src/python3_capsolver/imperva.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from typing import Union

from python3_capsolver.core.base import BaseCaptcha
from python3_capsolver.core.enum import AntiImpervaTaskEnm
from python3_capsolver.core.serializer import AntiImpervaTaskSer, CaptchaResponseSer


class Imperva(BaseCaptcha):
"""
The class is used to work with Capsolver Imperva method.
Args:
api_key: Capsolver API key
captcha_type: Captcha type name, like ``AntiImpervaTask`` and etc.
websiteUrl: The website url
userAgent: Browser userAgent
Examples:
>>> Imperva(api_key="CAI-BA9XXXXXXXXXXXXX2702E010",
... captcha_type="AntiImpervaTask",
... websiteUrl="https://www.milanuncios.com/",
... userAgent="Mozilla/5.0 (Windows ....",
... proxy="socks5:98.181.137.83:4145",
... utmvc=True,
... reese84=True,
... reeseScriptUrl="https://www.milanuncios.com/librarym.js",
... ).captcha_handler()
CaptchaResponseSer(errorId=0,
errorCode=None,
errorDescription=None,
taskId='73bdcd28-6c77-4414-8....',
status=<ResponseStatusEnm.Ready: 'ready'>,
solution={'token': '90F9EAF...'}
)
>>> Imperva(api_key="CAI-BA9XXXXXXXXXXXXX2702E010",
... captcha_type=AntiImpervaTaskEnm.AntiImpervaTask,
... websiteUrl="https://www.milanuncios.com/",
... userAgent="Mozilla/5.0 (Windows ....",
... proxy="socks5:98.181.137.83:4145",
... utmvc=True,
... reese84=True,
... reeseScriptUrl="https://www.milanuncios.com/librarym.js",
... ).captcha_handler()
CaptchaResponseSer(errorId=0,
errorCode=None,
errorDescription=None,
taskId='73bdcd28-6c77-4414-8....',
status=<ResponseStatusEnm.Ready: 'ready'>,
solution={'token': '90F9EAF...'}
)
>>> await Imperva(api_key="CAI-BA9650D2B9C2786B21120D512702E010",
... captcha_type=AntiImpervaTaskEnm.AntiImpervaTask,
... websiteUrl="https://www.milanuncios.com/",
... userAgent="Mozilla/5.0 (Windows ....",
... proxy="socks5:98.181.137.83:4145",
... utmvc=True,
... reese84=True,
... reeseScriptUrl="https://www.milanuncios.com/librarym.js",
... ).aio_captcha_handler()
CaptchaResponseSer(errorId=0,
errorCode=None,
errorDescription=None,
taskId='73bdcd28-6c77-4414-8....',
status=<ResponseStatusEnm.Ready: 'ready'>,
solution={'token': '90F9EAF...'}
)
Returns:
CaptchaResponseSer model with full server response
Notes:
https://docs.capsolver.com/guide/antibots/imperva.html
"""

def __init__(
self,
captcha_type: Union[AntiImpervaTaskEnm, str],
websiteUrl: str,
userAgent: str,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)

if captcha_type == AntiImpervaTaskEnm.AntiImpervaTask:
self.task_params = AntiImpervaTaskSer(**locals()).dict()
else:
raise ValueError(
f"""Invalid `captcha_type` parameter set for `{self.__class__.__name__}`,
available - {AntiImpervaTaskEnm.list_values()}"""
)

for key in kwargs:
self.task_params.update({key: kwargs[key]})

def captcha_handler(self) -> CaptchaResponseSer:
"""
Sync solving method
Returns:
CaptchaResponseSer model with full service response
Notes:
Check class docstring for more info
"""
return self._processing_captcha(create_params=self.task_params)

async def aio_captcha_handler(self) -> CaptchaResponseSer:
"""
Async method for captcha solving
Returns:
CaptchaResponseSer model with full service response
Notes:
Check class docstring for more info
"""
return await self._aio_processing_captcha(create_params=self.task_params)
2 changes: 2 additions & 0 deletions src/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def run(self):
cloudflare
amazon
amazon_waf
akamai
imperva
""",
python_requires=REQUIRES_PYTHON,
zip_safe=False,
Expand Down
46 changes: 46 additions & 0 deletions tests/test_imperva.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest

from tests.conftest import BaseTest
from python3_capsolver.imperva import Imperva
from python3_capsolver.core.enum import AntiImpervaTaskEnm


class TestImpervaBase(BaseTest):
def test_captcha_handler_exist(self):
assert "captcha_handler" in Imperva.__dict__.keys()

def test_aio_captcha_handler_exist(self):
assert "aio_captcha_handler" in Imperva.__dict__.keys()

def test_wrong_captcha_type(self):
with pytest.raises(ValueError):
Imperva(
api_key=self.get_random_string(36),
captcha_type="test",
websiteUrl=self.get_random_string(36),
userAgent=self.get_random_string(36),
)

def test_no_captcha_type(self):
with pytest.raises(TypeError):
Imperva(
api_key=self.get_random_string(36),
websiteUrl=self.get_random_string(36),
userAgent=self.get_random_string(36),
)

def test_no_websiteUrl(self):
with pytest.raises(TypeError):
Imperva(
api_key=self.get_random_string(36),
captcha_type=AntiImpervaTaskEnm.AntiImpervaTask,
userAgent=self.get_random_string(36),
)

def test_no_userAgent(self):
with pytest.raises(TypeError):
Imperva(
api_key=self.get_random_string(36),
captcha_type=AntiImpervaTaskEnm.AntiImpervaTask,
websiteUrl=self.get_random_string(36),
)

0 comments on commit fbd84d7

Please sign in to comment.