Class: CaptchaSolver::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/captcha_solver/base.rb

Direct Known Subclasses

RecaptchaV1, RecaptchaV2

Defined Under Namespace

Classes: FailedError, NotSolvedError, NotStartedError

Constant Summary collapse

TIMEOUTS_BETWEEN_ATTEMPTS =
[40, 40, 40, 40, 40]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Base

Returns a new instance of Base.



14
15
16
17
# File 'lib/captcha_solver/base.rb', line 14

def initialize(args = {})
  @rucaptcha_api_key = ENV['rucaptcha_api_key']
  @proxy_address = args[:proxy_address]
end

Instance Attribute Details

#complete_responseObject (readonly)

Returns the value of attribute complete_response.



11
12
13
# File 'lib/captcha_solver/base.rb', line 11

def complete_response
  @complete_response
end

#proxy_addressObject (readonly)

Returns the value of attribute proxy_address.



11
12
13
# File 'lib/captcha_solver/base.rb', line 11

def proxy_address
  @proxy_address
end

#rucaptcha_api_keyObject (readonly)

Returns the value of attribute rucaptcha_api_key.



11
12
13
# File 'lib/captcha_solver/base.rb', line 11

def rucaptcha_api_key
  @rucaptcha_api_key
end

#start_responseObject (readonly)

Returns the value of attribute start_response.



11
12
13
# File 'lib/captcha_solver/base.rb', line 11

def start_response
  @start_response
end

Instance Method Details

#solveObject

  1. Makes a in.php request in order to get the rucaptcha id.

  2. Waits for 40 seconds, so the captcha can be solved by that time.

  3. Makes a res.php request in order to get the solved captcha.

  4. If captcha is not solved, it tries to do the same 4 more times.

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/captcha_solver/base.rb', line 23

def solve
  start
  raise NotStartedError.new(start_response&.body) unless started?

  TIMEOUTS_BETWEEN_ATTEMPTS.each do |seconds|
    wait_for(seconds)

    complete

    raise FailedError.new(complete_response&.body) if failed?
    return solved_captcha if solved?
  end

  raise NotSolvedError.new(complete_response&.body)
end

#solved_captchaObject

Returns solved captcha



40
41
42
43
# File 'lib/captcha_solver/base.rb', line 40

def solved_captcha
  return nil unless solved?
  captcha_value
end