captcha_solver 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/captcha_solver.rb +2 -0
- data/lib/captcha_solver/base.rb +129 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c68a176a0bea62b9ed9ad44216fbb01742fe840231ae43ae6cae8fe2ee4fa79a
|
4
|
+
data.tar.gz: 7734c4328709d80aa40926ba093d152abb376ff9daa3d09f0bbaf339fad653fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39bf770938486d5c7d3078fe0f483755cf62d3349ff81f7f8bcccbf65c836987a760109aed5aa790af9a58b640e8022d0109a20fd00daab07f41a3287fa21771
|
7
|
+
data.tar.gz: dd6786cb112b2c7f005ca4cf895b87d92af779c92ffc9281d6a355ef4d42245f4282b4ab76370ae0dcc68a7ad9b5f1c02956772f78bd2d6ddad42a734c9ab4af
|
data/lib/captcha_solver.rb
CHANGED
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module CaptchaSolver
|
4
|
+
class Base
|
5
|
+
class NotStartedError < StandardError; end
|
6
|
+
class FailedError < StandardError; end
|
7
|
+
class NotSolvedError < StandardError; end
|
8
|
+
|
9
|
+
TIMEOUTS_BETWEEN_ATTEMPTS = [40, 40, 40, 40, 40]
|
10
|
+
|
11
|
+
attr_reader :rucaptcha_api_key, :proxy_address,
|
12
|
+
:start_response, :complete_response
|
13
|
+
|
14
|
+
def initialize(args)
|
15
|
+
@rucaptcha_api_key = ENV['rucaptcha_api_key']
|
16
|
+
@proxy_address = args[:proxy_address]
|
17
|
+
end
|
18
|
+
|
19
|
+
# 1. Makes a in.php request in order to get the rucaptcha id.
|
20
|
+
# 2. Waits for 40 seconds, so the captcha can be solved by that time.
|
21
|
+
# 3. Makes a res.php request in order to get the solved captcha.
|
22
|
+
# 4. If captcha is not solved, it tries to do the same 4 more times.
|
23
|
+
def solve
|
24
|
+
start
|
25
|
+
raise NotStartedError.new(start_response&.body) unless started?
|
26
|
+
|
27
|
+
TIMEOUTS_BETWEEN_ATTEMPTS.each do |seconds|
|
28
|
+
wait_for(seconds)
|
29
|
+
|
30
|
+
complete
|
31
|
+
|
32
|
+
raise FailedError.new(complete_response&.body) if failed?
|
33
|
+
return solved_captcha if solved?
|
34
|
+
end
|
35
|
+
|
36
|
+
raise NotSolvedError.new(complete_response&.body)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns solved captcha
|
40
|
+
def solved_captcha
|
41
|
+
return nil unless solved?
|
42
|
+
captcha_value
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# Headers that have to be passed in each request. Use it in the child class
|
48
|
+
def default_request_headers
|
49
|
+
{ 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded' }
|
50
|
+
end
|
51
|
+
|
52
|
+
# Params that have to be passed in each request. Use it in the child class
|
53
|
+
def default_request_params
|
54
|
+
{ key: rucaptcha_api_key, json: 1 }
|
55
|
+
end
|
56
|
+
|
57
|
+
# Makes a in.php request. Response must be returned as JSON
|
58
|
+
def make_start_request
|
59
|
+
raise NotImplementedError.new('#make_start_request method must be implemented')
|
60
|
+
end
|
61
|
+
|
62
|
+
# Calls #make_start_request and saves the result in @start_response
|
63
|
+
def start
|
64
|
+
@start_response = make_start_request
|
65
|
+
end
|
66
|
+
|
67
|
+
# Parses response from in.php request and returns rucaptcha id
|
68
|
+
def rucaptcha_id
|
69
|
+
return nil if start_response.blank?
|
70
|
+
JSON.parse(start_response.body)['request']
|
71
|
+
end
|
72
|
+
|
73
|
+
# Checks if a in.php request is successful
|
74
|
+
def started?
|
75
|
+
return false if start_response.blank?
|
76
|
+
return false if errors_regexp.match?(start_response.body)
|
77
|
+
rucaptcha_id.present?
|
78
|
+
end
|
79
|
+
|
80
|
+
# Makes a res.php request. Response must be returned as JSON
|
81
|
+
def make_complete_request
|
82
|
+
raise NotImplementedError.new('#make_complete_request method must be implemented')
|
83
|
+
end
|
84
|
+
|
85
|
+
# Calls #make_complete_request and saves the result in @complete_response
|
86
|
+
def complete
|
87
|
+
return nil unless started?
|
88
|
+
@complete_response = make_complete_request
|
89
|
+
end
|
90
|
+
|
91
|
+
# Checks if captcha solving is failed
|
92
|
+
def failed?
|
93
|
+
return false if complete_response.blank?
|
94
|
+
errors_regexp.match? complete_response.body
|
95
|
+
end
|
96
|
+
|
97
|
+
# Checks if captcha is solved
|
98
|
+
def solved?
|
99
|
+
return false if complete_response.blank?
|
100
|
+
return false if failed?
|
101
|
+
!not_ready_regexp.match?(complete_response.body) && captcha_value.present?
|
102
|
+
end
|
103
|
+
|
104
|
+
# Parses response from res.php request and returns captcha value
|
105
|
+
def captcha_value
|
106
|
+
JSON.parse(complete_response.body)['request']
|
107
|
+
end
|
108
|
+
|
109
|
+
# The error that is returned by res.php request if captcha is not solved yet
|
110
|
+
def not_ready_regexp
|
111
|
+
/CAPCHA_NOT_READY/i
|
112
|
+
end
|
113
|
+
|
114
|
+
# Any error that might be returned by in.php or res.php request
|
115
|
+
def errors_regexp
|
116
|
+
/(ERROR|BANNED|MAX_USER_TURN|REPORT_NOT_RECORDED)/i
|
117
|
+
end
|
118
|
+
|
119
|
+
# Makes a delay between requests
|
120
|
+
def wait_for(seconds)
|
121
|
+
puts "Waiting for #{seconds} seconds"
|
122
|
+
seconds.times do |i|
|
123
|
+
sleep(1)
|
124
|
+
print '.'
|
125
|
+
end
|
126
|
+
puts
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: captcha_solver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Randi Korhonen
|
@@ -17,7 +17,8 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- lib/captcha_solver.rb
|
20
|
-
|
20
|
+
- lib/captcha_solver/base.rb
|
21
|
+
homepage: https://github.com/randikorhonen/captcha_solver
|
21
22
|
licenses:
|
22
23
|
- MIT
|
23
24
|
metadata: {}
|