ruby-2captcha 1.0.3 → 1.0.5

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: 6cc7d94f83479f1d8c57bb4cf2315a2fcb04475a7c9f2ca170b09648d95d830e
4
+ data.tar.gz: 759ff2de9c3fed201fcb467e52b4fe2c0d24af9c006596ed58002c3b2f7b42b4
5
5
  SHA512:
6
- metadata.gz: 852b9b321840c5e27be438c34c133acd3f81e01073c047ec934266fdb3ca0a0a29ecf31d69ff826206f88d5e505900725a4f83a9071f0925cc8453d984b29d6b
7
- data.tar.gz: bcad95da8e13e0257e599f1046d5b54c57af34df1ad1aca2924d611583d9ee168a55f447a813700353b674387dd66e9a1bfdf9260f4b04911eb5173a56c5d0e5
6
+ metadata.gz: 9c9f92edcd7270fa86a6fcc287734596c8a9de31bb491451ab2569adb3d555c104716436cfb386dab9d95e0a4bb747a85e6a4dfc7ff705c4ae2b31689e8615ad
7
+ data.tar.gz: 71bd866a8dfe6e82b49abe66537ec7d2b0bcdef58e6e2bace9f9fc024a0e21e071aaa021cca517a31bf2f1d2b3296c94ece3485a76dab9d2d92190aab86c84aa
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,279 @@ 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, params = {}, file_path = nil, return_id: false)
31
+ params["method"] = method
32
+ params["key"] = @api_key
33
+ params["soft_id"] = @soft_id
34
+ params["json"] = 1
45
35
 
46
- def send(*args)
47
- raise ArgumentError,
48
- "Invalid arguments of the send method" unless args.size == 1
36
+ if @callback
37
+ params["pingback"] = @callback
38
+ return_id = true
39
+ end
49
40
 
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"
41
+ complete_params = get_params(params)
42
+ captcha_id = send_request(complete_params)
43
+ return captcha_id if return_id
44
+ get_result(captcha_id)
58
45
  end
59
- end
60
46
 
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
47
+ def send(*args)
48
+ raise ArgumentError,
49
+ "Invalid arguments of the send method" unless args.size == 1
64
50
 
65
- loop do
66
- response = make_request(uri)
51
+ arg = args.first
52
+ if arg.is_a?(String)
53
+ solve("POST", {}, arg, return_id: true)
54
+ elsif arg.is_a?(Hash)
55
+ method = arg.delete(:method) || "POST"
56
+ solve(method, arg, return_id: true)
57
+ else
58
+ raise ArgumentError, "Invalid arguments of the send method"
59
+ end
60
+ end
67
61
 
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)
62
+ def get_result(captcha_id)
63
+ uri = URI("#{base_url}/res.php?key=#{@api_key}&action=get&id=#{captcha_id}&json=1")
64
+ start_time = Time.now
65
+
66
+ loop do
67
+ response = make_request(uri)
68
+
69
+ case response
70
+ when Net::HTTPSuccess
71
+ response_json = JSON.parse(response.body)
72
+ if response_json["status"] == 1
73
+ return response_json["request"]
74
+ elsif response_json["request"] == "CAPCHA_NOT_READY"
75
+ sleep(polling_interval)
76
+ else
77
+ raise ApiException, "API Error: #{response_json["request"]}"
78
+ end
75
79
  else
76
- raise ApiException, "API Error: #{response_json["error_text"]}"
80
+ raise NetworkException, "Network Error: #{response.code.to_i}"
77
81
  end
78
- else
79
- raise NetworkException, "Network Error: #{response.code.to_i}"
80
- end
81
82
 
82
- raise TimeoutException, "Timeout" if Time.now - start_time > default_timeout
83
+ raise TimeoutException, "Timeout" if Time.now - start_time > default_timeout
84
+ end
83
85
  end
84
- end
85
86
 
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
87
+ def report(captcha_id, is_correct)
88
+ report = is_correct ? "reportgood" : "reportbad"
89
+ uri = URI("#{base_url}/res.php?key=#{@api_key}&action=#{report}&id=#{captcha_id}")
90
+ make_request(uri)
91
+ end
91
92
 
92
- def get_balance
93
- response = make_res_request({ "action" => "getbalance" }, "getbalance")
94
- return response["request"].to_f
95
- end
93
+ def get_balance
94
+ response = make_res_request({ "action" => "getbalance" }, "getbalance")
95
+ return response["request"].to_f
96
+ end
96
97
 
97
- def normal(params)
98
- solve("post", params)
99
- end
98
+ def normal(params)
99
+ solve("post", params)
100
+ end
100
101
 
101
- def text(params)
102
- solve("textcaptcha", params)
103
- end
102
+ def text(params)
103
+ solve("textcaptcha", params)
104
+ end
104
105
 
105
- def recaptcha_v2(params)
106
- solve("userrecaptcha", params)
107
- end
106
+ def recaptcha_v2(params)
107
+ solve("userrecaptcha", params)
108
+ end
108
109
 
109
- def recaptcha_v3(params)
110
- solve("userrecaptcha", params)
111
- end
110
+ def recaptcha_v3(params)
111
+ solve("userrecaptcha", params)
112
+ end
112
113
 
113
- def funcaptcha(params)
114
- solve("funcaptcha", params)
115
- end
114
+ def funcaptcha(params)
115
+ solve("funcaptcha", params)
116
+ end
116
117
 
117
- def geetest(params)
118
- solve("geetest", params)
119
- end
118
+ def geetest(params)
119
+ solve("geetest", params)
120
+ end
120
121
 
121
- def hcaptcha(params)
122
- solve("hcaptcha", params)
123
- end
122
+ def hcaptcha(params)
123
+ solve("hcaptcha", params)
124
+ end
124
125
 
125
- def keycaptcha(params)
126
- solve("keycaptcha", params)
127
- end
126
+ def keycaptcha(params)
127
+ solve("keycaptcha", params)
128
+ end
128
129
 
129
- def capy(params)
130
- solve("capy", params)
131
- end
130
+ def capy(params)
131
+ solve("capy", params)
132
+ end
132
133
 
133
- def grid(params)
134
- params["recaptcha"] = 1
135
- solve("post", params)
136
- end
134
+ def grid(params)
135
+ params["recaptcha"] = 1
136
+ solve("post", params)
137
+ end
137
138
 
138
- def canvas(params)
139
- params["recaptcha"] = 1
140
- params["canvas"] = 1
141
- solve("post", params)
142
- end
139
+ def canvas(params)
140
+ params["recaptcha"] = 1
141
+ params["canvas"] = 1
142
+ solve("post", params)
143
+ end
143
144
 
144
- def coordinates(params)
145
- params["coordinatescaptcha"] = 1
145
+ def coordinates(params)
146
+ params["coordinatescaptcha"] = 1
146
147
 
147
- solve("post", params)
148
- end
148
+ solve("post", params)
149
+ end
149
150
 
150
- def rotate(params)
151
- solve("rotatecaptcha", params)
152
- end
151
+ def rotate(params)
152
+ solve("rotatecaptcha", params)
153
+ end
153
154
 
154
- def geetest_v4(params)
155
- solve("geetest_v4", params)
156
- end
155
+ def geetest_v4(params)
156
+ solve("geetest_v4", params)
157
+ end
157
158
 
158
- def lemin(params)
159
- solve("lemin", params)
160
- end
159
+ def lemin(params)
160
+ solve("lemin", params)
161
+ end
161
162
 
162
- def turnstile(params)
163
- solve("turnstile", params)
164
- end
163
+ def turnstile(params)
164
+ solve("turnstile", params)
165
+ end
165
166
 
166
- def amazon_waf(params)
167
- solve("amazon_waf", params)
168
- end
167
+ def amazon_waf(params)
168
+ solve("amazon_waf", params)
169
+ end
169
170
 
170
- def audio(params)
171
- audio = params.delete(:audio)
172
- audio_content = File.file?(audio) ? File.binread(audio) : audio
171
+ def audio(params)
172
+ audio = params.delete(:audio)
173
+ audio_content = File.file?(audio) ? File.binread(audio) : audio
173
174
 
174
- params = params.merge(
175
- "body" => Base64.strict_encode64(audio_content),
176
- "lang" => params[:lang]
177
- )
178
- solve("audio", params)
179
- end
175
+ params = params.merge(
176
+ "body" => Base64.strict_encode64(audio_content),
177
+ "lang" => params[:lang]
178
+ )
179
+ solve("audio", params)
180
+ end
180
181
 
181
- def yandex(params)
182
- solve("yandex", params)
183
- end
182
+ def yandex(params)
183
+ solve("yandex", params)
184
+ end
184
185
 
185
- private
186
+ private
186
187
 
187
- def base_url
188
- BASE_URL_FORMAT % @domain
189
- end
188
+ def base_url
189
+ BASE_URL_FORMAT % @domain
190
+ end
190
191
 
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
192
+ def send_request(params)
193
+ uri = URI("#{base_url}/in.php")
194
+ req = Net::HTTP::Post.new(uri)
195
+ req.content_type = 'application/json'
196
+ req.body = params.to_json
197
+ captcha_id = get_captcha_id(make_request(uri, req))
198
+ end
198
199
 
199
- def get_params(params)
200
- params[:image].nil? ? params : file_params(params)
201
- end
200
+ def get_params(params)
201
+ params[:image].nil? ? params : file_params(params)
202
+ end
202
203
 
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
204
+ def file_params(params)
205
+ image = params.delete(:image)
206
+ hint_image = params.delete(:hint_image)
207
+
208
+ image_content = get_image_content(image)
209
+ hint_image_content = get_image_content(hint_image) if hint_image
210
+ result_params = {
211
+ "method" => "base64",
212
+ "body" => Base64.strict_encode64(image_content),
213
+ "filename" => File.basename(image),
214
+ "ext" => File.extname(image).delete(".")
215
+ }
216
+
217
+ result_params["imginstructions"] = Base64.strict_encode64(hint_image_content) if hint_image_content
218
+ params.merge(result_params)
219
+ end
219
220
 
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
221
+ def get_image_content(image)
222
+ return download_image(image) if image.start_with?('http')
223
+ return File.binread(image) if File.file?(image)
224
+ image
225
+ end
225
226
 
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}"
227
+ def download_image(url)
228
+ response = URI.open(url)
229
+ if response.status[0] != '200'
230
+ raise StandardError, "File could not be downloaded from url: #{url}"
231
+ end
232
+ response.read
230
233
  end
231
- response.read
232
- end
233
234
 
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
235
+ def handle_response(captcha_id)
236
+ captcha_result = get_result(captcha_id) if @callback.nil?
237
+ @callback&.call(captcha_id)
238
+ captcha_result
239
+ end
239
240
 
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"]
241
+ def get_captcha_id(response)
242
+ case response
243
+ when Net::HTTPSuccess
244
+ response_json = JSON.parse(response.body.strip)
245
+ if response_json["status"] == 1
246
+ response_json["request"]
247
+ else
248
+ raise ApiException, "API Error: #{response_json["error_text"]}"
249
+ end
246
250
  else
247
- raise ApiException, "API Error: #{response.body.strip}"
251
+ raise NetworkException, "Network Error: #{response.code.to_i}"
248
252
  end
249
- else
250
- raise NetworkException, "Network Error: #{response.code.to_i}"
253
+ rescue JSON::ParserError => e
254
+ raise "Failed to parse response: #{e.message}"
251
255
  end
252
- rescue JSON::ParserError => e
253
- raise "Failed to parse response: #{e.message}"
254
- end
255
256
 
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)
257
+ def make_request(uri, req = nil)
258
+ if req.nil?
259
+ Net::HTTP.get_response(uri)
260
+ else
261
+ Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
262
+ http.request(req)
263
+ end
262
264
  end
263
265
  end
264
- end
265
266
 
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
267
+ def make_res_request(request, action)
268
+ uri = URI("#{base_url}/res.php?key=#{@api_key}&action=#{action}&json=1")
269
+ req = Net::HTTP::Post.new(uri)
270
+ req.content_type = 'application/json'
271
+ req.body = request.to_json
271
272
 
272
- response = make_request(uri, req)
273
+ response = make_request(uri, req)
273
274
 
274
- case response
275
- when Net::HTTPSuccess
276
- return JSON.parse(response.body)
277
- else
278
- raise NetworkException, "Network Error: #{response.code.to_i}"
275
+ case response
276
+ when Net::HTTPSuccess
277
+ return JSON.parse(response.body)
278
+ else
279
+ raise Api2Captcha::NetworkException, "Network Error: #{response.code.to_i}"
280
+ end
279
281
  end
280
282
  end
281
283
  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.0.5"
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.0.5
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-03 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.