anti-captcha 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f84fee14d600df9262d135fde340a8fc5b3943c
4
+ data.tar.gz: 09627a5810cd2c188f7f2536f57b7fcbd53899b9
5
+ SHA512:
6
+ metadata.gz: 05c54a53eff0b1593125e3ae21b5fa2eafc0fc3ab734274fe128943dad46726193bc3add3df0fb96931a41ab61e608f740ccd047e4e9d226666186ff54ff0cf8
7
+ data.tar.gz: 51a374ba80791ab2f056246215e72ac981e9a4c93d9c8d3bf7e251be61913f5eb8e2f41bcca88e5f2f6aae044b259f015092e523ed315fce045c44d6bb08fe17
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 2.0.0
3
+
4
+ gemfile:
5
+ - Gemfile
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in antigate_rb.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 debbbbie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,42 @@
1
+ # AntiCaptcha [![Build Status](https://secure.travis-ci.org/pomnikita/antigate_rb.png)](http://travis-ci.org/pomnikita/antigate_rb)
2
+
3
+ anti-captcha.com ruby api wrapper
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'anti-captcha'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install anti-captcha
18
+
19
+ ## Usage example
20
+
21
+ Configure with api params
22
+
23
+ ```ruby
24
+ AntiCaptcha.configure do |config|
25
+ config.key = 'api_key'
26
+ config.min_len = 6
27
+ config.max_len = 10
28
+ end
29
+ ```
30
+
31
+ ```ruby
32
+ @client = AntiCaptcha::Client.new(retries_count: 5, phrase: 1)
33
+ code = @client.code(image_content)
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task default: :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs.push "lib"
8
+ t.test_files = FileList['test/*_test.rb']
9
+ t.verbose = true
10
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'anti-captcha/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "anti-captcha"
8
+ spec.version = AntiCaptcha::VERSION
9
+ spec.authors = ["Nikita Pomiashchii"]
10
+ spec.email = ["pomnikita@gmail.com"]
11
+ spec.summary = "AntiCaptcha api ruby wrapper"
12
+ spec.description = "AntiCaptcha api ruby wrapper"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "minitest"
23
+ spec.add_development_dependency "webmock"
24
+
25
+ spec.add_dependency "httpi"
26
+ spec.add_dependency "activesupport"
27
+ end
@@ -0,0 +1,16 @@
1
+ require 'anti-captcha/version'
2
+ require 'anti-captcha/configuration'
3
+ require 'anti-captcha/client'
4
+ require 'anti-captcha/error'
5
+
6
+ module AntiCaptcha
7
+
8
+ def self.configure(&block)
9
+ yield(configuration)
10
+ end
11
+
12
+ def self.configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ end
@@ -0,0 +1,113 @@
1
+ require 'httpi'
2
+ require 'active_support/core_ext/string'
3
+ require 'active_support/core_ext/object'
4
+
5
+ module AntiCaptcha
6
+ class Client
7
+
8
+ def initialize(options = {})
9
+ @retries_count = options.delete(:retries_count) || 10
10
+ @sleep = options.delete(:sleep) || 5
11
+ @options = AntiCaptcha.configuration.options.merge(options)
12
+ end
13
+
14
+ def decode(image)
15
+ case request_image(image)
16
+ when /OK\|(.+)/
17
+ @captcha_id = $1
18
+ check
19
+ when /^ERROR_(.+)/
20
+ raise error_class($1)
21
+ else
22
+ raise UnknownResponse
23
+ end
24
+ end
25
+
26
+ def check
27
+ return if @captcha_id.blank?
28
+ begin
29
+ get_status
30
+ rescue CaptchaNotReady => e
31
+ attempt ||= @retries_count
32
+ raise e if (attempt -= 1) < 0
33
+ sleep @sleep
34
+ retry
35
+ end
36
+ end
37
+
38
+ def report_bad
39
+ return if @captcha_id.blank?
40
+ request_report_bad
41
+ end
42
+
43
+ def get_balance
44
+ request_balance
45
+ end
46
+
47
+ def get_stats(date = Date.today)
48
+ request_stats date
49
+ end
50
+
51
+ private
52
+
53
+ def get_status
54
+ case request_status
55
+ when /^OK\|(.+)/
56
+ $1
57
+ when 'CAPCHA_NOT_READY'
58
+ raise CaptchaNotReady
59
+ when /^ERROR_(.+)/
60
+ raise error_class($1)
61
+ else
62
+ raise UnknownResponse
63
+ end
64
+ end
65
+
66
+ def request_status
67
+ request 'http://anti-captcha.com/res.php',
68
+ key: AntiCaptcha.configuration.key,
69
+ action: 'get',
70
+ id: @captcha_id
71
+ end
72
+
73
+ def request_image(image)
74
+ request 'http://anti-captcha.com/in.php',
75
+ @options.merge(method: 'base64', body: Base64.encode64(image))
76
+ end
77
+
78
+ def request_report_bad
79
+ request 'http://anti-captcha.com/res.php',
80
+ key: AntiCaptcha.configuration.key,
81
+ action: 'reportbad',
82
+ id: @captcha_id
83
+ end
84
+
85
+ def request_balance
86
+ request 'http://anti-captcha.com/res.php',
87
+ key: AntiCaptcha.configuration.key,
88
+ action: 'getbalance'
89
+ end
90
+
91
+ def request_stats(date)
92
+ request 'http://anti-captcha.com/res.php',
93
+ key: AntiCaptcha.configuration.key,
94
+ action: 'getstats',
95
+ date: date.strftime('%Y-%m-%d')
96
+ end
97
+
98
+ def request(url, params)
99
+ request = HTTPI::Request.new.tap do |request|
100
+ request.url = url
101
+ request.body = params
102
+ end
103
+ HTTPI.post(request).body
104
+ end
105
+
106
+ def error_class(string)
107
+ "AntiCaptcha::#{string.downcase.classify}".constantize
108
+ rescue NameError
109
+ UnknownErrorResponse
110
+ end
111
+
112
+ end
113
+ end
@@ -0,0 +1,32 @@
1
+ module AntiCaptcha
2
+ class Configuration
3
+
4
+ @@config_keys = []
5
+
6
+ def self.config_key(key, default_value = nil)
7
+ attr_accessor key
8
+ @@config_keys << key
9
+ if default_value
10
+ define_method key do
11
+ instance_variable_get(:"@#{key}") || default_value
12
+ end
13
+ end
14
+ end
15
+
16
+ config_key :key
17
+ config_key :phrase, 0
18
+ config_key :regsense, 0
19
+ config_key :numeric, 0
20
+ config_key :calc, 0
21
+ config_key :min_len, 0
22
+ config_key :max_len, 0
23
+ config_key :is_russian, 0
24
+
25
+ def options
26
+ @@config_keys.each_with_object({}) do |key, hash|
27
+ hash[key] = __send__(key) if __send__(key)
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,48 @@
1
+ module AntiCaptcha
2
+
3
+ class Error < StandardError
4
+ end
5
+
6
+ class UnknownResponse < Error
7
+ end
8
+
9
+ class UnknownErrorResponse < Error
10
+ end
11
+
12
+ class WrongUserKey < Error
13
+ end
14
+
15
+ class KeyDoesNotExist < Error
16
+ end
17
+
18
+ class ZeroBalance < Error
19
+ end
20
+
21
+ class NoSlotAvailable < Error
22
+ end
23
+
24
+ class ZeroCaptchaFilesize < Error
25
+ end
26
+
27
+ class TooBigCaptchaFilesize < Error
28
+ end
29
+
30
+ class WrongFileExtension < Error
31
+ end
32
+
33
+ class ImageTypeNotSupported < Error
34
+ end
35
+
36
+ class IpNotAllowed < Error
37
+ end
38
+
39
+ class CaptchaNotReady < Error
40
+ end
41
+
42
+ class WrongIdFormat < Error
43
+ end
44
+
45
+ class CaptchaUnsolvable < Error
46
+ end
47
+
48
+ end
@@ -0,0 +1,3 @@
1
+ module AntiCaptcha
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,130 @@
1
+ require 'minitest/autorun'
2
+ require 'webmock/minitest'
3
+ require 'anti-captcha'
4
+
5
+ HTTPI.log = false
6
+
7
+ class TestAntiCaptchaConfigure < MiniTest::Unit::TestCase
8
+ def setup
9
+ AntiCaptcha.configure do |config|
10
+ config.key = 'test_key'
11
+ config.phrase = 1
12
+ end
13
+ end
14
+
15
+ def test_configuration_default_options
16
+ assert_equal({ key: "test_key", phrase: 1, regsense: 0, numeric: 0, calc: 0,
17
+ min_len: 0, max_len: 0, is_russian: 0 },
18
+ AntiCaptcha.configuration.options)
19
+ end
20
+ end
21
+
22
+ class TestAntiCaptcha < MiniTest::Unit::TestCase
23
+ def setup
24
+ AntiCaptcha.configure do |config|
25
+ config.key = 'test_key'
26
+ end
27
+ @client = AntiCaptcha::Client.new(sleep: 0, retries_count: 1)
28
+ end
29
+
30
+ def test_success_request
31
+ stub_request(:post, "http://anti-captcha.com/in.php").
32
+ to_return(status: 200, body: 'OK|request_id')
33
+
34
+ stub_request(:post, "http://anti-captcha.com/res.php").
35
+ with(body: {"action"=>"get", "id"=>"request_id", "key"=>"test_key"}).
36
+ to_return(status: 200, body: 'CAPCHA_NOT_READY').then.
37
+ to_return(status: 200, body: 'OK|result')
38
+
39
+ result = @client.decode 'file_content'
40
+ assert_equal 'result', result
41
+ end
42
+
43
+ def test_with_retries
44
+ stub_request(:post, "http://anti-captcha.com/in.php").
45
+ to_return(status: 200, body: 'OK|request_id')
46
+
47
+ stub_request(:post, "http://anti-captcha.com/res.php").
48
+ with(body: {"action"=>"get", "id"=>"request_id", "key"=>"test_key"}).
49
+ to_return(status: 200, body: 'CAPCHA_NOT_READY').then.
50
+ to_return(status: 200, body: 'CAPCHA_NOT_READY')
51
+
52
+ assert_raises AntiCaptcha::CaptchaNotReady do
53
+ @client.decode 'file_content'
54
+ end
55
+ end
56
+
57
+ %w(ERROR_KEY_DOES_NOT_EXIST ERROR_WRONG_ID_FORMAT ERROR_CAPTCHA_UNSOLVABLE).each do |error_message|
58
+ define_method "test_status_#{error_message}" do
59
+ stub_request(:post, "http://anti-captcha.com/in.php").
60
+ to_return(status: 200, body: 'OK|request_id')
61
+
62
+ stub_request(:post, "http://anti-captcha.com/res.php").
63
+ with(body: {"action"=>"get", "id"=>"request_id", "key"=>"test_key"}).
64
+ to_return(status: 200, body: error_message)
65
+
66
+ assert_raises "AntiCaptcha::#{error_message.gsub(/^ERROR_/,'').downcase.classify}".constantize do
67
+ @client.decode 'file_content'
68
+ end
69
+ end
70
+ end
71
+
72
+ %w(ERROR_WRONG_USER_KEY ERROR_KEY_DOES_NOT_EXIST ERROR_ZERO_BALANCE
73
+ ERROR_NO_SLOT_AVAILABLE ERROR_ZERO_CAPTCHA_FILESIZE
74
+ ERROR_TOO_BIG_CAPTCHA_FILESIZE ERROR_WRONG_FILE_EXTENSION
75
+ ERROR_IMAGE_TYPE_NOT_SUPPORTED ERROR_IP_NOT_ALLOWED).each do |error_message|
76
+
77
+ define_method("test_request_#{error_message}") do
78
+ stub_request(:post, "http://anti-captcha.com/in.php").
79
+ to_return(status: 200, body: error_message)
80
+
81
+ assert_raises "AntiCaptcha::#{error_message.gsub(/^ERROR_/,'').downcase.classify}".constantize do
82
+ @client.decode 'file_content'
83
+ end
84
+ end
85
+ end
86
+
87
+ def test_unknown_response
88
+ stub_request(:post, "http://anti-captcha.com/in.php").
89
+ to_return(status: 200, body: 'unknown')
90
+
91
+ assert_raises AntiCaptcha::UnknownResponse do
92
+ @client.decode 'file_content'
93
+ end
94
+ end
95
+
96
+ def test_unknown_error_response
97
+ stub_request(:post, "http://anti-captcha.com/in.php").
98
+ to_return(status: 200, body: 'ERROR_unknown')
99
+
100
+ assert_raises AntiCaptcha::UnknownErrorResponse do
101
+ @client.decode 'file_content'
102
+ end
103
+ end
104
+
105
+ def test_report_bad
106
+ @client.instance_variable_set :@captcha_id, 'test_id'
107
+ stub_request(:post, "http://anti-captcha.com/res.php").
108
+ with(body: {"action"=>"reportbad", "id"=>"test_id", "key"=>"test_key"}).
109
+ to_return(status: 200, body: 'ok')
110
+
111
+ assert @client.report_bad
112
+ end
113
+
114
+ def test_get_balance
115
+ stub_request(:post, "http://anti-captcha.com/res.php").
116
+ with(body: {"action"=>"getbalance", "key"=>"test_key"}).
117
+ to_return(status: 200, body: '10.05')
118
+
119
+ assert_equal '10.05', @client.get_balance
120
+ end
121
+
122
+ def test_get_stats
123
+ stub_request(:post, "http://anti-captcha.com/res.php").
124
+ with(body: {"action" => "getstats", "key"=>"test_key", "date" => Date.today.strftime('%Y-%m-%d')}).
125
+ to_return(status: 200, body: '<?xml version="1.0"?><response></response>')
126
+
127
+ assert_equal '<?xml version="1.0"?><response></response>', @client.get_stats
128
+ end
129
+
130
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: anti-captcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nikita Pomiashchii
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httpi
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: AntiCaptcha api ruby wrapper
84
+ email:
85
+ - pomnikita@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .travis.yml
92
+ - Gemfile
93
+ - LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - anti-captcha.gemspec
97
+ - lib/anti-captcha.rb
98
+ - lib/anti-captcha/client.rb
99
+ - lib/anti-captcha/configuration.rb
100
+ - lib/anti-captcha/error.rb
101
+ - lib/anti-captcha/version.rb
102
+ - test/anti-captcha_test.rb
103
+ homepage: ''
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.8
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: AntiCaptcha api ruby wrapper
127
+ test_files:
128
+ - test/anti-captcha_test.rb