<commit_before>require 'logger'
require 'digest/sha1'
require 'rest_client'

module DeathByCaptcha
  
  #
  # DeathByCaptcha API Client
  #
  class Client
    
    attr_accessor :config
    
    def initialize(username, password, extra={})
      data = {
        :is_verbose => false, # If true, prints messages during execution
        :logger_output => STDOUT, # Logger output path or IO instance
        :api_version => API_VERSION, # API version (used as user-agent with http requests)
        :software_vendor_id => 0, # API unique software ID
        :max_captcha_file_size => 64 * 1024, # Maximum CAPTCHA image filesize, currently 64K
        :default_timeout => 60, # Default CAPTCHA timeout
        :polls_interval => 5, # Default decode polling interval
        :http_base_url => 'http://api.deathbycaptcha.com/api', # Base HTTP API url
        :http_response_type => 'application/json', # Preferred HTTP API server's response content type, do not change
        :socket_host => 'api.deathbycaptcha.com', # Socket API server's host
        :socket_port => (8123..8130).map {|p| p}, # Socket API server's ports range
        :username => username, # DeathByCaptcha username
        :password_hashed => <PASSWORD>::SHA1.hexdigest(password), # DeathByCaptcha user's password encrypted
        :password => '', # DeathByCaptcha user's password not encrypted
        :password_is_hashed => true # True, if the password is hashed
      }.merge(extra)
      
      @config = DeathByCaptcha::Config.new(data) # Config instance
      @logger = Logger.new(@config.logger_output) # Logger
      
    end
    
    #
    # Fetch the user's details -- balance, rate and banned status
    #
    def get_user
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Fetch the user's balance (in US cents)
    #
    def get_balance
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Fetch a CAPTCHA details -- its numeric ID, text and correctness
    #
    def get_captcha(cid)
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Fetch a CAPTCHA text
    #
    def get_text(cid)
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Report a CAPTCHA as incorrectly solved
    #
    def report(cid)
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Remove an unsolved CAPTCHA
    #
    def remove(cid)
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Upload a CAPTCHA
    #
    # Accepts file names, file objects or urls, and an optional flag telling
    # whether the CAPTCHA is case-sensitive or not.  Returns CAPTCHA details
    # on success.
    #
    def upload(captcha, options={})
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Try to solve a CAPTCHA.
    #
    # See Client.upload() for arguments details.
    #
    # Uploads a CAPTCHA, polls for its status periodically with arbitrary
    # timeout (in seconds).  Removes unsolved CAPTCHAs.  Returns CAPTCHA
    # details if (correctly) solved.
    #
    def decode(captcha, timeout=@config.default_timeout, is_case_sensitive=false)
      deadline = Time.now + timeout
      c = upload(captcha, is_case_sensitive)
      if c
        while deadline > Time.now and not c['text']
          sleep(config.polls_interval)
          c = get_captcha(c['captcha'])
        end
        
        if c['text']
          if c['is_correct']
            return c
          end
        else
          remove(c['captcha'])
        end
        
      end
      
      
    end
    
    protected
    #
    # Return a hash with the user's credentials
    #<commit_msg>Fixed client#decode method<commit_after>require 'logger'
require 'digest/sha1'
require 'rest_client'

module DeathByCaptcha
  
  #
  # DeathByCaptcha API Client
  #
  class Client
    
    attr_accessor :config
    
    def initialize(username, password, extra={})
      data = {
        :is_verbose => false, # If true, prints messages during execution
        :logger_output => STDOUT, # Logger output path or IO instance
        :api_version => API_VERSION, # API version (used as user-agent with http requests)
        :software_vendor_id => 0, # API unique software ID
        :max_captcha_file_size => 64 * 1024, # Maximum CAPTCHA image filesize, currently 64K
        :default_timeout => 60, # Default CAPTCHA timeout
        :polls_interval => 5, # Default decode polling interval
        :http_base_url => 'http://api.deathbycaptcha.com/api', # Base HTTP API url
        :http_response_type => 'application/json', # Preferred HTTP API server's response content type, do not change
        :socket_host => 'api.deathbycaptcha.com', # Socket API server's host
        :socket_port => (8123..8130).map {|p| p}, # Socket API server's ports range
        :username => username, # DeathByCaptcha username
        :password_hashed => <PASSWORD>(password), # DeathByCaptcha user's password encrypted
        :password => '', # DeathByCaptcha user's password not encrypted
        :password_is_hashed => true # True, if the password is hashed
      }.merge(extra)
      
      @config = DeathByCaptcha::Config.new(data) # Config instance
      @logger = Logger.new(@config.logger_output) # Logger
      
    end
    
    #
    # Fetch the user's details -- balance, rate and banned status
    #
    def get_user
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Fetch the user's balance (in US cents)
    #
    def get_balance
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Fetch a CAPTCHA details -- its numeric ID, text and correctness
    #
    def get_captcha(cid)
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Fetch a CAPTCHA text
    #
    def get_text(cid)
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Report a CAPTCHA as incorrectly solved
    #
    def report(cid)
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Remove an unsolved CAPTCHA
    #
    def remove(cid)
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Upload a CAPTCHA
    #
    # Accepts file names, file objects or urls, and an optional flag telling
    # whether the CAPTCHA is case-sensitive or not.  Returns CAPTCHA details
    # on success.
    #
    def upload(captcha, options={})
      raise DeathByCaptcha::Errors::NotImplemented
    end
    
    #
    # Try to solve a CAPTCHA.
    #
    # See Client.upload() for arguments details.
    #
    # Uploads a CAPTCHA, polls for its status periodically with arbitrary
    # timeout (in seconds).  Removes unsolved CAPTCHAs.  Returns CAPTCHA
    # details if (correctly) solved.
    #
    def decode(captcha, options={})
      options = {:timeout => config.default_timeout, :is_case_sensitive => false, :is_raw_content => false}.merge(options)
      deadline = Time.now + options[:timeout]
      c = upload(captcha, options)
      if c
        while deadline > Time.now and not c['text']
          sleep(config.polls_interval)
          c = get_captcha(c['captcha'])
        end
        
        if c['text']
          if c['is_correct']
            return c
          end
        else
          remove(c['captcha'])
        end
        
      end
      
      
    end
    
    protected
    #
    # Return a hash with the user's credentials
    #
{'fraction_non_alphanumeric': 0.08748988945807495, 'fraction_numerical': 0.005796710703693718, 'mean_word_length': 2.373806275579809, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6787421', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2174, 'n_words': 638}