Class: AntiCaptcha::Client
- Inherits:
-
Object
- Object
- AntiCaptcha::Client
- Defined in:
- lib/anti_captcha/client.rb
Overview
AntiCaptcha::Client is a client that communicates with the Anti Captcha API: anti-captcha.com.
Constant Summary collapse
- BASE_URL =
'https://api.anti-captcha.com/:action'
- PROXYABLE_TASKS =
%w(NoCaptchaTask FunCaptchaTask)
- SUPPORTED_TASKS =
%w(ImageToTextTask NoCaptchaTask FunCaptchaTask)
Instance Attribute Summary collapse
-
#client_key ⇒ Object
Returns the value of attribute client_key.
-
#polling ⇒ Object
Returns the value of attribute polling.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#create_task!(type, options, proxy = nil) ⇒ Hash
Creates a task for solving the selected CAPTCHA type.
-
#decode_fun_captcha!(options, proxy = nil) ⇒ AntiCaptcha::FunCaptchaSolution
Decodes a FunCaptcha CAPTCHA.
-
#decode_image(options) ⇒ Object
Decodes an image CAPTCHA.
-
#decode_image!(options) ⇒ AntiCaptcha::ImageToTextSolution
Decodes an image CAPTCHA.
-
#decode_nocaptcha(options, proxy = nil) ⇒ Object
Decodes a NoCaptcha CAPTCHA.
-
#decode_nocaptcha!(options, proxy = nil) ⇒ AntiCaptcha::NoCaptchaSolution
Decodes a NoCaptcha CAPTCHA.
-
#decode_recaptcha_v3(options) ⇒ Object
Decodes a reCAPTCHA V3.
-
#decode_recaptcha_v3!(options) ⇒ AntiCaptcha::RecaptchaV3Solution
Decodes a reCAPTCHA V3.
-
#get_balance! ⇒ Hash
Retrieves account balance.
-
#get_queue_stats!(queue_id) ⇒ Hash
This method allows you to define if it is a suitable time to upload new tasks.
-
#get_task_result!(task_id) ⇒ Hash
Creates a task for solving the selected CAPTCHA type.
-
#initialize(client_key, options = {}) ⇒ AntiCaptcha::Client
constructor
Creates a client for the Anti Captcha API.
-
#report_incorrect_image_catpcha!(task_id) ⇒ Hash
Complaints are accepted only for image CAPTCHAs.
Constructor Details
#initialize(client_key, options = {}) ⇒ AntiCaptcha::Client
Creates a client for the Anti Captcha API.
24 25 26 27 28 |
# File 'lib/anti_captcha/client.rb', line 24 def initialize(client_key, = {}) self.client_key = client_key self.timeout = [:timeout] || 60 self.polling = [:polling] || 5 end |
Instance Attribute Details
#client_key ⇒ Object
Returns the value of attribute client_key.
10 11 12 |
# File 'lib/anti_captcha/client.rb', line 10 def client_key @client_key end |
#polling ⇒ Object
Returns the value of attribute polling.
10 11 12 |
# File 'lib/anti_captcha/client.rb', line 10 def polling @polling end |
#timeout ⇒ Object
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/anti_captcha/client.rb', line 10 def timeout @timeout end |
Instance Method Details
#create_task!(type, options, proxy = nil) ⇒ Hash
Creates a task for solving the selected CAPTCHA type.
@option proxy [String] :proxy_type
@option proxy [String] :proxy_address
@option proxy [String] :proxy_port
@option proxy [String] :proxy_login
@option proxy [String] :proxy_login
@option proxy [String] :proxy_password
@option proxy [String] :user_agent
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/anti_captcha/client.rb', line 209 def create_task!(type, , proxy = nil) args = { languagePool: ([:language_pool] || 'en'), softId: '859' } case type when 'ImageToTextTask' args[:task] = { type: 'ImageToTextTask', body: [:body64], phrase: [:phrase], case: [:case], numeric: [:numeric], math: [:math], minLength: [:min_length], maxLength: [:max_length], comment: [:comment], } when 'NoCaptchaTask' args[:task] = { type: 'NoCaptchaTask', websiteURL: [:website_url], websiteKey: [:website_key], } when 'RecaptchaV3TaskProxyless' args[:task] = { type: 'RecaptchaV3TaskProxyless', websiteURL: [:website_url], websiteKey: [:website_key], minScore: [:min_score].to_f, pageAction: [:page_action], } when 'FunCaptchaTask' args[:task] = { type: 'FunCaptchaTask', websiteURL: [:website_url], websitePublicKey: [:website_public_key], } else = "Invalid task type: '#{type}'. Allowed types: " + "#{SUPPORTED_TASKS.join(', ')}" raise AntiCaptcha.raise_error() end if PROXYABLE_TASKS.include?(type) if proxy.nil? args[:task][:type] += 'Proxyless' else args.merge!( proxyType: proxy[:proxy_type], proxyAddress: proxy[:proxy_address], proxyPort: proxy[:proxy_port], proxyLogin: proxy[:proxy_login], proxyPassword: proxy[:proxy_password], userAgent: proxy[:user_agent], ) end end request('createTask', args) end |
#decode_fun_captcha!(options, proxy = nil) ⇒ AntiCaptcha::FunCaptchaSolution
Decodes a FunCaptcha CAPTCHA.
@option proxy [String] :proxy_type
@option proxy [String] :proxy_address
@option proxy [String] :proxy_port
@option proxy [String] :proxy_login
@option proxy [String] :proxy_login
@option proxy [String] :proxy_password
@option proxy [String] :user_agent
164 165 166 167 168 |
# File 'lib/anti_captcha/client.rb', line 164 def decode_fun_captcha!(, proxy = nil) task = create_task!('FunCaptchaTask', , proxy) task_result = get_task_result!(task['taskId']) AntiCaptcha::FunCaptchaSolution.new(task_result) end |
#decode_image(options) ⇒ Object
Decodes an image CAPTCHA.
35 36 37 38 39 |
# File 'lib/anti_captcha/client.rb', line 35 def decode_image() decode_image!() rescue AntiCaptcha::ImageToTextSolution.new end |
#decode_image!(options) ⇒ AntiCaptcha::ImageToTextSolution
Decodes an image CAPTCHA.
70 71 72 73 74 75 |
# File 'lib/anti_captcha/client.rb', line 70 def decode_image!() [:body64] = load_captcha() task = create_task!('ImageToTextTask', ) task_result = get_task_result!(task['taskId']) AntiCaptcha::ImageToTextSolution.new(task_result) end |
#decode_nocaptcha(options, proxy = nil) ⇒ Object
Decodes a NoCaptcha CAPTCHA.
82 83 84 85 86 |
# File 'lib/anti_captcha/client.rb', line 82 def decode_nocaptcha(, proxy = nil) decode_nocaptcha!(, proxy) rescue AntiCaptcha::NoCaptchaSolution.new end |
#decode_nocaptcha!(options, proxy = nil) ⇒ AntiCaptcha::NoCaptchaSolution
Decodes a NoCaptcha CAPTCHA.
@option proxy [String] :proxy_type
@option proxy [String] :proxy_address
@option proxy [String] :proxy_port
@option proxy [String] :proxy_login
@option proxy [String] :proxy_login
@option proxy [String] :proxy_password
@option proxy [String] :user_agent
108 109 110 111 112 |
# File 'lib/anti_captcha/client.rb', line 108 def decode_nocaptcha!(, proxy = nil) task = create_task!('NoCaptchaTask', , proxy) task_result = get_task_result!(task['taskId']) AntiCaptcha::NoCaptchaSolution.new(task_result) end |
#decode_recaptcha_v3(options) ⇒ Object
Decodes a reCAPTCHA V3.
119 120 121 122 123 |
# File 'lib/anti_captcha/client.rb', line 119 def decode_recaptcha_v3() decode_recaptcha_v3!() rescue AntiCaptcha::RecaptchaV3Solution.new end |
#decode_recaptcha_v3!(options) ⇒ AntiCaptcha::RecaptchaV3Solution
Decodes a reCAPTCHA V3. Proxy is not supported.
138 139 140 141 142 |
# File 'lib/anti_captcha/client.rb', line 138 def decode_recaptcha_v3!() task = create_task!('RecaptchaV3TaskProxyless', ) task_result = get_task_result!(task['taskId']) AntiCaptcha::RecaptchaV3Solution.new(task_result) end |
#get_balance! ⇒ Hash
Retrieves account balance.
305 306 307 |
# File 'lib/anti_captcha/client.rb', line 305 def get_balance! request('getBalance') end |
#get_queue_stats!(queue_id) ⇒ Hash
This method allows you to define if it is a suitable time to upload new tasks.
323 324 325 326 |
# File 'lib/anti_captcha/client.rb', line 323 def get_queue_stats!(queue_id) args = { queueId: queue_id } request('getQueueStats', args) end |
#get_task_result!(task_id) ⇒ Hash
Creates a task for solving the selected CAPTCHA type.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/anti_captcha/client.rb', line 283 def get_task_result!(task_id) raise AntiCaptcha.raise_error('taskId not received from Anti Captcha.') unless task_id started_at = Time.now loop do api_result = request('getTaskResult', { taskId: task_id }) if api_result['status'] == 'ready' return AntiCaptcha::TaskResult.new(api_result, task_id) end sleep(polling) raise AntiCaptcha::Timeout if (Time.now - started_at) > timeout end end |
#report_incorrect_image_catpcha!(task_id) ⇒ Hash
Complaints are accepted only for image CAPTCHAs. A complaint is checked by 5 workers, 3 of them must confirm it.
336 337 338 339 |
# File 'lib/anti_captcha/client.rb', line 336 def report_incorrect_image_catpcha!(task_id) args = { taskId: task_id } request('getTaskResult', args) end |