-
-
Notifications
You must be signed in to change notification settings - Fork 134
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
base: main
Are you sure you want to change the base?
Conversation
Reviewer's Guide by SourceryThis 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
Tips
|
WalkthroughThe recent updates to the Changes
Poem
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this 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
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 forService
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 ofself.api_url
enhance flexibility by allowing different captcha-solving services to be used.
63-67
: Usage ofself.api_url
increate_task
is correct.The method correctly uses
self.api_url
to create tasks, improving maintainability by centralizing the API URL configuration.
72-76
: Usage ofself.api_url
inget_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
: Methodsolve_funcaptcha
correctly integrates updated methods.The method correctly uses the
create_task
andget_task_result
methods, which now useself.api_url
, ensuring the captcha-solving process works with the selected service.
There was a problem hiding this 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
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, |
There was a problem hiding this comment.
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.
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.") |
twikit/_captcha/capsolver.py
Outdated
response = httpx.post( | ||
'https://api.capsolver.com/createTask', | ||
"https://%s/createTask" % self.api_url, |
There was a problem hiding this comment.
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".
"https://%s/createTask" % self.api_url, | |
f"https://{self.api_url}/createTask", |
from enum import Enum | ||
|
||
|
||
class Service(Enum): |
There was a problem hiding this comment.
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:
-
Enum Class: The introduction of the
Enum
classService
adds an additional layer of abstraction. While it can be useful for extensibility, it increases the complexity of the code. -
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.
-
Additional Parameter in Constructor: The constructor now requires an additional
service
parameter, which increases the complexity of initializing theCapsolver
class. This change necessitates updating all instances ofCapsolver
to include this new parameter, potentially leading to more code changes and errors. -
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.
or may be better just pass api_url as parameter? |
There was a problem hiding this 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
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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
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 becond is not None
Replace with
cond is not None
(E711)
twikit/client/client.py
Outdated
@@ -91,7 +91,10 @@ def __init__( | |||
|
|||
self.http = AsyncClient(proxy=proxy, **kwargs) | |||
self.language = language | |||
self.proxy = proxy | |||
|
|||
if proxy != None: |
There was a problem hiding this comment.
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.
if proxy != None: | |
if proxy is not None: |
Tools
Ruff
95-95: Comparison to
None
should becond is not None
Replace with
cond is not None
(E711)
There was a problem hiding this 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
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
There was a problem hiding this 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
Added new parameter of the Capsolver class to select service that will solve captcha
code for select service:
Old code still works fine:
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:
Enhancements:
Summary by CodeRabbit
Summary by CodeRabbit
New Features
Improvements