Class: AntiCaptcha::Client

Inherits:
Object
  • Object
show all
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(NoCaptchaTask FunCaptchaTask)
SUPPORTED_TASKS =
%w(ImageToTextTask NoCaptchaTask FunCaptchaTask)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_key, options = {}) ⇒ AntiCaptcha::Client

Creates a client for the Anti Captcha API.

Parameters:

  • client_key (String)

    The key of the Anti Captcha account.

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :timeout (Integer) — default: 60

    Seconds before giving up of a captcha being solved.

  • :polling (Integer) — default: 5

    Seconds before checking answer again.



24
25
26
27
28
# File 'lib/anti_captcha/client.rb', line 24

def initialize(client_key, options = {})
  self.client_key = client_key
  self.timeout    = options[:timeout] || 60
  self.polling    = options[:polling] || 5
end

Instance Attribute Details

#client_keyObject

Returns the value of attribute client_key.



10
11
12
# File 'lib/anti_captcha/client.rb', line 10

def client_key
  @client_key
end

#pollingObject

Returns the value of attribute polling.



10
11
12
# File 'lib/anti_captcha/client.rb', line 10

def polling
  @polling
end

#timeoutObject

Returns the value of attribute timeout.



10
11
12
# File 'lib/anti_captcha/client.rb', line 10

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

Parameters:

  • type (String)

    The type of the CAPTCHA.

  • options (Hash)

    Options hash. # Image to text CAPTCHA @option options [String] :body64 File body encoded in base64. Make sure to

    send it without line breaks.
    

    @option options [Boolean] :phrase If the worker must enter an answer

    with at least one "space".
    

    @option options [Boolean] :case If the answer must be entered with case

    sensitivity.
    

    @option options [Integer] :numeric 0 - no requirements; 1 - only numbers

    are allowed; 2 - any letters are
    allowed except numbers.
    

    @option options [Boolean] :math If the answer must be calculated. @option options [Integer] :min_length Defines minimum length of the

    answer. 0 - no requirements.
    

    @option options [Integer] :max_length Defines maximum length of the

    answer. 0 - no requirements.
    

    @option options [String] :comment Additional comment for workers like

    "enter letters in red color". Result
    is not guaranteed.
    

    # NoCaptcha @option options [String] :website_url Address of target web page. @option options [String] :website_key Recaptcha website key. @option options [String] :language_pool

  • proxy (Hash) (defaults to: nil)

    Not mandatory. A hash with configs of the proxy that has to be used. Defaults to ‘nil`.

Returns:

  • (Hash)

    Information about the task.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/anti_captcha/client.rb', line 209

def create_task!(type, options, proxy = nil)
  args = {
    languagePool: (options[:language_pool] || 'en'),
    softId: '859'
  }

  case type
  when 'ImageToTextTask'
    args[:task] = {
      type:      'ImageToTextTask',
      body:      options[:body64],
      phrase:    options[:phrase],
      case:      options[:case],
      numeric:   options[:numeric],
      math:      options[:math],
      minLength: options[:min_length],
      maxLength: options[:max_length],
      comment:   options[:comment],
    }

  when 'NoCaptchaTask'
    args[:task] = {
      type:       'NoCaptchaTask',
      websiteURL: options[:website_url],
      websiteKey: options[:website_key],
    }

  when 'RecaptchaV3TaskProxyless'
    args[:task] = {
      type: 'RecaptchaV3TaskProxyless',
      websiteURL: options[:website_url],
      websiteKey: options[:website_key],
      minScore:   options[:min_score].to_f,
      pageAction: options[:page_action],
    }

  when 'FunCaptchaTask'
    args[:task] = {
      type:            'FunCaptchaTask',
      websiteURL:       options[:website_url],
      websitePublicKey: options[:website_public_key],
    }

  else
    message = "Invalid task type: '#{type}'. Allowed types: " +
      "#{SUPPORTED_TASKS.join(', ')}"
    raise AntiCaptcha.raise_error(message)
  end

  if PROXYABLE_TASKS.include?(type)
    if proxy.nil?
      args[:task][:type] += 'Proxyless'
    else
      args.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

Parameters:

  • options (Hash)

    Options hash. @option options [String] :website_url @option options [String] :website_public_key @option options [String] :language_pool

  • proxy (Hash) (defaults to: nil)

    Not mandatory. A hash with configs of the proxy that has to be used. Defaults to ‘nil`.

Returns:



164
165
166
167
168
# File 'lib/anti_captcha/client.rb', line 164

def decode_fun_captcha!(options, proxy = nil)
  task = create_task!('FunCaptchaTask', options, proxy)
  task_result = get_task_result!(task['taskId'])
  AntiCaptcha::FunCaptchaSolution.new(task_result)
end

#decode_image(options) ⇒ Object

Decodes an image CAPTCHA.

See Also:

  • AntiCaptcha::Client.`AntiCaptcha`AntiCaptcha::Client`AntiCaptcha::Client#decode_image!`


35
36
37
38
39
# File 'lib/anti_captcha/client.rb', line 35

def decode_image(options)
  decode_image!(options)
rescue
  AntiCaptcha::ImageToTextSolution.new
end

#decode_image!(options) ⇒ AntiCaptcha::ImageToTextSolution

Decodes an image CAPTCHA.

Parameters:

  • options (Hash)

    Options hash. @option options [String] :body64 File body encoded in base64. Make sure to

    send it without line breaks.
    

    @option options [String] :body Binary file body. @option options [String] :path File path of the image to be decoded. @option options [File] :file File instance with image to be

    decoded.
    

    @option options [Boolean] :phrase If the worker must enter an answer

    with at least one "space".
    

    @option options [Boolean] :case If the answer must be entered with case

    sensitivity.
    

    @option options [Integer] :numeric 0 - no requirements; 1 - only numbers

    are allowed; 2 - any letters are
    allowed except numbers.
    

    @option options [Boolean] :math If the answer must be calculated. @option options [Integer] :min_length Defines minimum length of the

    answer. 0 - no requirements.
    

    @option options [Integer] :max_length Defines maximum length of the

    answer. 0 - no requirements.
    

    @option options [String] :comment Additional comment for workers like

    "enter letters in red color". Result
    is not guaranteed.
    

Returns:



70
71
72
73
74
75
# File 'lib/anti_captcha/client.rb', line 70

def decode_image!(options)
  options[:body64] = load_captcha(options)
  task = create_task!('ImageToTextTask', options)
  task_result = get_task_result!(task['taskId'])
  AntiCaptcha::ImageToTextSolution.new(task_result)
end

#decode_nocaptcha(options, proxy = nil) ⇒ Object

Decodes a NoCaptcha CAPTCHA.

See Also:

  • AntiCaptcha::Client.`AntiCaptcha`AntiCaptcha::Client`AntiCaptcha::Client#decode_nocaptcha!`


82
83
84
85
86
# File 'lib/anti_captcha/client.rb', line 82

def decode_nocaptcha(options, proxy = nil)
  decode_nocaptcha!(options, proxy)
rescue
  AntiCaptcha::NoCaptchaSolution.new
end

#decode_nocaptcha!(options, proxy = nil) ⇒ AntiCaptcha::NoCaptchaSolution

Decodes a NoCaptcha 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

Parameters:

  • options (Hash)

    Options hash. @option options [String] :website_url @option options [String] :website_key @option options [String] :language_pool

  • proxy (Hash) (defaults to: nil)

    Not mandatory. A hash with configs of the proxy that has to be used. Defaults to ‘nil`.

Returns:



108
109
110
111
112
# File 'lib/anti_captcha/client.rb', line 108

def decode_nocaptcha!(options, proxy = nil)
  task = create_task!('NoCaptchaTask', options, proxy)
  task_result = get_task_result!(task['taskId'])
  AntiCaptcha::NoCaptchaSolution.new(task_result)
end

#decode_recaptcha_v3(options) ⇒ Object

Decodes a reCAPTCHA V3.

See Also:

  • AntiCaptcha::Client.`AntiCaptcha`AntiCaptcha::Client`AntiCaptcha::Client#decode_recaptcha_v3!`


119
120
121
122
123
# File 'lib/anti_captcha/client.rb', line 119

def decode_recaptcha_v3(options)
  decode_recaptcha_v3!(options)
rescue
  AntiCaptcha::RecaptchaV3Solution.new
end

#decode_recaptcha_v3!(options) ⇒ AntiCaptcha::RecaptchaV3Solution

Decodes a reCAPTCHA V3. Proxy is not supported.

Parameters:

  • options (Hash)

    Options hash. @option options [String] :website_url @option options [String] :website_key @option options [String] :min_score (one of 0.3, 0,5 or 0.7) @option options [String] :page_action @option options [String] :language_pool

Returns:



138
139
140
141
142
# File 'lib/anti_captcha/client.rb', line 138

def decode_recaptcha_v3!(options)
  task = create_task!('RecaptchaV3TaskProxyless', options)
  task_result = get_task_result!(task['taskId'])
  AntiCaptcha::RecaptchaV3Solution.new(task_result)
end

#get_balance!Hash

Retrieves account balance.

Returns:

  • (Hash)

    Information about the account balance.



305
306
307
# File 'lib/anti_captcha/client.rb', line 305

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.

Parameters:

  • queue_id (String)

    The ID of the queue. Options: 1 - standart ImageToText, English language. 2 - standart ImageToText, Russian language. 5 - Recaptcha NoCaptcha tasks. 6 - Recaptcha Proxyless task. 7 - Funcaptcha task. 10 - Funcaptcha Proxyless task.

Returns:

  • (Hash)

    Information about the queue.



323
324
325
326
# File 'lib/anti_captcha/client.rb', line 323

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.

Parameters:

  • task_id (String)

    The ID of the CAPTCHA task.

Returns:

  • (Hash)

    Information about the task.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/anti_captcha/client.rb', line 283

def get_task_result!(task_id)
  raise AntiCaptcha.raise_error('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.

Parameters:

  • task_id (String)

    The ID of the CAPTCHA task.

Returns:

  • (Hash)

    Information about the complaint.



336
337
338
339
# File 'lib/anti_captcha/client.rb', line 336

def report_incorrect_image_catpcha!(task_id)
  args = { taskId: task_id }
  request('getTaskResult', args)
end