Class: TwoCaptcha::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/two_captcha/client.rb

Overview

TwoCaptcha::Client is a client that communicates with the TwoCaptcha API: 2captcha.com/.

Constant Summary collapse

BASE_URL =
'http://2captcha.com/:action.php'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, options = {}) ⇒ TwoCaptcha::Client

Create a TwoCaptcha API client.

Parameters:

  • Captcha (String)

    key of the TwoCaptcha 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 check_answer again



20
21
22
23
24
# File 'lib/two_captcha/client.rb', line 20

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

Instance Attribute Details

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/two_captcha/client.rb', line 8

def key
  @key
end

#pollingObject

Returns the value of attribute polling.



8
9
10
# File 'lib/two_captcha/client.rb', line 8

def polling
  @polling
end

#timeoutObject

Returns the value of attribute timeout.



8
9
10
# File 'lib/two_captcha/client.rb', line 8

def timeout
  @timeout
end

Instance Method Details

#balanceFloat

Get balance from your account.

Returns:

  • (Float)

    Balance in USD.



232
233
234
# File 'lib/two_captcha/client.rb', line 232

def balance
  request('res', :get, action: 'getbalance').to_f
end

#captcha(captcha_id) ⇒ TwoCaptcha::Captcha

Retrieve information from an uploaded captcha.

Parameters:

  • captcha_id (Integer)

    Numeric ID of the captcha.

Returns:



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/two_captcha/client.rb', line 203

def captcha(captcha_id)
  response = request('res', :get, action: 'get', id: captcha_id)

  decoded_captcha = TwoCaptcha::Captcha.new(id: captcha_id)
  decoded_captcha.api_response = response

  if response.match(/\AOK\|/)
    decoded_captcha.text = response.split('|', 2)[1]
  end

  decoded_captcha
end

#decode(options = {}) ⇒ TwoCaptcha::Captcha

Decode the text from an image (i.e. solve a captcha).

Parameters:

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

    Options hash. Check docs for the method decode!.

Returns:

  • (TwoCaptcha::Captcha)

    The captcha (with solution) or an empty captcha instance if something goes wrong.



33
34
35
36
37
# File 'lib/two_captcha/client.rb', line 33

def decode(options = {})
  decode!(options)
rescue TwoCaptcha::Error => ex
  TwoCaptcha::Captcha.new
end

#decode!(options = {}) ⇒ TwoCaptcha::Captcha

Decode the text from an image (i.e. solve a captcha).

Parameters:

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

    Options hash.

Options Hash (options):

  • :url (String)

    URL of the image to be decoded.

  • :path (String)

    File path of the image to be decoded.

  • :file (File)

    File instance with image to be decoded.

  • :raw (String)

    Binary content of the image to be decoded.

  • :raw64 (String)

    Binary content encoded in base64 of the image to be decoded.

  • :phrase (Integer) — default: 0
  • :regsense (Integer) — default: 0
  • :numeric (Integer) — default: 0
  • :calc (Integer) — default: 0
  • :min_len (Integer) — default: 0
  • :max_len (Integer) — default: 0
  • :language (Integer) — default: 0
  • :header_acao (Integer) — default: 0
  • :id_constructor (Integer) — default: 0

    23 if new reCAPTCHA.

  • :coordinatescaptcha (Integer) — default: 0

    1 if clickable captcha.

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/two_captcha/client.rb', line 63

def decode!(options = {})
  started_at = Time.now

  raw64 = load_captcha(options)
  fail(TwoCaptcha::InvalidCaptcha) if raw64.to_s.empty?

  decoded_captcha = upload(options.merge(raw64: raw64))

  # pool untill the answer is ready
  while decoded_captcha.text.to_s.empty?
    sleep(polling)
    decoded_captcha = captcha(decoded_captcha.id)
    fail TwoCaptcha::Timeout if (Time.now - started_at) > timeout
  end

  decoded_captcha
end

#decode_recaptcha_v2(options = {}) ⇒ TwoCaptcha::Captcha

Solve reCAPTCHA v2.

Parameters:

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

    Options hash. Check docs for the method decode!.

Returns:



88
89
90
91
92
# File 'lib/two_captcha/client.rb', line 88

def decode_recaptcha_v2(options = {})
  decode_recaptcha_v2!(options)
rescue TwoCaptcha::Error => ex
  TwoCaptcha::Captcha.new
end

#decode_recaptcha_v2!(options = {}) ⇒ TwoCaptcha::Captcha

Solve reCAPTCHA v2.

Parameters:

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

    Options hash.

Options Hash (options):

  • :googlekey (String)

    The open key of the site in which recaptcha is installed.

  • :pageurl (String)

    The URL of the page where the recaptcha is encountered.

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/two_captcha/client.rb', line 103

def decode_recaptcha_v2!(options = {})
  started_at = Time.now

  fail(TwoCaptcha::GoogleKey) if options[:googlekey].empty?

  upload_options = {
    method: 'userrecaptcha',
    googlekey: options[:googlekey],
    pageurl: options[:pageurl]
  }
  decoded_captcha = upload(upload_options)

  # pool untill the answer is ready
  while decoded_captcha.text.to_s.empty?
    sleep([polling, 10].max) # sleep at least 10 seconds
    decoded_captcha = captcha(decoded_captcha.id)
    fail TwoCaptcha::Timeout if (Time.now - started_at) > timeout
  end

  decoded_captcha
end

#decode_recaptcha_v3(options = {}) ⇒ TwoCaptcha::Captcha

Solve reCAPTCHA v3.

Parameters:

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

    Options hash. Check docs for the method decode!.

Returns:



132
133
134
135
136
# File 'lib/two_captcha/client.rb', line 132

def decode_recaptcha_v3(options = {})
  decode_recaptcha_v3!(options)
rescue TwoCaptcha::Error => ex
  TwoCaptcha::Captcha.new
end

#decode_recaptcha_v3!(options = {}) ⇒ TwoCaptcha::Captcha

Solve reCAPTCHA v3.

Parameters:

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

    Options hash.

Options Hash (options):

  • :googlekey (String)

    The open key of the site in which recaptcha is installed.

  • :pageurl (String)

    The URL of the page where the recaptcha is encountered.

  • :action (String)

    The action paramenter present on the page that uses recaptcha.

  • :min_score (String)

    The minimum score necessary to pass the challenge.

Returns:



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/two_captcha/client.rb', line 149

def decode_recaptcha_v3!(options = {})
  started_at = Time.now

  fail(TwoCaptcha::GoogleKey) if options[:googlekey].empty?

  upload_options = {
    method:    'userrecaptcha',
    version:   'v3',
    googlekey: options[:googlekey],
    pageurl:   options[:pageurl],
    action:    options[:action],
    min_score: options[:min_score]
  }
  decoded_captcha = upload(upload_options)

  # pool untill the answer is ready
  while decoded_captcha.text.to_s.empty?
    sleep([polling, 10].max) # sleep at least 10 seconds
    decoded_captcha = captcha(decoded_captcha.id)
    fail TwoCaptcha::Timeout if (Time.now - started_at) > timeout
  end

  decoded_captcha
end

#loadString

Get current load from 2Captcha.

Returns:

  • (String)

    Load in an XML string.



250
251
252
# File 'lib/two_captcha/client.rb', line 250

def load
  request('load', :get)
end

#report!(captcha_id, action = 'reportbad') ⇒ Boolean

Report incorrectly solved captcha for refund.

Parameters:

  • id (Integer)

    Numeric ID of the captcha.

  • action (Integer) (defaults to: 'reportbad')

    ‘reportbad’ (default) or ‘reportgood’.

Returns:

  • (Boolean)

    true if correctly reported



223
224
225
226
# File 'lib/two_captcha/client.rb', line 223

def report!(captcha_id, action = 'reportbad')
  response = request('res', :get, action: action, id: captcha_id)
  response == 'OK_REPORT_RECORDED'
end

#stats(date) ⇒ String

Get statistics from your account.

Parameters:

  • date (Date)

    Date when the statistics were collected.

Returns:

  • (String)

    Statistics from date in an XML string.



242
243
244
# File 'lib/two_captcha/client.rb', line 242

def stats(date)
  request('res', :get, action: 'getstats', date: date.strftime('%Y-%m-%d'))
end

#upload(options = {}) ⇒ TwoCaptcha::Captcha

Upload a captcha to 2Captcha.

This method will not return the solution. It helps on separating concerns.

Returns:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/two_captcha/client.rb', line 180

def upload(options = {})
  args = {}
  args[:body]   = options[:raw64] if options[:raw64]
  args[:method] = options[:method] || 'base64'
  args.merge!(options)
  response = request('in', :multipart, args)

  unless response.match(/\AOK\|/)
    fail(TwoCaptcha::Error, 'Unexpected API Response')
  end

  TwoCaptcha::Captcha.new(
    id: response.split('|', 2)[1],
    api_response: response
  )
end