Class: Api2Captcha::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/api_2captcha/client.rb

Constant Summary collapse

DEFAULT_DOMAIN =
"2captcha.com"
BASE_URL_FORMAT =
"https://%s"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, callback = nil) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
28
# File 'lib/api_2captcha/client.rb', line 19

def initialize(api_key, callback = nil)
  @api_key = api_key
  @soft_id = soft_id
  @callback = callback
  @default_timeout = 120
  @recaptcha_timeout = 600
  @polling_interval = 10
  @soft_id = 0
  @domain = DEFAULT_DOMAIN
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



13
14
15
# File 'lib/api_2captcha/client.rb', line 13

def api_key
  @api_key
end

#callbackObject

Returns the value of attribute callback.



13
14
15
# File 'lib/api_2captcha/client.rb', line 13

def callback
  @callback
end

#default_timeoutObject

Returns the value of attribute default_timeout.



13
14
15
# File 'lib/api_2captcha/client.rb', line 13

def default_timeout
  @default_timeout
end

#domainObject

Returns the value of attribute domain.



13
14
15
# File 'lib/api_2captcha/client.rb', line 13

def domain
  @domain
end

#polling_intervalObject

Returns the value of attribute polling_interval.



13
14
15
# File 'lib/api_2captcha/client.rb', line 13

def polling_interval
  @polling_interval
end

#recaptcha_timeoutObject

Returns the value of attribute recaptcha_timeout.



13
14
15
# File 'lib/api_2captcha/client.rb', line 13

def recaptcha_timeout
  @recaptcha_timeout
end

#soft_idObject

Returns the value of attribute soft_id.



13
14
15
# File 'lib/api_2captcha/client.rb', line 13

def soft_id
  @soft_id
end

Instance Method Details

#amazon_waf(params) ⇒ Object



169
170
171
# File 'lib/api_2captcha/client.rb', line 169

def amazon_waf(params)
  solve("amazon_waf", **params)
end

#audio(params) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/api_2captcha/client.rb', line 173

def audio(params)
  audio = params.delete("audio")
  audio_content = File.file?(audio) ? File.binread(audio) : audio

  params = params.merge(
    "body" => Base64.strict_encode64(audio_content),
    "lang" => params["lang"]
  )
  solve("audio", **params)
end

#canvas(params) ⇒ Object



141
142
143
144
145
# File 'lib/api_2captcha/client.rb', line 141

def canvas(params)
  params["recaptcha"] = 1
  params["canvas"] = 1
  solve("post", **params)
end

#capy(params) ⇒ Object



132
133
134
# File 'lib/api_2captcha/client.rb', line 132

def capy(params)
  solve("capy", **params)
end

#coordinates(params) ⇒ Object



147
148
149
150
151
# File 'lib/api_2captcha/client.rb', line 147

def coordinates(params)
  params["coordinatescaptcha"] = 1

  solve("post", **params)
end

#cyber_siara(params) ⇒ Object



188
189
190
# File 'lib/api_2captcha/client.rb', line 188

def cyber_siara(params)
  solve("cybersiara", **params)
end

#data_dome(params) ⇒ Object



192
193
194
# File 'lib/api_2captcha/client.rb', line 192

def data_dome(params)
  solve("datadome", **params)
end

#friendly(params) ⇒ Object



200
201
202
# File 'lib/api_2captcha/client.rb', line 200

def friendly(params)
  solve("friendly_captcha", **params)
end

#funcaptcha(params) ⇒ Object



116
117
118
# File 'lib/api_2captcha/client.rb', line 116

def funcaptcha(params)
  solve("funcaptcha", **params)
end

#geetest(params) ⇒ Object



120
121
122
# File 'lib/api_2captcha/client.rb', line 120

def geetest(params)
  solve("geetest", **params)
end

#geetest_v4(params) ⇒ Object



157
158
159
# File 'lib/api_2captcha/client.rb', line 157

def geetest_v4(params)
  solve("geetest_v4", **params)
end

#get_balanceObject



95
96
97
98
# File 'lib/api_2captcha/client.rb', line 95

def get_balance
  response = make_res_request({ "action" => "getbalance" }, "getbalance")
  return response["request"].to_f
end

#get_result(captcha_id) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/api_2captcha/client.rb', line 63

def get_result(captcha_id)
  uri = URI("#{base_url}/res.php?key=#{@api_key}&action=get&id=#{captcha_id}&json=1")
  start_time = Time.now

  loop do
    response = make_request(uri)

    case response
    when Net::HTTPSuccess
      response_json = JSON.parse(response.body)
      if response_json["status"] == 1
        response_json["captcha_id"] = captcha_id
        return response_json
      elsif response_json["request"] == "CAPCHA_NOT_READY"
        sleep(polling_interval)
      else
        raise ApiException, "API Error: #{response_json["request"]}"
      end
    else
      raise NetworkException, "Network Error: #{response.code.to_i}"
    end

    raise TimeoutException, "Timeout" if Time.now - start_time > default_timeout
  end
end

#grid(params) ⇒ Object



136
137
138
139
# File 'lib/api_2captcha/client.rb', line 136

def grid(params)
  params["recaptcha"] = 1
  solve("post", **params)
end

#hcaptcha(params) ⇒ Object



124
125
126
# File 'lib/api_2captcha/client.rb', line 124

def hcaptcha(params)
  solve("hcaptcha", **params)
end

#keycaptcha(params) ⇒ Object



128
129
130
# File 'lib/api_2captcha/client.rb', line 128

def keycaptcha(params)
  solve("keycaptcha", **params)
end

#lemin(params) ⇒ Object



161
162
163
# File 'lib/api_2captcha/client.rb', line 161

def lemin(params)
  solve("lemin", **params)
end

#mt_captcha(params) ⇒ Object



196
197
198
# File 'lib/api_2captcha/client.rb', line 196

def mt_captcha(params)
  solve("mt_captcha", **params)
end

#normal(params) ⇒ Object



100
101
102
# File 'lib/api_2captcha/client.rb', line 100

def normal(params)
  solve("post", **params)
end

#recaptcha_v2(params) ⇒ Object



108
109
110
# File 'lib/api_2captcha/client.rb', line 108

def recaptcha_v2(params)
  solve("userrecaptcha", **params)
end

#recaptcha_v3(params) ⇒ Object



112
113
114
# File 'lib/api_2captcha/client.rb', line 112

def recaptcha_v3(params)
  solve("userrecaptcha", **params)
end

#report(captcha_id, is_correct) ⇒ Object



89
90
91
92
93
# File 'lib/api_2captcha/client.rb', line 89

def report(captcha_id, is_correct)
  report = is_correct ? "reportgood" : "reportbad"
  uri = URI("#{base_url}/res.php?key=#{@api_key}&action=#{report}&id=#{captcha_id}")
  make_request(uri)
end

#rotate(params) ⇒ Object



153
154
155
# File 'lib/api_2captcha/client.rb', line 153

def rotate(params)
  solve("rotatecaptcha", **params)
end

#send(*args) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/api_2captcha/client.rb', line 48

def send(*args)
  raise ArgumentError,
  "Invalid arguments of the send method" unless args.size == 1

  arg = args.first
  if arg.is_a?(String)
    solve("POST", {}, arg, return_id: true)
  elsif arg.is_a?(Hash)
    method = arg.delete(:method) || "POST"
    solve(method, arg, return_id: true)
  else
    raise ArgumentError, "Invalid arguments of the send method"
  end
end

#solve(method, return_id: false, **params) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/api_2captcha/client.rb', line 30

def solve(method, return_id: false, **params)
  params = params.transform_keys(&:to_s)
  params["method"] = method
  params["key"] = @api_key
  params["soft_id"] = @soft_id
  params["json"] = 1

  if @callback
    params["pingback"] = @callback
    return_id = true
  end

  complete_params = get_params(params)
  captcha_id = send_request(complete_params)
  return captcha_id if return_id
  get_result(captcha_id)
end

#text(params) ⇒ Object



104
105
106
# File 'lib/api_2captcha/client.rb', line 104

def text(params)
  solve("textcaptcha", **params)
end

#turnstile(params) ⇒ Object



165
166
167
# File 'lib/api_2captcha/client.rb', line 165

def turnstile(params)
  solve("turnstile", **params)
end

#yandex(params) ⇒ Object



184
185
186
# File 'lib/api_2captcha/client.rb', line 184

def yandex(params)
  solve("yandex", **params)
end