Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tls_client support #15

Open
FuckingToasters opened this issue Sep 12, 2024 · 1 comment
Open

Add tls_client support #15

FuckingToasters opened this issue Sep 12, 2024 · 1 comment

Comments

@FuckingToasters
Copy link

Hello, so i already have an tls client session oben, i would like to use this session in my requests but if that wo't be added for some reason, then just add a slver function with tls_client libary. thanks in advance

@FuckingToasters
Copy link
Author

FuckingToasters commented Sep 12, 2024

I forked the repo and tried to add it myself. after it prints out solving... nothing is happening, it just is stuck indefenently and i don't know how to solve that.

import typing
from logging import getLogger

try:
    import tls_client
except ImportError:
    raise ImportError("You must install 'tls-client' to use `nopecha.api.tlsclient`")

from ._base import APIClient, UniformResponse

logger = getLogger(__name__)
__all__ = ["TlsAPIClient"]

class TlsAPIClient(APIClient):
    def __init__(self, *args, **kwargs):
        session = kwargs.pop("session", None)
        #super().__init__(*args, **kwargs)
        self.session = session or tls_client.Session(
            client_identifier="chrome119", random_tls_extension_order=True
        )

    def _request_raw(
        self, method: str, url: str, body: typing.Optional[dict] = None
    ) -> UniformResponse:
        status = 999
        try:
            # Choose the correct method from tls_client (e.g., get, post, etc.)
            if method.lower() == "post":
                response = self.session.post(
                    url, json=body, headers=self._get_headers()
                )
            elif method.lower() == "get":
                response = self.session.get(
                    url, headers=self._get_headers()
                )
            elif method.lower() == "put":
                response = self.session.get(
                    url, headers=self._get_headers()
                )
            elif method.lower() == "delete":
                response = self.session.get(
                    url, headers=self._get_headers()
            	)

            else:
                raise ValueError(f"Unsupported HTTP method: {method}")

            status = response.status_code
            return UniformResponse(status, response.json())
        except Exception as e:
            logger.warning("Request failed: %s", e)
            return UniformResponse(status, None)
# Install the client using the following command:
# pip install --upgrade nopecha

import tls_client
from nopecha.api.tls_client import TlsAPIClient
solver = TlsAPIClient("7ils8cwb7hb3md2j", tls_client.Session(client_identifier="chrome119", random_tls_extension_order=True))
print("solving...")
solution = solver.solve_hcaptcha("b4c45857-0e23-48e6-9017-e28fff99ffb2", "https://nopecha.com/demo/hcaptcha#easy")
print("token is", solution["data"])

# Print the token
print(token)

After arund a minute of it being stuck there, it printed out following error log:

Traceback (most recent call last):
  File "/root/python-tools/testing/captcha.py", line 8, in <module>
    solution = solver.solve_hcaptcha("b4c45857-0e23-48e6-9017-e28fff99ffb2", "                                     https://nopecha.com/demo/hcaptcha#easy")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/nopecha/api/_base.py", line 301                                     , in solve_hcaptcha
    return typing.cast(TokenResponse, self.solve_raw(body))
                                      ^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/nopecha/api/_base.py", line 161                                     , in solve_raw
    return self._request(f"{self.host}/token/", body)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/nopecha/api/_base.py", line 120                                     , in _request
    job_id = self._request_post(endpoint, body)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/nopecha/api/_base.py", line 136                                     , in _request_post
    raise RuntimeError(
RuntimeError: Server did not accept job after 10 attempts. Server may be overl                                     oaded (https://nopecha.com/discord). Alternatively try increasing the post_max                                     _attempts parameter (or set to 0 for unlimited retries).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant