Class: TwoCaptcha::Client
- Inherits:
-
Object
- Object
- TwoCaptcha::Client
- 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
-
#key ⇒ Object
Returns the value of attribute key.
-
#polling ⇒ Object
Returns the value of attribute polling.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#balance ⇒ Float
Get balance from your account.
-
#captcha(captcha_id) ⇒ TwoCaptcha::Captcha
Retrieve information from an uploaded captcha.
-
#decode_hcaptcha(options = {}) ⇒ TwoCaptcha::Captcha
Solve hCaptcha.
-
#decode_hcaptcha!(options = {}) ⇒ TwoCaptcha::Captcha
Solve hCaptcha.
-
#decode_image(options = {}) ⇒ TwoCaptcha::Captcha
(also: #decode)
Decode the text from an image (i.e. solve a captcha).
-
#decode_image!(options = {}) ⇒ TwoCaptcha::Captcha
(also: #decode!)
Decode the text from an image (i.e. solve a captcha).
-
#decode_recaptcha_v2(options = {}) ⇒ TwoCaptcha::Captcha
Solve reCAPTCHA v2.
-
#decode_recaptcha_v2!(options = {}) ⇒ TwoCaptcha::Captcha
Solve reCAPTCHA v2.
-
#decode_recaptcha_v3(options = {}) ⇒ TwoCaptcha::Captcha
Solve reCAPTCHA v3.
-
#decode_recaptcha_v3!(options = {}) ⇒ TwoCaptcha::Captcha
Solve reCAPTCHA v3.
-
#initialize(key, options = {}) ⇒ TwoCaptcha::Client
constructor
Create a TwoCaptcha API client.
-
#load ⇒ String
Get current load from 2Captcha.
-
#report!(captcha_id, action = 'reportbad') ⇒ Boolean
Report incorrectly solved captcha for refund.
-
#stats(date) ⇒ String
Get statistics from your account.
-
#upload(options = {}) ⇒ TwoCaptcha::Captcha
Upload a captcha to 2Captcha.
Constructor Details
#initialize(key, options = {}) ⇒ TwoCaptcha::Client
Create a TwoCaptcha API client.
20 21 22 23 24 |
# File 'lib/two_captcha/client.rb', line 20 def initialize(key, = {}) self.key = key self.timeout = [:timeout] || 60 self.polling = [:polling] || 5 end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
8 9 10 |
# File 'lib/two_captcha/client.rb', line 8 def key @key end |
#polling ⇒ Object
Returns the value of attribute polling.
8 9 10 |
# File 'lib/two_captcha/client.rb', line 8 def polling @polling end |
#timeout ⇒ Object
Returns the value of attribute timeout.
8 9 10 |
# File 'lib/two_captcha/client.rb', line 8 def timeout @timeout end |
Instance Method Details
#balance ⇒ Float
Get balance from your account.
266 267 268 |
# File 'lib/two_captcha/client.rb', line 266 def balance request('res', :get, action: 'getbalance').to_f end |
#captcha(captcha_id) ⇒ TwoCaptcha::Captcha
Retrieve information from an uploaded captcha.
237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/two_captcha/client.rb', line 237 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_hcaptcha(options = {}) ⇒ TwoCaptcha::Captcha
Solve hCaptcha.
175 176 177 178 179 |
# File 'lib/two_captcha/client.rb', line 175 def decode_hcaptcha( = {}) decode_hcaptcha!() rescue TwoCaptcha::Error => ex TwoCaptcha::Captcha.new end |
#decode_hcaptcha!(options = {}) ⇒ TwoCaptcha::Captcha
Solve hCaptcha.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/two_captcha/client.rb', line 190 def decode_hcaptcha!( = {}) started_at = Time.now fail(TwoCaptcha::SiteKey) if [:sitekey].empty? = { method: 'hcaptcha' }.merge() decoded_captcha = upload() # 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_image(options = {}) ⇒ TwoCaptcha::Captcha Also known as: decode
Decode the text from an image (i.e. solve a captcha).
33 34 35 36 37 |
# File 'lib/two_captcha/client.rb', line 33 def decode_image( = {}) decode_image!() rescue TwoCaptcha::Error => ex TwoCaptcha::Captcha.new end |
#decode_image!(options = {}) ⇒ TwoCaptcha::Captcha Also known as: decode!
Decode the text from an image (i.e. solve a captcha).
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/two_captcha/client.rb', line 64 def decode_image!( = {}) started_at = Time.now raw64 = load_captcha() fail(TwoCaptcha::InvalidCaptcha) if raw64.to_s.empty? decoded_captcha = upload(.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.
90 91 92 93 94 |
# File 'lib/two_captcha/client.rb', line 90 def decode_recaptcha_v2( = {}) decode_recaptcha_v2!() rescue TwoCaptcha::Error => ex TwoCaptcha::Captcha.new end |
#decode_recaptcha_v2!(options = {}) ⇒ TwoCaptcha::Captcha
Solve reCAPTCHA v2.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/two_captcha/client.rb', line 105 def decode_recaptcha_v2!( = {}) started_at = Time.now fail(TwoCaptcha::GoogleKey) if [:googlekey].empty? = { method: 'userrecaptcha' }.merge() decoded_captcha = upload() # 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.
130 131 132 133 134 |
# File 'lib/two_captcha/client.rb', line 130 def decode_recaptcha_v3( = {}) decode_recaptcha_v3!() rescue TwoCaptcha::Error => ex TwoCaptcha::Captcha.new end |
#decode_recaptcha_v3!(options = {}) ⇒ TwoCaptcha::Captcha
Solve reCAPTCHA v3.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/two_captcha/client.rb', line 147 def decode_recaptcha_v3!( = {}) started_at = Time.now fail(TwoCaptcha::GoogleKey) if [:googlekey].empty? = { method: 'userrecaptcha', version: 'v3', }.merge() decoded_captcha = upload() # 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 |
#load ⇒ String
Get current load from 2Captcha.
284 285 286 |
# File 'lib/two_captcha/client.rb', line 284 def load request('load', :get) end |
#report!(captcha_id, action = 'reportbad') ⇒ Boolean
Report incorrectly solved captcha for refund.
257 258 259 260 |
# File 'lib/two_captcha/client.rb', line 257 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.
276 277 278 |
# File 'lib/two_captcha/client.rb', line 276 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.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/two_captcha/client.rb', line 214 def upload( = {}) args = {} args[:body] = [:raw64] if [:raw64] args[:method] = [:method] || 'base64' args.merge!() 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 |