antigate_api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/antigate_api.gemspec +20 -0
- data/lib/antigate_api/client.rb +100 -0
- data/lib/antigate_api/errors.rb +73 -0
- data/lib/antigate_api/version.rb +3 -0
- data/lib/antigate_api.rb +7 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Tam Vo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# AntigateApi
|
2
|
+
|
3
|
+
Antigate (Decode captcha service - antigate.com) wrapper
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'antigate_api'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install antigate_api
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```
|
22
|
+
options = {
|
23
|
+
recognition_time: 5, # First waiting time
|
24
|
+
sleep_time: 1, # Sleep time for every check interval
|
25
|
+
timeout: 60, # Max time out for decoding captcha
|
26
|
+
debug: false # Verborse or not
|
27
|
+
}
|
28
|
+
client = AntigateApi::Client.new(ANTIGATE_KEY, options)
|
29
|
+
captcha_id, captcha_answer = client.decode("captcha.gif")
|
30
|
+
puts captcha_id + " " + captcha_answer
|
31
|
+
```
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'antigate_api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "antigate_api"
|
8
|
+
gem.version = AntigateApi::VERSION
|
9
|
+
gem.authors = ["Tam Vo"]
|
10
|
+
gem.email = ["vo.mita.ov@gmail.com"]
|
11
|
+
gem.description = %q{Antigate (Decode captcha service) wrapper for Ruby}
|
12
|
+
gem.summary = %q{Antigate (Decode captcha service) wrapper for Ruby}
|
13
|
+
gem.homepage = "http://github.com/tamvo"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/http/post/multipart'
|
3
|
+
|
4
|
+
module AntigateApi
|
5
|
+
class Client
|
6
|
+
attr_reader :key
|
7
|
+
attr_accessor :options
|
8
|
+
|
9
|
+
DEFAULT_CONFIGS = {
|
10
|
+
recognition_time: 5, # First waiting time
|
11
|
+
sleep_time: 1, # Sleep time for every check interval
|
12
|
+
timeout: 60, # Max time out for decoding captcha
|
13
|
+
debug: false # Verborse or not
|
14
|
+
}
|
15
|
+
|
16
|
+
def initialize(key, opts={})
|
17
|
+
@key = key
|
18
|
+
@options = DEFAULT_CONFIGS.merge(opts)
|
19
|
+
end
|
20
|
+
|
21
|
+
def send_captcha( captcha_file )
|
22
|
+
uri = URI.parse( 'http://antigate.com/in.php' )
|
23
|
+
file = File.new( captcha_file, 'rb' )
|
24
|
+
req = Net::HTTP::Post::Multipart.new( uri.path,
|
25
|
+
:method => 'post',
|
26
|
+
:key => @key,
|
27
|
+
:file => UploadIO.new( file, 'image/jpeg', 'image.jpg' ),
|
28
|
+
:numeric => 1 )
|
29
|
+
http = Net::HTTP.new( uri.host, uri.port )
|
30
|
+
begin
|
31
|
+
resp = http.request( req )
|
32
|
+
rescue => err
|
33
|
+
puts err
|
34
|
+
return nil
|
35
|
+
end
|
36
|
+
|
37
|
+
id = resp.body
|
38
|
+
id[ 3..id.size ]
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_captcha_text( id )
|
42
|
+
data = { :key => @key,
|
43
|
+
:action => 'get',
|
44
|
+
:id => id,
|
45
|
+
:min_len => 5,
|
46
|
+
:max_len => 5 }
|
47
|
+
uri = URI.parse('http://antigate.com/res.php' )
|
48
|
+
req = Net::HTTP::Post.new( uri.path )
|
49
|
+
http = Net::HTTP.new( uri.host, uri.port )
|
50
|
+
req.set_form_data( data )
|
51
|
+
|
52
|
+
begin
|
53
|
+
resp = http.request(req)
|
54
|
+
rescue => err
|
55
|
+
puts err
|
56
|
+
return nil
|
57
|
+
end
|
58
|
+
|
59
|
+
text = resp.body
|
60
|
+
if text != "CAPCHA_NOT_READY"
|
61
|
+
return text[ 3..text.size ]
|
62
|
+
end
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
|
66
|
+
def report_bad( id )
|
67
|
+
data = { :key => @key,
|
68
|
+
:action => 'reportbad',
|
69
|
+
:id => id }
|
70
|
+
uri = URI.parse('http://antigate.com/res.php' )
|
71
|
+
req = Net::HTTP::Post.new( uri.path )
|
72
|
+
http = Net::HTTP.new( uri.host, uri.port )
|
73
|
+
req.set_form_data( data )
|
74
|
+
|
75
|
+
begin
|
76
|
+
resp = http.request(req)
|
77
|
+
rescue => err
|
78
|
+
puts err
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def decode(captcha_file)
|
83
|
+
captcha_id = self.send_captcha(captcha_file)
|
84
|
+
start_time = Time.now.to_i
|
85
|
+
sleep @options[:recognition_time]
|
86
|
+
|
87
|
+
code = nil
|
88
|
+
while code == nil do
|
89
|
+
code = self.get_captcha_text( captcha_id )
|
90
|
+
duration = Time.now.to_i - start_time
|
91
|
+
puts "Spent time: #{duration}" if @options[:debug]
|
92
|
+
sleep @options[:sleep_time]
|
93
|
+
raise AntigateApi::Errors::TimeoutError.new if duration > @options[:timeout]
|
94
|
+
end
|
95
|
+
|
96
|
+
[captcha_id, code]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module AntigateApi
|
2
|
+
module Errors
|
3
|
+
|
4
|
+
#
|
5
|
+
# Custom Error class for rescuing from AntigateApi API errors.
|
6
|
+
#
|
7
|
+
class Error < StandardError
|
8
|
+
|
9
|
+
def initialize(message)
|
10
|
+
super("#{message} (ANTIGATE API ERROR)")
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class TimeoutError < Error
|
16
|
+
def initialize
|
17
|
+
super('Timeout!!')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Raised when a method tries to access a not implemented method.
|
23
|
+
#
|
24
|
+
class NotImplemented < Error
|
25
|
+
def initialize
|
26
|
+
super('The requested functionality was not implemented')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Raised when a HTTP call fails.
|
32
|
+
#
|
33
|
+
class CallError < Error
|
34
|
+
def initialize
|
35
|
+
super('HTTP call failed')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Raised when the user is not allowed to access the API.
|
41
|
+
#
|
42
|
+
class AccessDenied < Error
|
43
|
+
def initialize
|
44
|
+
super('Access denied, please check your credentials and/or balance')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
# Raised when the captcha file could not be loaded or is empty.
|
50
|
+
#
|
51
|
+
class CaptchaEmpty
|
52
|
+
def initialize
|
53
|
+
super('CAPTCHA image is empty or could not be loaded')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# Raised when the size of the captcha file is too big.
|
59
|
+
#
|
60
|
+
class CaptchaOverflow
|
61
|
+
def initialize
|
62
|
+
super('CAPTCHA image is too big')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class ServiceOverload
|
67
|
+
def initialize
|
68
|
+
super('CAPTCHA was rejected due to service overload, try again later')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
data/lib/antigate_api.rb
ADDED
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: antigate_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tam Vo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-05 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Antigate (Decode captcha service) wrapper for Ruby
|
15
|
+
email:
|
16
|
+
- vo.mita.ov@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- antigate_api.gemspec
|
27
|
+
- lib/antigate_api.rb
|
28
|
+
- lib/antigate_api/client.rb
|
29
|
+
- lib/antigate_api/errors.rb
|
30
|
+
- lib/antigate_api/version.rb
|
31
|
+
homepage: http://github.com/tamvo
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.24
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Antigate (Decode captcha service) wrapper for Ruby
|
55
|
+
test_files: []
|
56
|
+
has_rdoc:
|