Class: AntiCaptcha::Client
- Inherits:
-
Object
- Object
- AntiCaptcha::Client
- Defined in:
- lib/anti_captcha/client.rb
Overview
AntiCaptcha::Client is a client that communicates with the Anti Captcha API: anti-captcha.com.
Constant Summary collapse
- BASE_URL =
'https://api.anti-captcha.com/:action'
- PROXYABLE_TASKS =
%w(RecaptchaV2Task FunCaptchaTask HCaptchaTask GeeTestTask TurnstileTask)
Instance Attribute Summary collapse
-
#client_key ⇒ Object
Returns the value of attribute client_key.
-
#polling ⇒ Object
Returns the value of attribute polling.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#create_task!(type, options, proxy = nil) ⇒ Hash
Creates a task for solving the selected CAPTCHA type.
-
#decode_fun_captcha!(options, proxy = nil) ⇒ AntiCaptcha::FunCaptchaSolution
Decodes a FunCaptcha CAPTCHA.
-
#decode_geetest(options, proxy = nil) ⇒ Object
Decodes a Geetest CAPTCHA.
-
#decode_geetest!(options, proxy = nil) ⇒ AntiCaptcha::GeetestSolution
Decodes a Geetest CAPTCHA.
-
#decode_h_captcha(options, proxy = nil) ⇒ Object
Decodes a HCaptcha CAPTCHA.
-
#decode_h_captcha!(options, proxy = nil) ⇒ AntiCaptcha::HCaptchaSolution
Decodes a HCaptcha CAPTCHA.
-
#decode_image(options) ⇒ Object
Decodes an image CAPTCHA.
-
#decode_image!(options) ⇒ AntiCaptcha::ImageToTextSolution
Decodes an image CAPTCHA.
-
#decode_recaptcha_v2(options, proxy = nil) ⇒ Object
(also: #decode_nocaptcha)
Decodes a reCAPTCHA v2 (NoCaptcha).
-
#decode_recaptcha_v2!(options, proxy = nil) ⇒ AntiCaptcha::RecaptchaV2Solution
(also: #decode_nocaptcha!)
Decodes a reCAPTCHA v2 (NoCaptcha).
-
#decode_recaptcha_v3(options) ⇒ Object
Decodes a reCAPTCHA v3.
-
#decode_recaptcha_v3!(options) ⇒ AntiCaptcha::RecaptchaV3Solution
Decodes a reCAPTCHA v3.
-
#decode_turnstile(options, proxy = nil) ⇒ Object
Decodes a Turnstile CAPTCHA.
-
#decode_turnstile!(options, proxy = nil) ⇒ AntiCaptcha::TurnstileSolution
Decodes a Turnstile CAPTCHA.
-
#get_balance! ⇒ Hash
Retrieves account balance.
-
#get_queue_stats!(queue_id) ⇒ Hash
This method allows you to define if it is a suitable time to upload new tasks.
-
#get_task_result!(task_id) ⇒ Hash
Creates a task for solving the selected CAPTCHA type.
-
#initialize(client_key, options = {}) ⇒ AntiCaptcha::Client
constructor
Creates a client for the Anti Captcha API.
-
#report_incorrect_image_catpcha!(task_id) ⇒ Hash
Complaints are accepted only for image CAPTCHAs.
Constructor Details
#initialize(client_key, options = {}) ⇒ AntiCaptcha::Client
Creates a client for the Anti Captcha API.
23 24 25 26 27 |
# File 'lib/anti_captcha/client.rb', line 23 def initialize(client_key, = {}) self.client_key = client_key self.timeout = [:timeout] || 120 self.polling = [:polling] || 5 end |
Instance Attribute Details
#client_key ⇒ Object
Returns the value of attribute client_key.
9 10 11 |
# File 'lib/anti_captcha/client.rb', line 9 def client_key @client_key end |
#polling ⇒ Object
Returns the value of attribute polling.
9 10 11 |
# File 'lib/anti_captcha/client.rb', line 9 def polling @polling end |
#timeout ⇒ Object
Returns the value of attribute timeout.
9 10 11 |
# File 'lib/anti_captcha/client.rb', line 9 def timeout @timeout end |
Instance Method Details
#create_task!(type, options, proxy = nil) ⇒ Hash
Creates a task for solving the selected CAPTCHA type.
@option proxy [String] :proxy_type
@option proxy [String] :proxy_address
@option proxy [String] :proxy_port
@option proxy [String] :proxy_login
@option proxy [String] :proxy_login
@option proxy [String] :proxy_password
@option proxy [String] :user_agent
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/anti_captcha/client.rb', line 322 def create_task!(type, , proxy = nil) args = { languagePool: ([:language_pool] || 'en'), softId: '859' } case type when 'ImageToTextTask' args[:task] = { type: 'ImageToTextTask', body: [:body64], phrase: [:phrase], case: [:case], numeric: [:numeric], math: [:math], minLength: [:min_length], maxLength: [:max_length], comment: [:comment], } when 'RecaptchaV2Task' args[:task] = { type: 'RecaptchaV2Task', websiteURL: [:website_url], websiteKey: [:website_key], } when 'RecaptchaV3TaskProxyless' args[:task] = { type: 'RecaptchaV3TaskProxyless', websiteURL: [:website_url], websiteKey: [:website_key], minScore: ([:min_score] || 0.3).to_f, pageAction: [:page_action], } args[:isEnterprise] = [:is_enterprise] if [true, false].include?([:is_enterprise]) when 'FunCaptchaTask' args[:task] = { type: 'FunCaptchaTask', websiteURL: [:website_url], websitePublicKey: [:website_public_key], } when 'HCaptchaTask' args[:task] = { type: 'HCaptchaTask', websiteURL: [:website_url], websiteKey: [:website_key], } when 'GeeTestTask' args[:task] = { type: 'GeeTestTask', websiteURL: [:website_url], gt: [:gt], challenge: [:challenge], } args[:geetestApiServerSubdomain] = [:geetest_api_server_subdomain] if ![:geetest_api_server_subdomain].nil? args[:geetestGetLib] = [:geetest_get_lib] if ![:geetest_get_lib].nil? args[:version] = [:version] if ![:version].nil? args[:initParameters] = [:init_parameters] if ![:init_parameters].nil? when 'TurnstileTask' args[:task] = { type: 'TurnstileTask', websiteURL: [:website_url], websiteKey: [:website_key], } else = "Invalid task type: '#{type}'." raise AntiCaptcha::ArgumentError.new() end if PROXYABLE_TASKS.include?(type) if proxy.nil? args[:task][:type] += 'Proxyless' else args[:task].merge!( proxyType: proxy[:proxy_type], proxyAddress: proxy[:proxy_address], proxyPort: proxy[:proxy_port], proxyLogin: proxy[:proxy_login], proxyPassword: proxy[:proxy_password], userAgent: proxy[:user_agent], ) end end request('createTask', args) end |
#decode_fun_captcha!(options, proxy = nil) ⇒ AntiCaptcha::FunCaptchaSolution
Decodes a FunCaptcha CAPTCHA.
@option proxy [String] :proxy_type
@option proxy [String] :proxy_address
@option proxy [String] :proxy_port
@option proxy [String] :proxy_login
@option proxy [String] :proxy_login
@option proxy [String] :proxy_password
@option proxy [String] :user_agent
166 167 168 169 170 |
# File 'lib/anti_captcha/client.rb', line 166 def decode_fun_captcha!(, proxy = nil) task = create_task!('FunCaptchaTask', , proxy) task_result = get_task_result!(task['taskId']) AntiCaptcha::FunCaptchaSolution.new(task_result) end |
#decode_geetest(options, proxy = nil) ⇒ Object
Decodes a Geetest CAPTCHA.
213 214 215 216 217 |
# File 'lib/anti_captcha/client.rb', line 213 def decode_geetest(, proxy = nil) decode_geetest!(, proxy) rescue AntiCaptcha::GeetestSolution.new end |
#decode_geetest!(options, proxy = nil) ⇒ AntiCaptcha::GeetestSolution
Decodes a Geetest CAPTCHA.
@option proxy [String] :proxy_type
@option proxy [String] :proxy_address
@option proxy [String] :proxy_port
@option proxy [String] :proxy_login
@option proxy [String] :proxy_login
@option proxy [String] :proxy_password
@option proxy [String] :user_agent
243 244 245 246 247 |
# File 'lib/anti_captcha/client.rb', line 243 def decode_geetest!(, proxy = nil) task = create_task!('GeeTestTask', , proxy) task_result = get_task_result!(task['taskId']) AntiCaptcha::GeetestSolution.new(task_result) end |
#decode_h_captcha(options, proxy = nil) ⇒ Object
Decodes a HCaptcha CAPTCHA.
177 178 179 180 181 |
# File 'lib/anti_captcha/client.rb', line 177 def decode_h_captcha(, proxy = nil) decode_h_captcha!(, proxy) rescue AntiCaptcha::HCaptchaSolution.new end |
#decode_h_captcha!(options, proxy = nil) ⇒ AntiCaptcha::HCaptchaSolution
Decodes a HCaptcha CAPTCHA.
@option proxy [String] :proxy_type
@option proxy [String] :proxy_address
@option proxy [String] :proxy_port
@option proxy [String] :proxy_login
@option proxy [String] :proxy_login
@option proxy [String] :proxy_password
@option proxy [String] :user_agent
202 203 204 205 206 |
# File 'lib/anti_captcha/client.rb', line 202 def decode_h_captcha!(, proxy = nil) task = create_task!('HCaptchaTask', , proxy) task_result = get_task_result!(task['taskId']) AntiCaptcha::HCaptchaSolution.new(task_result) end |
#decode_image(options) ⇒ Object
Decodes an image CAPTCHA.
34 35 36 37 38 |
# File 'lib/anti_captcha/client.rb', line 34 def decode_image() decode_image!() rescue AntiCaptcha::ImageToTextSolution.new end |
#decode_image!(options) ⇒ AntiCaptcha::ImageToTextSolution
Decodes an image CAPTCHA.
69 70 71 72 73 74 |
# File 'lib/anti_captcha/client.rb', line 69 def decode_image!() [:body64] = load_captcha() task = create_task!('ImageToTextTask', ) task_result = get_task_result!(task['taskId']) AntiCaptcha::ImageToTextSolution.new(task_result) end |
#decode_recaptcha_v2(options, proxy = nil) ⇒ Object Also known as: decode_nocaptcha
Decodes a reCAPTCHA v2 (NoCaptcha).
81 82 83 84 85 |
# File 'lib/anti_captcha/client.rb', line 81 def decode_recaptcha_v2(, proxy = nil) decode_recaptcha_v2!(, proxy) rescue AntiCaptcha::RecaptchaV2Solution.new end |
#decode_recaptcha_v2!(options, proxy = nil) ⇒ AntiCaptcha::RecaptchaV2Solution Also known as: decode_nocaptcha!
Decodes a reCAPTCHA v2 (NoCaptcha).
@option proxy [String] :proxy_type
@option proxy [String] :proxy_address
@option proxy [String] :proxy_port
@option proxy [String] :proxy_login
@option proxy [String] :proxy_login
@option proxy [String] :proxy_password
@option proxy [String] :user_agent
108 109 110 111 112 |
# File 'lib/anti_captcha/client.rb', line 108 def decode_recaptcha_v2!(, proxy = nil) task = create_task!('RecaptchaV2Task', , proxy) task_result = get_task_result!(task['taskId']) AntiCaptcha::RecaptchaV2Solution.new(task_result) end |
#decode_recaptcha_v3(options) ⇒ Object
Decodes a reCAPTCHA v3.
120 121 122 123 124 |
# File 'lib/anti_captcha/client.rb', line 120 def decode_recaptcha_v3() decode_recaptcha_v3!() rescue AntiCaptcha::RecaptchaV3Solution.new end |
#decode_recaptcha_v3!(options) ⇒ AntiCaptcha::RecaptchaV3Solution
Decodes a reCAPTCHA v3. Proxy is not supported.
140 141 142 143 144 |
# File 'lib/anti_captcha/client.rb', line 140 def decode_recaptcha_v3!() task = create_task!('RecaptchaV3TaskProxyless', ) task_result = get_task_result!(task['taskId']) AntiCaptcha::RecaptchaV3Solution.new(task_result) end |
#decode_turnstile(options, proxy = nil) ⇒ Object
Decodes a Turnstile CAPTCHA.
254 255 256 257 258 |
# File 'lib/anti_captcha/client.rb', line 254 def decode_turnstile(, proxy = nil) decode_turnstile!(, proxy) rescue AntiCaptcha::TurnstileSolution.new end |
#decode_turnstile!(options, proxy = nil) ⇒ AntiCaptcha::TurnstileSolution
Decodes a Turnstile CAPTCHA.
@option proxy [String] :proxy_type
@option proxy [String] :proxy_address
@option proxy [String] :proxy_port
@option proxy [String] :proxy_login
@option proxy [String] :proxy_password
277 278 279 280 281 |
# File 'lib/anti_captcha/client.rb', line 277 def decode_turnstile!(, proxy = nil) task = create_task!('TurnstileTask', , proxy) task_result = get_task_result!(task['taskId']) AntiCaptcha::TurnstileSolution.new(task_result) end |
#get_balance! ⇒ Hash
Retrieves account balance.
445 446 447 |
# File 'lib/anti_captcha/client.rb', line 445 def get_balance! request('getBalance') end |
#get_queue_stats!(queue_id) ⇒ Hash
This method allows you to define if it is a suitable time to upload new tasks.
470 471 472 473 |
# File 'lib/anti_captcha/client.rb', line 470 def get_queue_stats!(queue_id) args = { queueId: queue_id } request('getQueueStats', args) end |
#get_task_result!(task_id) ⇒ Hash
Creates a task for solving the selected CAPTCHA type.
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/anti_captcha/client.rb', line 423 def get_task_result!(task_id) raise AntiCaptcha::Error.new('taskId not received from Anti Captcha.') unless task_id started_at = Time.now loop do api_result = request('getTaskResult', { taskId: task_id }) if api_result['status'] == 'ready' return AntiCaptcha::TaskResult.new(api_result, task_id) end sleep(polling) raise AntiCaptcha::Timeout if (Time.now - started_at) > timeout end end |
#report_incorrect_image_catpcha!(task_id) ⇒ Hash
Complaints are accepted only for image CAPTCHAs. A complaint is checked by 5 workers, 3 of them must confirm it.
483 484 485 486 |
# File 'lib/anti_captcha/client.rb', line 483 def report_incorrect_image_catpcha!(task_id) args = { taskId: task_id } request('getTaskResult', args) end |