|
| 1 | +from typing import Union, Optional |
| 2 | + |
| 3 | +from .core.base import CaptchaParams |
| 4 | +from .core.enum import ProxyTypeEnm, CaptchaTypeEnm |
| 5 | + |
| 6 | +__all__ = ("FriendlyCaptcha",) |
| 7 | + |
| 8 | + |
| 9 | +class FriendlyCaptcha(CaptchaParams): |
| 10 | + def __init__( |
| 11 | + self, |
| 12 | + api_key: str, |
| 13 | + websiteURL: str, |
| 14 | + websiteKey: str, |
| 15 | + captcha_type: Union[CaptchaTypeEnm, str] = CaptchaTypeEnm.FriendlyCaptchaTaskProxyless, |
| 16 | + funcaptchaApiJSSubdomain: Optional[str] = None, |
| 17 | + data: Optional[str] = None, |
| 18 | + proxyType: Optional[Union[ProxyTypeEnm, str]] = None, |
| 19 | + proxyAddress: Optional[str] = None, |
| 20 | + proxyPort: Optional[int] = None, |
| 21 | + proxyLogin: Optional[str] = None, |
| 22 | + proxyPassword: Optional[str] = None, |
| 23 | + userAgent: Optional[str] = None, |
| 24 | + sleep_time: Optional[int] = 10, |
| 25 | + ): |
| 26 | + """ |
| 27 | + The class is used to work with FriendlyCaptcha. |
| 28 | +
|
| 29 | + Args: |
| 30 | + api_key: Capsolver API key |
| 31 | + captcha_type: Captcha type |
| 32 | + websiteURL: Address of the webpage |
| 33 | + websiteKey: Friendly Captcha sitekey |
| 34 | + proxyType: Type of the proxy |
| 35 | + proxyAddress: Proxy IP address IPv4/IPv6. Not allowed to use: |
| 36 | + host names instead of IPs, |
| 37 | + transparent proxies (where client IP is visible), |
| 38 | + proxies from local networks (192.., 10.., 127...) |
| 39 | + proxyPort: Proxy port. |
| 40 | + proxyLogin: Proxy login. |
| 41 | + proxyPassword: Proxy password. |
| 42 | + userAgent: Browser UserAgent. |
| 43 | + sleep_time: The waiting time between requests to get the result of the Captcha |
| 44 | +
|
| 45 | + Examples: |
| 46 | + >>> FriendlyCaptcha(api_key="99d7d111a0111dc11184111c8bb111da", |
| 47 | + ... captcha_type="FunCaptchaTaskProxyless", |
| 48 | + ... websiteURL="https://demo.arkoselabs.com", |
| 49 | + ... websiteKey="FCMDESUD3M34857N" |
| 50 | + ... ).captcha_handler() |
| 51 | + { |
| 52 | + "errorId": 0, |
| 53 | + "errorCode": None, |
| 54 | + "errorDescription": None, |
| 55 | + "status":"ready", |
| 56 | + "solution":{ |
| 57 | + "token":"0.Qz0.....f1", |
| 58 | + "userAgent":"Mozilla/5.0 (Wind.....", |
| 59 | + }, |
| 60 | + "cost": 0.002, |
| 61 | + "ip": "46.53.249.230", |
| 62 | + "createTime": 1679004358, |
| 63 | + "endTime": 1679004368, |
| 64 | + "solveCount": 0, |
| 65 | + "taskId": 396687629 |
| 66 | + } |
| 67 | +
|
| 68 | + >>> await FriendlyCaptcha(api_key="99d7d111a0111dc11184111c8bb111da", |
| 69 | + ... captcha_type="FunCaptchaTaskProxyless", |
| 70 | + ... websiteURL="https://demo.arkoselabs.com", |
| 71 | + ... websitePublicKey="DF9C4D87-CB7B-4062-9FEB-BADB6ADA61E6" |
| 72 | + ... ).aio_captcha_handler() |
| 73 | + { |
| 74 | + "errorId": 0, |
| 75 | + "errorCode": None, |
| 76 | + "errorDescription": None, |
| 77 | + "status":"ready", |
| 78 | + "solution":{ |
| 79 | + "token":"0.Qz0.....f1", |
| 80 | + "userAgent":"Mozilla/5.0 (Wind.....", |
| 81 | + }, |
| 82 | + "cost": 0.002, |
| 83 | + "ip": "46.53.249.230", |
| 84 | + "createTime": 1679004358, |
| 85 | + "endTime": 1679004368, |
| 86 | + "solveCount": 0, |
| 87 | + "taskId": 396687629 |
| 88 | + } |
| 89 | +
|
| 90 | + >>> FriendlyCaptcha(api_key="99d7d111a0111dc11184111c8bb111da", |
| 91 | + ... captcha_type="FunCaptchaTask", |
| 92 | + ... websiteURL="https://demo.arkoselabs.com", |
| 93 | + ... websitePublicKey="DF9C4D87-CB7B-4062-9FEB-BADB6ADA61E6", |
| 94 | + ... proxyType="http", |
| 95 | + ... proxyAddress="0.0.0.0", |
| 96 | + ... proxyPort=9988, |
| 97 | + ... proxyLogin="proxy_login", |
| 98 | + ... proxyPassword="proxy_password", |
| 99 | + ... userAgent="some_real_user_agent", |
| 100 | + ... ).captcha_handler() |
| 101 | + { |
| 102 | + "errorId": 0, |
| 103 | + "errorCode": None, |
| 104 | + "errorDescription": None, |
| 105 | + "status":"ready", |
| 106 | + "solution":{ |
| 107 | + "token":"0.Qz0.....f1", |
| 108 | + "userAgent":"Mozilla/5.0 (Wind.....", |
| 109 | + }, |
| 110 | + "cost": 0.002, |
| 111 | + "ip": "46.53.249.230", |
| 112 | + "createTime": 1679004358, |
| 113 | + "endTime": 1679004368, |
| 114 | + "solveCount": 0, |
| 115 | + "taskId": 396687629 |
| 116 | + } |
| 117 | +
|
| 118 | + Notes |
| 119 | + https://anti-captcha.com/apidoc/task-types/FriendlyCaptchaTask |
| 120 | +
|
| 121 | + https://anti-captcha.com/apidoc/task-types/FriendlyCaptchaTaskProxyless |
| 122 | + """ |
| 123 | + super().__init__(api_key=api_key, sleep_time=sleep_time) |
| 124 | + |
| 125 | + # validation of the received parameters |
| 126 | + if captcha_type == CaptchaTypeEnm.FriendlyCaptchaTask: |
| 127 | + self.task_params = dict( |
| 128 | + type=captcha_type, |
| 129 | + websiteURL=websiteURL, |
| 130 | + websiteKey=websiteKey, |
| 131 | + funcaptchaApiJSSubdomain=funcaptchaApiJSSubdomain, |
| 132 | + data=data, |
| 133 | + proxyType=proxyType, |
| 134 | + proxyAddress=proxyAddress, |
| 135 | + proxyPort=proxyPort, |
| 136 | + proxyLogin=proxyLogin, |
| 137 | + proxyPassword=proxyPassword, |
| 138 | + userAgent=userAgent, |
| 139 | + ) |
| 140 | + elif captcha_type == CaptchaTypeEnm.FriendlyCaptchaTaskProxyless: |
| 141 | + self.task_params = dict( |
| 142 | + type=captcha_type, |
| 143 | + websiteURL=websiteURL, |
| 144 | + websiteKey=websiteKey, |
| 145 | + funcaptchaApiJSSubdomain=funcaptchaApiJSSubdomain, |
| 146 | + data=data, |
| 147 | + ) |
| 148 | + else: |
| 149 | + raise ValueError( |
| 150 | + f"Invalid `captcha_type` parameter set for `{self.__class__.__name__}`, \ |
| 151 | + available - {CaptchaTypeEnm.FriendlyCaptchaTaskProxyless.value,CaptchaTypeEnm.FriendlyCaptchaTask.value}" |
| 152 | + ) |
0 commit comments