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 service selecting for captcha solving and optimize Client proxy #168

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

PR0F1L3R1
Copy link

@PR0F1L3R1 PR0F1L3R1 commented Aug 2, 2024

Added new parameter of the Capsolver class to select service that will solve captcha

code for select service:

Capsolver(api_key="", service=Service.Capsolver)

Old code still works fine:

Capsolver(api_key="")

Summary by Sourcery

Add support for selecting different captcha solving services in the Capsolver class by introducing a new 'service' parameter and an enumeration class 'Service' to define available services.

New Features:

  • Introduce a new parameter 'service' in the Capsolver class to select the captcha solving service.

Enhancements:

  • Add an enumeration class 'Service' to define available captcha solving services.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced support for multiple captcha-solving services, allowing users to choose between Capsolver, CapMonster, AntiCaptcha, and TwoCaptcha when initializing the Capsolver class.
    • Added validation for the proxy parameter in the client initialization, ensuring robustness by preventing the assignment of invalid proxy values.
  • Improvements

    • Updated methods to utilize the selected service URL, enhancing maintainability and ease of use.

Copy link

sourcery-ai bot commented Aug 2, 2024

Reviewer's Guide by Sourcery

This pull request introduces a new 'Service' Enum to allow the selection of different captcha solving services in the 'Capsolver' class. The 'Capsolver' class is updated to include a new 'service' parameter, defaulting to 'Service.Capsolver'. The 'init', 'create_task', and 'get_task_result' methods are modified to use the selected service's API URL. Additionally, minor refactoring is done in the 'solve_funcaptcha' method for consistency in string literals.

File-Level Changes

Files Changes
twikit/_captcha/capsolver.py Introduced a new 'Service' Enum to support multiple captcha solving services and updated the 'Capsolver' class to use this Enum for selecting the service.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

coderabbitai bot commented Aug 2, 2024

Walkthrough

The recent updates to the Capsolver class introduce a Service enumeration, allowing users to select from multiple captcha-solving services. This enhancement improves flexibility and maintainability by consolidating service URL management. The constructor now accepts a service parameter, facilitating the selection of a specific service upon instantiation and streamlining interactions with the API. Additionally, a validation check for the proxy parameter in the client enhances code robustness.

Changes

File Change Summary
twikit/_captcha/capsolver.py Introduced Service enumeration for captcha services. Updated Capsolver constructor to accept service parameter and set api_url accordingly. Modified create_task and get_task_result methods to utilize self.api_url.
twikit/client/client.py Added validation for proxy parameter in the __init__ method, ensuring self.proxy is only assigned when a valid proxy is provided.

Poem

In fields of code where bunnies hop,
A new enum blooms, we can’t stop!
With services many, oh what a treat,
Selecting our path, oh so sweet!
Capsolver's now nimble, like a spring breeze,
Hopping through captchas with effortless ease! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5cdeb53 and 677ad23.

Files selected for processing (1)
  • twikit/_captcha/capsolver.py (3 hunks)
Additional comments not posted (5)
twikit/_captcha/capsolver.py (5)

12-16: Enum values for Service look good.

The Service enumeration correctly defines the URLs for four different captcha-solving services. This enhances flexibility and maintainability.


51-57: Constructor updates look good.

The addition of the service parameter and the assignment of self.api_url enhance flexibility by allowing different captcha-solving services to be used.


63-67: Usage of self.api_url in create_task is correct.

The method correctly uses self.api_url to create tasks, improving maintainability by centralizing the API URL configuration.


72-76: Usage of self.api_url in get_task_result is correct.

The method correctly uses self.api_url to get task results, improving maintainability by centralizing the API URL configuration.


82-100: Method solve_funcaptcha correctly integrates updated methods.

The method correctly uses the create_task and get_task_result methods, which now use self.api_url, ensuring the captcha-solving process works with the selected service.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @PR0F1L3R1 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding error handling for API calls to gracefully handle potential exceptions.
  • Replace string formatting for URLs with f-strings or urllib.parse.urljoin for improved safety and readability.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

@@ -38,58 +48,54 @@ class Capsolver(CaptchaSolver):
def __init__(
self,
api_key: str,
service: Service = Service.Capsolver,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Add validation for the service parameter

Consider adding a check to ensure that the provided service is a valid Service enum value to prevent potential runtime errors.

Suggested change
service: Service = Service.Capsolver,
service: Service = Service.Capsolver,
if not isinstance(service, Service):
raise ValueError(f"Invalid service: {service}. Must be a Service enum value.")

response = httpx.post(
'https://api.capsolver.com/createTask',
"https://%s/createTask" % self.api_url,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Use f-strings for URL construction

Consider using f-strings for better readability, e.g., f"https://{self.api_url}/createTask".

Suggested change
"https://%s/createTask" % self.api_url,
f"https://{self.api_url}/createTask",

from enum import Enum


class Service(Enum):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider using a simple dictionary to map service names to URLs.

The new code introduces several complexities that may not be necessary. Here are some points to consider:

  1. Enum Class: The introduction of the Enum class Service adds an additional layer of abstraction. While it can be useful for extensibility, it increases the complexity of the code.

  2. String Formatting for URLs: Using string formatting to construct URLs dynamically can make the code harder to read and maintain, especially if the URLs are complex or if there are many different endpoints.

  3. Additional Parameter in Constructor: The constructor now requires an additional service parameter, which increases the complexity of initializing the Capsolver class. This change necessitates updating all instances of Capsolver to include this new parameter, potentially leading to more code changes and errors.

  4. Increased Cognitive Load: The combination of these changes increases the cognitive load on developers who need to understand and maintain this code. They now have to understand the Enum class, the new constructor parameter, and the dynamic URL construction.

Consider using a simple dictionary to map service names to URLs. This approach avoids the need for an Enum and keeps the URL construction straightforward, maintaining flexibility while keeping the code simple and easy to understand.

@PR0F1L3R1
Copy link
Author

or may be better just pass api_url as parameter?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 677ad23 and 26e96a1.

Files selected for processing (1)
  • twikit/_captcha/capsolver.py (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • twikit/_captcha/capsolver.py

Copy link
Author

@PR0F1L3R1 PR0F1L3R1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if proxy not passed (old):
image

if proxy not passed (new):
image

2.5 times better

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 26e96a1 and 91af5e3.

Files selected for processing (1)
  • twikit/client/client.py (1 hunks)
Additional context used
Ruff
twikit/client/client.py

95-95: Comparison to None should be cond is not None

Replace with cond is not None

(E711)

@@ -91,7 +91,10 @@ def __init__(

self.http = AsyncClient(proxy=proxy, **kwargs)
self.language = language
self.proxy = proxy

if proxy != None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use is not None for comparison with None.

Replace proxy != None with proxy is not None to follow Python best practices.

-        if proxy != None:
+        if proxy is not None:
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if proxy != None:
if proxy is not None:
Tools
Ruff

95-95: Comparison to None should be cond is not None

Replace with cond is not None

(E711)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 91af5e3 and cc40dcb.

Files selected for processing (1)
  • twikit/client/client.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • twikit/client/client.py

@PR0F1L3R1 PR0F1L3R1 changed the title Add service selecting for captcha solving Add service selecting for captcha solving and optimize Client proxy Aug 2, 2024
Copy link
Author

@PR0F1L3R1 PR0F1L3R1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im tested with my project, and self.proxy useless imo. nothing changed after remove self.proxy, but Client initialization works faster cuz not need to geneate mounts on all threads

self.proxy takes 300ms for every Client. if remove self.proxy, it works fine and not need additional time. works well on 150 threads

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

Successfully merging this pull request may close these issues.

1 participant