Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module NoMoreCaptcha
- require 'base64'
- require 'uri'
- require 'httparty'
- # Disable SSL verification
- HTTParty::Basement.default_options.update(verify: false)
- class Captcha
- attr_accessor :service, :api_key
- attr_reader :balance
- AntiCaptchaService = 'http://anti-captcha.com'
- AntiGateService = 'http://antigate.com'
- RIPCaptchaService = 'http://ripcaptcha.com/'
- RUCaptchaService = 'https://rucaptcha.com'
- def initialize service, api_key
- @service = service
- @api_key = api_key
- @balance = 0
- update_balance()
- end
- # Updates @balance
- def update_balance
- response = HTTParty.get build_url "#{@service}/res.php", {:action => 'getbalance', :key => @api_key}
- unless response.code == 200
- puts "HTTP error: #{response.code}"
- return false
- end
- case response.body
- when "ERROR_WRONG_USER_KEY", "ERROR_KEY_DOES_NOT_EXIST"
- abort "Wrong API key"
- else
- @balance = response.body
- end
- end
- # Returns text from passed image URL
- def recognize image_url, parameters = nil
- if parameters
- raise ArgumentError, "parameters should be hash" unless parameters.kind_of?(Hash)
- end
- return false unless image = get_image(image_url)
- return false unless captcha_id = upload_image(image, parameters)
- return false unless captcha_text = check_captcha(captcha_id)
- captcha_text
- end
- private
- def build_url url, parameters
- raise ArgumentError, "parameters should be hash" unless parameters.kind_of?(Hash)
- parameters.each_with_index do |(parameter, value), index|
- prefix = index == 0 ? '?' : '&'
- url += "#{prefix}#{parameter}=#{URI.encode(value.to_s)}"
- end
- url
- end
- # Returns base64 encrypted image
- def get_image image_url
- begin
- response = HTTParty.get image_url
- rescue
- puts "Can't get an image from URL: \"#{image_url}\""
- return false
- end
- Base64.encode64 response.body
- end
- # Uploads an image to captcha recognition service
- def upload_image image_base64, parameters = nil
- if parameters
- raise ArgumentError, "parameters should be hash" unless parameters.kind_of?(Hash)
- end
- begin
- body = {:method => 'base64', :key => @api_key, :body => image_base64}
- if parameters
- body.merge!(parameters)
- end
- response = HTTParty.post "#{@service}/in.php", :body => body
- rescue
- puts "Can't upload image"
- return false
- end
- return false unless response = handle_response(response.body)
- response
- end
- # Check if captcha is ready
- def check_captcha captcha_id
- 5.times do
- sleep 5
- response = HTTParty.get build_url "#{@service}/res.php", {:action => 'get', :id => captcha_id, :key => @api_key}
- return false unless captcha_text = handle_response(response.body)
- if captcha_text != true
- return captcha_text
- end
- end
- false
- end
- def handle_response response
- case response
- when "ERROR_WRONG_USER_KEY"
- puts "Wrong API key format"
- return false
- when "ERROR_KEY_DOES_NOT_EXIST"
- puts "API key does not exist"
- return false
- when "ERROR_ZERO_BALANCE"
- puts "Zero balance"
- return false
- when "ERROR_NO_SLOT_AVAILABLE"
- puts "No available slots"
- return false
- when "ERROR_ZERO_CAPTCHA_FILESIZE"
- puts "Your captcha size is less than 100kb"
- return false
- when "ERROR_WRONG_FILE_EXTENSION"
- puts "Wrong captcha file extension"
- return false
- when "ERROR_IMAGE_TYPE_NOT_SUPPORTED"
- puts "Captcha image type not supported"
- return false
- when "ERROR_IP_NOT_ALLOWED"
- pust "Requests from your IP are not allowed"
- return false
- when "CAPCHA_NOT_READY"
- puts "Captcha is not ready"
- return true
- when "ERROR_WRONG_ID_FORMAT"
- puts "Wrong captcha ID format"
- return false
- when "ERROR_CAPTCHA_UNSOLVABLE"
- puts "Captcha is unsolvable"
- return false
- end
- if response.start_with? 'OK|'
- return response[3..-1]
- end
- false
- end
- end
- end
- include NoMoreCaptcha
- captcha = Captcha.new Captcha::RUCaptchaService, '5034b431e072a5472d5b33d88bacaa65'
- puts "#{captcha.balance} rubles"
- # http://i.imgur.com/Ni4NYbo.png
- text = captcha.recognize 'http://somepony.org/getacutiemark/captcha.php'
- if text
- puts "Our captcha is #{text}"
- end
Advertisement
Add Comment
Please, Sign In to add comment