Class: CaptchaSolver::Base

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

Direct Known Subclasses

FunCaptcha, 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.



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

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.



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

def complete_response
  @complete_response
end

#proxy_addressObject (readonly)

Returns the value of attribute proxy_address.



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

def proxy_address
  @proxy_address
end

#rucaptcha_api_keyObject (readonly)

Returns the value of attribute rucaptcha_api_key.



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

def rucaptcha_api_key
  @rucaptcha_api_key
end

#start_responseObject (readonly)

Returns the value of attribute start_response.



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

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:



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

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.



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

def solved_captcha
  return nil unless solved?
  captcha_value
end