Guest User

Untitled

a guest
Mar 4th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.73 KB | None | 0 0
  1. module NoMoreCaptcha
  2.   require 'base64'
  3.   require 'uri'
  4.   require 'httparty'
  5.  
  6.   # Disable SSL verification
  7.   HTTParty::Basement.default_options.update(verify: false)
  8.  
  9.   class Captcha
  10.     attr_accessor :service, :api_key
  11.     attr_reader :balance
  12.  
  13.     AntiCaptchaService  = 'http://anti-captcha.com'
  14.     AntiGateService     = 'http://antigate.com'
  15.     RIPCaptchaService   = 'http://ripcaptcha.com/'
  16.     RUCaptchaService    = 'https://rucaptcha.com'
  17.  
  18.     def initialize service, api_key
  19.       @service = service
  20.       @api_key = api_key
  21.       @balance = 0
  22.       update_balance()
  23.     end
  24.  
  25.     # Updates @balance
  26.     def update_balance
  27.       response = HTTParty.get build_url "#{@service}/res.php", {:action => 'getbalance', :key => @api_key}
  28.  
  29.       unless response.code == 200
  30.         puts "HTTP error: #{response.code}"
  31.         return false
  32.       end
  33.  
  34.       case response.body
  35.       when "ERROR_WRONG_USER_KEY", "ERROR_KEY_DOES_NOT_EXIST"
  36.         abort "Wrong API key"
  37.       else
  38.         @balance = response.body
  39.       end
  40.     end
  41.  
  42.     # Returns text from passed image URL
  43.     def recognize image_url, parameters = nil
  44.       if parameters
  45.         raise ArgumentError, "parameters should be hash" unless parameters.kind_of?(Hash)
  46.       end
  47.  
  48.       return false unless image = get_image(image_url)
  49.  
  50.       return false unless captcha_id = upload_image(image, parameters)
  51.  
  52.       return false unless captcha_text = check_captcha(captcha_id)
  53.  
  54.       captcha_text
  55.     end
  56.  
  57.     private
  58.  
  59.     def build_url url, parameters
  60.       raise ArgumentError, "parameters should be hash" unless parameters.kind_of?(Hash)
  61.  
  62.       parameters.each_with_index do |(parameter, value), index|
  63.         prefix = index == 0 ? '?' : '&'
  64.         url += "#{prefix}#{parameter}=#{URI.encode(value.to_s)}"
  65.       end
  66.  
  67.       url
  68.     end
  69.  
  70.     # Returns base64 encrypted image
  71.     def get_image image_url
  72.       begin
  73.         response = HTTParty.get image_url
  74.       rescue
  75.         puts "Can't get an image from URL: \"#{image_url}\""
  76.         return false
  77.       end
  78.  
  79.       Base64.encode64 response.body
  80.     end
  81.  
  82.     # Uploads an image to captcha recognition service
  83.     def upload_image image_base64, parameters = nil
  84.       if parameters
  85.         raise ArgumentError, "parameters should be hash" unless parameters.kind_of?(Hash)
  86.       end
  87.  
  88.       begin
  89.         body = {:method => 'base64', :key => @api_key, :body => image_base64}
  90.  
  91.         if parameters
  92.           body.merge!(parameters)
  93.         end
  94.  
  95.         response = HTTParty.post "#{@service}/in.php", :body => body
  96.       rescue
  97.         puts "Can't upload image"
  98.         return false
  99.       end
  100.  
  101.       return false unless response = handle_response(response.body)
  102.  
  103.       response
  104.     end
  105.  
  106.     # Check if captcha is ready
  107.     def check_captcha captcha_id
  108.       5.times do
  109.         sleep 5
  110.  
  111.         response = HTTParty.get build_url "#{@service}/res.php", {:action => 'get', :id => captcha_id, :key => @api_key}
  112.  
  113.         return false unless captcha_text = handle_response(response.body)
  114.  
  115.         if captcha_text != true
  116.           return captcha_text
  117.         end
  118.       end
  119.  
  120.       false
  121.     end
  122.  
  123.     def handle_response response
  124.       case response
  125.       when "ERROR_WRONG_USER_KEY"
  126.         puts "Wrong API key format"
  127.         return false
  128.  
  129.       when "ERROR_KEY_DOES_NOT_EXIST"
  130.         puts "API key does not exist"
  131.         return false
  132.  
  133.       when "ERROR_ZERO_BALANCE"
  134.         puts "Zero balance"
  135.         return false
  136.  
  137.       when "ERROR_NO_SLOT_AVAILABLE"
  138.         puts "No available slots"
  139.         return false
  140.  
  141.       when "ERROR_ZERO_CAPTCHA_FILESIZE"
  142.         puts "Your captcha size is less than 100kb"
  143.         return false
  144.  
  145.       when "ERROR_WRONG_FILE_EXTENSION"
  146.         puts "Wrong captcha file extension"
  147.         return false
  148.  
  149.       when "ERROR_IMAGE_TYPE_NOT_SUPPORTED"
  150.         puts "Captcha image type not supported"
  151.         return false
  152.  
  153.       when "ERROR_IP_NOT_ALLOWED"
  154.         pust "Requests from your IP are not allowed"
  155.         return false
  156.  
  157.       when "CAPCHA_NOT_READY"
  158.         puts "Captcha is not ready"
  159.         return true
  160.  
  161.       when "ERROR_WRONG_ID_FORMAT"
  162.         puts "Wrong captcha ID format"
  163.         return false
  164.  
  165.       when "ERROR_CAPTCHA_UNSOLVABLE"
  166.         puts "Captcha is unsolvable"
  167.         return false
  168.       end
  169.  
  170.       if response.start_with? 'OK|'
  171.         return response[3..-1]
  172.       end
  173.  
  174.       false
  175.     end
  176.  
  177.   end
  178. end
  179.  
  180. include NoMoreCaptcha
  181.  
  182. captcha = Captcha.new Captcha::RUCaptchaService, '5034b431e072a5472d5b33d88bacaa65'
  183.  
  184. puts "#{captcha.balance} rubles"
  185.  
  186. # http://i.imgur.com/Ni4NYbo.png
  187. text = captcha.recognize 'http://somepony.org/getacutiemark/captcha.php'
  188.  
  189. if text
  190.   puts "Our captcha is #{text}"
  191. end
Advertisement
Add Comment
Please, Sign In to add comment