ruby-2captcha 1.0.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 331e841199be9725ada41287e036cdafb5a9ca9ce1754b14b3580ac27a376053
4
- data.tar.gz: 93c39616225f3e3ab35c021fbd8473496829797792aa80e5272d0868b1d9884f
3
+ metadata.gz: d80f00729bbf9f6f08867c59bb5d6e078dbe7675dc14a0b7dd65922ab640663c
4
+ data.tar.gz: b8dd22428eab71d775a7d597bd42d661f8ded8cd1b94f30c7c54be507ea5a769
5
5
  SHA512:
6
- metadata.gz: 852b9b321840c5e27be438c34c133acd3f81e01073c047ec934266fdb3ca0a0a29ecf31d69ff826206f88d5e505900725a4f83a9071f0925cc8453d984b29d6b
7
- data.tar.gz: bcad95da8e13e0257e599f1046d5b54c57af34df1ad1aca2924d611583d9ee168a55f447a813700353b674387dd66e9a1bfdf9260f4b04911eb5173a56c5d0e5
6
+ metadata.gz: 36f2d745961278c462b64b2cccbc30b6fee82b67e0b4f768f8068babb1c9af5b8e675b88089ad7c81afa33321f288749ac808d01f23966a50c498a70dea4aeac
7
+ data.tar.gz: f91c2ec90c7d09edf188c47b9718bf4152f508f808146b965a3be0e8fa69dbf661cc70df9b8634ff760bcdf54d79d44873119abf09bb353f494203139b670c02
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.0.0
1
+ 2.6.0
data/README.md CHANGED
@@ -34,7 +34,6 @@ A Ruby client for the 2Captcha API.
34
34
  - [Error handling](#error-handling)
35
35
 
36
36
  ## Installation
37
-
38
37
  Add this line to your application's Gemfile:
39
38
 
40
39
  ```bash
@@ -56,7 +55,7 @@ $ gem install ruby-2captcha
56
55
  To use the api2captcha gem, you'll need to import the module and create a Client instance. Here's an example:
57
56
 
58
57
  ```ruby
59
- require 'api-2captcha'
58
+ require 'api_2captcha'
60
59
 
61
60
  client = Api2Captcha.new("YOUR_API_KEY")
62
61
  ```
data/README.ru.md CHANGED
@@ -32,7 +32,6 @@ Ruby-клиент для API 2Captcha.
32
32
  - [Обработка ошибок](#error-handling)
33
33
 
34
34
  ## Установка
35
-
36
35
  Автоматическая установка гема с помощью Bundler. Добавьте следующую строку в ваш Gemfile:
37
36
  ```ruby
38
37
  gem 'ruby-2captcha'
@@ -54,7 +53,10 @@ gem install ruby-2captcha
54
53
  Описание всех необходимых параметров для настройки установленного гема.
55
54
 
56
55
  Экземпляр класса Api2Captcha можно создать следующим образом:
56
+
57
57
  ```ruby
58
+ require 'api_2captcha'
59
+
58
60
  client = Api2Captcha.new("YOUR_API_KEY")
59
61
  ```
60
62
 
@@ -5,277 +5,281 @@ require 'json'
5
5
  require 'base64'
6
6
  require 'open-uri'
7
7
 
8
- class Api2Captcha::Client
9
- DEFAULT_DOMAIN = "2captcha.com"
10
- BASE_URL_FORMAT = "https://%s"
11
-
12
- attr_reader :api_key, :soft_id
13
-
14
- attr_accessor :domain, :callback,
15
- :default_timeout,
16
- :recaptcha_timeout,
17
- :polling_interval
18
-
19
- def initialize(api_key, soft_id = 0, callback = nil)
20
- @api_key = api_key
21
- @soft_id = soft_id
22
- @callback = callback
23
- @default_timeout = 120
24
- @recaptcha_timeout = 600
25
- @polling_interval = 10
26
- @domain = DEFAULT_DOMAIN
27
- end
28
-
29
- def solve(method, params = {}, file_path = nil, return_id: false)
30
- params["method"] = method
31
- params["key"] = @api_key
32
- params["soft_id"] = @soft_id
33
- params["json"] = 1
34
-
35
- if @callback
36
- params["pingback"] = @callback
37
- return_id = true
8
+ module Api2Captcha
9
+ class Client
10
+ DEFAULT_DOMAIN = "2captcha.com"
11
+ BASE_URL_FORMAT = "https://%s"
12
+
13
+ attr_reader :api_key, :soft_id
14
+
15
+ attr_accessor :domain, :callback,
16
+ :default_timeout,
17
+ :recaptcha_timeout,
18
+ :polling_interval
19
+
20
+ def initialize(api_key, soft_id = 0, callback = nil)
21
+ @api_key = api_key
22
+ @soft_id = soft_id
23
+ @callback = callback
24
+ @default_timeout = 120
25
+ @recaptcha_timeout = 600
26
+ @polling_interval = 10
27
+ @domain = DEFAULT_DOMAIN
38
28
  end
39
29
 
40
- complete_params = get_params(params)
41
- captcha_id = send_request(complete_params)
42
- return captcha_id if return_id
43
- get_result(captcha_id)
44
- end
30
+ def solve(method, file_path = nil, return_id: false, **params)
31
+ params = params.transform_keys(&:to_s)
32
+ params["method"] = method
33
+ params["key"] = @api_key
34
+ params["soft_id"] = @soft_id
35
+ params["json"] = 1
45
36
 
46
- def send(*args)
47
- raise ArgumentError,
48
- "Invalid arguments of the send method" unless args.size == 1
37
+ if @callback
38
+ params["pingback"] = @callback
39
+ return_id = true
40
+ end
49
41
 
50
- arg = args.first
51
- if arg.is_a?(String)
52
- solve("POST", {}, arg, return_id: true)
53
- elsif arg.is_a?(Hash)
54
- method = arg.delete(:method) || "POST"
55
- solve(method, arg, return_id: true)
56
- else
57
- raise ArgumentError, "Invalid arguments of the send method"
42
+ complete_params = get_params(params)
43
+ captcha_id = send_request(complete_params)
44
+ return captcha_id if return_id
45
+ get_result(captcha_id)
58
46
  end
59
- end
60
47
 
61
- def get_result(captcha_id)
62
- uri = URI("#{base_url}/res.php?key=#{@api_key}&action=get&id=#{captcha_id}&json=1")
63
- start_time = Time.now
48
+ def send(*args)
49
+ raise ArgumentError,
50
+ "Invalid arguments of the send method" unless args.size == 1
64
51
 
65
- loop do
66
- response = make_request(uri)
52
+ arg = args.first
53
+ if arg.is_a?(String)
54
+ solve("POST", {}, arg, return_id: true)
55
+ elsif arg.is_a?(Hash)
56
+ method = arg.delete(:method) || "POST"
57
+ solve(method, arg, return_id: true)
58
+ else
59
+ raise ArgumentError, "Invalid arguments of the send method"
60
+ end
61
+ end
67
62
 
68
- case response
69
- when Net::HTTPSuccess
70
- response_json = JSON.parse(response.body)
71
- if response_json["status"] == 1
72
- return response_json["request"]
73
- elsif response_json["request"] == "CAPCHA_NOT_READY"
74
- sleep(polling_interval)
63
+ def get_result(captcha_id)
64
+ uri = URI("#{base_url}/res.php?key=#{@api_key}&action=get&id=#{captcha_id}&json=1")
65
+ start_time = Time.now
66
+
67
+ loop do
68
+ response = make_request(uri)
69
+
70
+ case response
71
+ when Net::HTTPSuccess
72
+ response_json = JSON.parse(response.body)
73
+ if response_json["status"] == 1
74
+ response_json["captcha_id"] = captcha_id
75
+ return response_json
76
+ elsif response_json["request"] == "CAPCHA_NOT_READY"
77
+ sleep(polling_interval)
78
+ else
79
+ raise ApiException, "API Error: #{response_json["request"]}"
80
+ end
75
81
  else
76
- raise ApiException, "API Error: #{response_json["error_text"]}"
82
+ raise NetworkException, "Network Error: #{response.code.to_i}"
77
83
  end
78
- else
79
- raise NetworkException, "Network Error: #{response.code.to_i}"
80
- end
81
84
 
82
- raise TimeoutException, "Timeout" if Time.now - start_time > default_timeout
85
+ raise TimeoutException, "Timeout" if Time.now - start_time > default_timeout
86
+ end
83
87
  end
84
- end
85
88
 
86
- def report(captcha_id, is_correct)
87
- report = is_correct ? "reportgood" : "reportbad"
88
- uri = URI("#{base_url}/res.php?key=#{@api_key}&action=#{report}&id=#{captcha_id}")
89
- make_request(uri)
90
- end
89
+ def report(captcha_id, is_correct)
90
+ report = is_correct ? "reportgood" : "reportbad"
91
+ uri = URI("#{base_url}/res.php?key=#{@api_key}&action=#{report}&id=#{captcha_id}")
92
+ make_request(uri)
93
+ end
91
94
 
92
- def get_balance
93
- response = make_res_request({ "action" => "getbalance" }, "getbalance")
94
- return response["request"].to_f
95
- end
95
+ def get_balance
96
+ response = make_res_request({ "action" => "getbalance" }, "getbalance")
97
+ return response["request"].to_f
98
+ end
96
99
 
97
- def normal(params)
98
- solve("post", params)
99
- end
100
+ def normal(params)
101
+ solve("post", params)
102
+ end
100
103
 
101
- def text(params)
102
- solve("textcaptcha", params)
103
- end
104
+ def text(params)
105
+ solve("textcaptcha", params)
106
+ end
104
107
 
105
- def recaptcha_v2(params)
106
- solve("userrecaptcha", params)
107
- end
108
+ def recaptcha_v2(params)
109
+ solve("userrecaptcha", params)
110
+ end
108
111
 
109
- def recaptcha_v3(params)
110
- solve("userrecaptcha", params)
111
- end
112
+ def recaptcha_v3(params)
113
+ solve("userrecaptcha", params)
114
+ end
112
115
 
113
- def funcaptcha(params)
114
- solve("funcaptcha", params)
115
- end
116
+ def funcaptcha(params)
117
+ solve("funcaptcha", params)
118
+ end
116
119
 
117
- def geetest(params)
118
- solve("geetest", params)
119
- end
120
+ def geetest(params)
121
+ solve("geetest", params)
122
+ end
120
123
 
121
- def hcaptcha(params)
122
- solve("hcaptcha", params)
123
- end
124
+ def hcaptcha(params)
125
+ solve("hcaptcha", params)
126
+ end
124
127
 
125
- def keycaptcha(params)
126
- solve("keycaptcha", params)
127
- end
128
+ def keycaptcha(params)
129
+ solve("keycaptcha", params)
130
+ end
128
131
 
129
- def capy(params)
130
- solve("capy", params)
131
- end
132
+ def capy(params)
133
+ solve("capy", params)
134
+ end
132
135
 
133
- def grid(params)
134
- params["recaptcha"] = 1
135
- solve("post", params)
136
- end
136
+ def grid(params)
137
+ params["recaptcha"] = 1
138
+ solve("post", params)
139
+ end
137
140
 
138
- def canvas(params)
139
- params["recaptcha"] = 1
140
- params["canvas"] = 1
141
- solve("post", params)
142
- end
141
+ def canvas(params)
142
+ params["recaptcha"] = 1
143
+ params["canvas"] = 1
144
+ solve("post", params)
145
+ end
143
146
 
144
- def coordinates(params)
145
- params["coordinatescaptcha"] = 1
147
+ def coordinates(params)
148
+ params["coordinatescaptcha"] = 1
146
149
 
147
- solve("post", params)
148
- end
150
+ solve("post", params)
151
+ end
149
152
 
150
- def rotate(params)
151
- solve("rotatecaptcha", params)
152
- end
153
+ def rotate(params)
154
+ solve("rotatecaptcha", params)
155
+ end
153
156
 
154
- def geetest_v4(params)
155
- solve("geetest_v4", params)
156
- end
157
+ def geetest_v4(params)
158
+ solve("geetest_v4", params)
159
+ end
157
160
 
158
- def lemin(params)
159
- solve("lemin", params)
160
- end
161
+ def lemin(params)
162
+ solve("lemin", params)
163
+ end
161
164
 
162
- def turnstile(params)
163
- solve("turnstile", params)
164
- end
165
+ def turnstile(params)
166
+ solve("turnstile", params)
167
+ end
165
168
 
166
- def amazon_waf(params)
167
- solve("amazon_waf", params)
168
- end
169
+ def amazon_waf(params)
170
+ solve("amazon_waf", params)
171
+ end
169
172
 
170
- def audio(params)
171
- audio = params.delete(:audio)
172
- audio_content = File.file?(audio) ? File.binread(audio) : audio
173
+ def audio(params)
174
+ audio = params.delete("audio")
175
+ audio_content = File.file?(audio) ? File.binread(audio) : audio
173
176
 
174
- params = params.merge(
175
- "body" => Base64.strict_encode64(audio_content),
176
- "lang" => params[:lang]
177
- )
178
- solve("audio", params)
179
- end
177
+ params = params.merge(
178
+ "body" => Base64.strict_encode64(audio_content),
179
+ "lang" => params["lang"]
180
+ )
181
+ solve("audio", params)
182
+ end
180
183
 
181
- def yandex(params)
182
- solve("yandex", params)
183
- end
184
+ def yandex(params)
185
+ solve("yandex", params)
186
+ end
184
187
 
185
- private
188
+ private
186
189
 
187
- def base_url
188
- BASE_URL_FORMAT % @domain
189
- end
190
+ def base_url
191
+ BASE_URL_FORMAT % @domain
192
+ end
190
193
 
191
- def send_request(params)
192
- uri = URI("#{base_url}/in.php")
193
- req = Net::HTTP::Post.new(uri)
194
- req.content_type = 'application/json'
195
- req.body = params.to_json
196
- captcha_id = get_captcha_id(make_request(uri, req))
197
- end
194
+ def send_request(params)
195
+ uri = URI("#{base_url}/in.php")
196
+ req = Net::HTTP::Post.new(uri)
197
+ req.content_type = 'application/json'
198
+ req.body = params.to_json
199
+ captcha_id = get_captcha_id(make_request(uri, req))
200
+ end
198
201
 
199
- def get_params(params)
200
- params[:image].nil? ? params : file_params(params)
201
- end
202
+ def get_params(params)
203
+ params["image"].nil? ? params : file_params(params)
204
+ end
202
205
 
203
- def file_params(params)
204
- image = params.delete(:image)
205
- hint_image = params.delete(:hint_image)
206
-
207
- image_content = get_image_content(image)
208
- hint_image_content = get_image_content(hint_image) if hint_image
209
- result_params = {
210
- "method" => "base64",
211
- "body" => Base64.strict_encode64(image_content),
212
- "filename" => File.basename(image),
213
- "ext" => File.extname(image).delete(".")
214
- }
215
-
216
- result_params["imginstructions"] = Base64.strict_encode64(hint_image_content) if hint_image_content
217
- params.merge(result_params)
218
- end
206
+ def file_params(params)
207
+ image = params.delete("image")
208
+ hint_image = params.delete("hint_image")
209
+
210
+ image_content = get_image_content(image)
211
+ hint_image_content = get_image_content(hint_image) if hint_image
212
+ result_params = {
213
+ "method" => "base64",
214
+ "body" => Base64.strict_encode64(image_content),
215
+ "filename" => File.basename(image),
216
+ "ext" => File.extname(image).delete(".")
217
+ }
218
+
219
+ result_params["imginstructions"] = Base64.strict_encode64(hint_image_content) if hint_image_content
220
+ params.merge(result_params)
221
+ end
219
222
 
220
- def get_image_content(image)
221
- return download_image(image) if image.start_with?('http')
222
- return File.binread(image) if File.file?(image)
223
- image
224
- end
223
+ def get_image_content(image)
224
+ return download_image(image) if image.start_with?('http')
225
+ return File.binread(image) if File.file?(image)
226
+ image
227
+ end
225
228
 
226
- def download_image(url)
227
- response = URI.open(url)
228
- if response.status[0] != '200'
229
- raise StandardError, "File could not be downloaded from url: #{url}"
229
+ def download_image(url)
230
+ response = URI.open(url)
231
+ if response.status[0] != '200'
232
+ raise StandardError, "File could not be downloaded from url: #{url}"
233
+ end
234
+ response.read
230
235
  end
231
- response.read
232
- end
233
236
 
234
- def handle_response(captcha_id)
235
- captcha_result = get_result(captcha_id) if @callback.nil?
236
- @callback&.call(captcha_id)
237
- captcha_result
238
- end
237
+ def handle_response(captcha_id)
238
+ captcha_result = get_result(captcha_id) if @callback.nil?
239
+ @callback&.call(captcha_id)
240
+ captcha_result
241
+ end
239
242
 
240
- def get_captcha_id(response)
241
- case response
242
- when Net::HTTPSuccess
243
- response_json = JSON.parse(response.body.strip)
244
- if response_json["status"] == 1
245
- response_json["request"]
243
+ def get_captcha_id(response)
244
+ case response
245
+ when Net::HTTPSuccess
246
+ response_json = JSON.parse(response.body.strip)
247
+ if response_json["status"] == 1
248
+ response_json["request"]
249
+ else
250
+ raise ApiException, "API Error: #{response_json["request"]}"
251
+ end
246
252
  else
247
- raise ApiException, "API Error: #{response.body.strip}"
253
+ raise NetworkException, "Network Error: #{response.code.to_i}"
248
254
  end
249
- else
250
- raise NetworkException, "Network Error: #{response.code.to_i}"
255
+ rescue JSON::ParserError => e
256
+ raise "Failed to parse response: #{e.message}"
251
257
  end
252
- rescue JSON::ParserError => e
253
- raise "Failed to parse response: #{e.message}"
254
- end
255
258
 
256
- def make_request(uri, req = nil)
257
- if req.nil?
258
- Net::HTTP.get_response(uri)
259
- else
260
- Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
261
- http.request(req)
259
+ def make_request(uri, req = nil)
260
+ if req.nil?
261
+ Net::HTTP.get_response(uri)
262
+ else
263
+ Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
264
+ http.request(req)
265
+ end
262
266
  end
263
267
  end
264
- end
265
268
 
266
- def make_res_request(request, action)
267
- uri = URI("#{base_url}/res.php?key=#{@api_key}&action=#{action}&json=1")
268
- req = Net::HTTP::Post.new(uri)
269
- req.content_type = 'application/json'
270
- req.body = request.to_json
269
+ def make_res_request(request, action)
270
+ uri = URI("#{base_url}/res.php?key=#{@api_key}&action=#{action}&json=1")
271
+ req = Net::HTTP::Post.new(uri)
272
+ req.content_type = 'application/json'
273
+ req.body = request.to_json
271
274
 
272
- response = make_request(uri, req)
275
+ response = make_request(uri, req)
273
276
 
274
- case response
275
- when Net::HTTPSuccess
276
- return JSON.parse(response.body)
277
- else
278
- raise NetworkException, "Network Error: #{response.code.to_i}"
277
+ case response
278
+ when Net::HTTPSuccess
279
+ return JSON.parse(response.body)
280
+ else
281
+ raise Api2Captcha::NetworkException, "Network Error: #{response.code.to_i}"
282
+ end
279
283
  end
280
284
  end
281
285
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Api2Captcha
4
- VERSION = "1.0.3"
4
+ VERSION = "1.1.0"
5
5
  end
data/lib/api_2captcha.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "api_2captcha/version"
3
+ require_relative 'api_2captcha/api2captcha_exceptions'
4
+ require_relative 'api_2captcha/client'
5
+ require_relative 'api_2captcha/version'
4
6
 
5
7
  module Api2Captcha
6
- require_relative 'api_2captcha/api2captcha_exceptions'
7
8
  def self.new(*args)
8
9
  Client.new(*args)
9
10
  end
10
11
  end
11
12
 
12
- require 'api_2captcha/client'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-2captcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 2captcha.com
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-15 00:00:00.000000000 Z
11
+ date: 2023-07-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby package for easy integration with the API of 2captcha captcha solving
14
14
  service to bypass recaptcha, hcaptcha, funcaptcha, geetest and solve any other captchas.
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  - !ruby/object:Gem::Version
58
58
  version: '0'
59
59
  requirements: []
60
- rubygems_version: 3.2.3
60
+ rubygems_version: 3.0.1
61
61
  signing_key:
62
62
  specification_version: 4
63
63
  summary: 2Captcha API wrapper for Ruby.