From 454a46d50cf128efe76e0f4ec7a8eb35c0ada361 Mon Sep 17 00:00:00 2001 From: Ivan Aryutkin Date: Tue, 13 Dec 2011 00:30:19 +0800 Subject: [PATCH] Added gem source and readme --- .gitignore | 4 ++ Gemfile | 4 ++ README.markdown | 35 ++++++++++++++++ Rakefile | 1 + antigate.gemspec | 24 +++++++++++ lib/antigate.rb | 92 +++++++++++++++++++++++++++++++++++++++++ lib/antigate/version.rb | 3 ++ 7 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 README.markdown create mode 100644 Rakefile create mode 100644 antigate.gemspec create mode 100644 lib/antigate.rb create mode 100644 lib/antigate/version.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4040c6c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.gem +.bundle +Gemfile.lock +pkg/* diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..bf84e8d --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "http://rubygems.org" + +# Specify your gem's dependencies in antigate.gemspec +gemspec diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..5ccb1e9 --- /dev/null +++ b/README.markdown @@ -0,0 +1,35 @@ +# Wrapper for [Antigate][1] API +Gem recognizes CAPTCHA by means Antigate. + +[Registration Antigate account][2] + +## Install + gem install antigate + +## Usage +### Recognize captcha + captcha = Antigate.wrapper(KEY) + captcha.phrase = 0 or 1 (0 default, 1 marks that at captcha 2-4 words) + captcha.regsense = 0 or 1 (0 default, 1 marks that text captcha is case sensitive) + captcha.numeric = 0 or 1 or 2 (0 default, 1 marks that text captcha consists only of numbers, 2 marks that on captcha no digit) + captcha.calc = 0 or 1 (0 default, 1 marks that digit on captcha should be folded) + captcha.min_len = 0..20 (0 default, minimum length text captcha) + captcha.max_len = 0..20 (0 - unlimited, maximum length text captcha) + recognized = captcha.recognize(URL, EXT) + puts recognized[0] # ID recognized CAPTCHA + puts recognized[1] # Text CAPTCHA + +#### Example + captcha = Antigate.wrapper('660aaf58948bae3fa81362ef71b9ebcc') + captcha.phrase = 1 + recognized = captcha.recognize('http://www.google.com/recaptcha/api/image?c=03AHJ_Vuu-Kun_wMo4M8JiWA87K6awfoiUxJCUF9KkQq3tCfyxjYELhHcsIJrcJ_qgqIQQsBw5vWAkpHBqP4VEHv1nwtoAnD5uZvwzHknOFyID4OrX0_6q8QXQ5TwkRn7qBxdt3QdX6D8NvPcFHFHzmEhu1yCJJQfTwQ', 'jpg') + puts recognized[1] + +### Get balance + puts Antigate.balance(KEY) + +#### Example + puts Antigate.balance('660aaf58948bae3fa81362ef71b9ebcc') + +[1]: http://antigate.com/ +[2]: http://antigate.com/index.php?action=regscreen \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2995527 --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require "bundler/gem_tasks" diff --git a/antigate.gemspec b/antigate.gemspec new file mode 100644 index 0000000..d959bb1 --- /dev/null +++ b/antigate.gemspec @@ -0,0 +1,24 @@ +# -*- encoding: utf-8 -*- +$:.push File.expand_path("../lib", __FILE__) +require "antigate/version" + +Gem::Specification.new do |s| + s.name = "antigate" + s.version = Antigate::VERSION + s.authors = ["Ivan Aryutkin"] + s.email = ["iivanare@gmail.com"] + s.homepage = "https://github.com/ivanare/antigate" + s.summary = %q{Antigate wrapper} + s.description = %q{Wrapper for Antigate API} + + s.rubyforge_project = "antigate" + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] + + # specify any dependencies here; for example: + # s.add_development_dependency "rspec" + # s.add_runtime_dependency "rest-client" +end diff --git a/lib/antigate.rb b/lib/antigate.rb new file mode 100644 index 0000000..bcea0dc --- /dev/null +++ b/lib/antigate.rb @@ -0,0 +1,92 @@ +require "antigate/version" + +module Antigate + require 'net/http' + require 'uri' + require 'base64' + + def self.wrapper(key) + return Wrapper.new(key) + end + + def self.balance(key) + wrapper = Wrapper.new(key) + return wrapper.balance + end + + class Wrapper + attr_accessor :phrase, :regsense, :numeric, :calc, :min_len, :max_len + + def initialize(key) + @key = key + + @phrase = 0 + @regsense = 0 + @numeric = 0 + @calc = 0 + @min_len = 0 + @max_len = 0 + end + + def recognize(url, ext) + added = nil + loop do + added = add(url, ext) + if added.include? 'ERROR_NO_SLOT_AVAILABLE' + sleep(1) + next + else + break + end + end + if added.include? 'OK' + id = added.split('|')[1] + sleep(10) + status = nil + loop do + status = status(id) + if status.include? 'CAPCHA_NOT_READY' + sleep(1) + next + else + break + end + end + return [id, status.split('|')[1]] + else + return added + end + end + + def add(url, ext) + captcha = Net::HTTP.get(URI(url)) rescue nil + if captcha + params = { + 'method' => 'base64', + 'key' => @key, + 'body' => Base64.encode64(captcha), + 'ext' => ext, + 'phrase' => @phrase, + 'regsense' => @regsense, + 'numeric' => @numeric, + 'calc' => @calc, + 'min_len' => @min_len, + 'max_len' => @max_len + } + return Net::HTTP.post_form(URI('http://antigate.com/in.php'), params).body rescue nil + end + end + + def status(id) + return Net::HTTP.get(URI("http://antigate.com/res.php?key=#{@key}&action=get&id=#{id}")) rescue nil + end + + def bad(id) + return Net::HTTP.get(URI("http://antigate.com/res.php?key=#{@key}&action=reportbad&id=#{id}")) rescue nil + end + + def balance + return Net::HTTP.get(URI("http://antigate.com/res.php?key=#{@key}&action=getbalance")) rescue nil + end + end +end diff --git a/lib/antigate/version.rb b/lib/antigate/version.rb new file mode 100644 index 0000000..4c821ba --- /dev/null +++ b/lib/antigate/version.rb @@ -0,0 +1,3 @@ +module Antigate + VERSION = "0.0.1" +end