<commit_before>require 'mechanize'
require 'mechanize/progressbar'
require 'security'

module XcodeInstaller
  class Agent < ::Mechanize
    attr_accessor :username, :password, :verbose, :dry_run

    def initialize
      super
      self.user_agent_alias = 'Mac Safari'

      pw = Security::InternetPassword.find(:server => XcodeInstaller::AppleDeveloperCenter::HOST)
      @username, @password = pw.attributes['acct'], pw.password if pw
    end

    def download(xcode_url)
      adc_login_url = XcodeInstaller::AppleDeveloperCenter::LOGIN_URL
      downloads_url = XcodeInstaller::AppleDeveloperCenter::DOWNLOADS_URL

      begin
        # Request login response
        puts "\n>>> Login response >>>" if @verbose
        response = get(adc_login_url)
        if @verbose
          puts "status code: #{response.code}\n"
          pp response
          puts cookie_jar.jar
        end

        # Submit login form
        puts "\n>>> Submit Login Form >>>" if @verbose
        form = response.form_with(:name => 'appleConnectForm')
        form.theAccountName = username
        form.theAccountPW = password
        response = form.submit
        if @verbose
          puts "status code: #{response.code}\n"
          pp response
          puts cookie_jar.jar
        end

        # Request downloads response
        puts "\n>>> Downloads >>>" if @verbose
        response = get(downloads_url)
        if @verbose
          puts "status code: #{response.code}\n"
          pp response
          puts cookie_jar.jar
        end
        # Shouldn't get the login form if login was successful
        form = response.form_with(:name => 'appleConnectForm')
        raise UnsuccessfulAuthenticationError if form

        # Download
        puts "\n>>> Xcode >>>" if @verbose

        if @dry_run
          # HEAD request for testing
          response = head(xcode_url)
          if @verbose
            puts "status code: #{response.code}\n"
            pp response
          else
            puts "filename: #{response.filename}"
            puts "size: #{response.header['content-length']}"
            puts "last-modified: #{response.header['last-modified']}"
            return response.filename
          end
        else
          # GET request for actual download
          pluggable_parser.default = Mechanize::Download
          self.progressbar{ file = get(xcode_url) }
          file.save
          return file.filename
        end

      rescue Mechanize::ResponseCodeError => exception
        if exception.response_code == '403'
          response = exception.page
          puts "status code: #{response.code}\n"
          pp response
          puts cookie_jar.jar
        else<commit_msg>Disable progressbar, the patch isn't working<commit_after>require 'mechanize'
# require 'mechanize/progressbar'
require 'security'

module XcodeInstaller
  class Agent < ::Mechanize
    attr_accessor :username, :password, :verbose, :dry_run

    def initialize
      super
      self.user_agent_alias = 'Mac Safari'

      pw = Security::InternetPassword.find(:server => XcodeInstaller::AppleDeveloperCenter::HOST)
      @username, @password = pw.attributes['acct'], pw.password if pw
    end

    def download(xcode_url)
      adc_login_url = XcodeInstaller::AppleDeveloperCenter::LOGIN_URL
      downloads_url = XcodeInstaller::AppleDeveloperCenter::DOWNLOADS_URL

      begin
        # Request login response
        puts "\n>>> Login response >>>" if @verbose
        response = get(adc_login_url)
        if @verbose
          puts "status code: #{response.code}\n"
          pp response
          puts cookie_jar.jar
        end

        # Submit login form
        puts "\n>>> Submit Login Form >>>" if @verbose
        form = response.form_with(:name => 'appleConnectForm')
        form.theAccountName = username
        form.theAccountPW = password
        response = form.submit
        if @verbose
          puts "status code: #{response.code}\n"
          pp response
          puts cookie_jar.jar
        end

        # Request downloads response
        puts "\n>>> Downloads >>>" if @verbose
        response = get(downloads_url)
        if @verbose
          puts "status code: #{response.code}\n"
          pp response
          puts cookie_jar.jar
        end
        # Shouldn't get the login form if login was successful
        form = response.form_with(:name => 'appleConnectForm')
        raise UnsuccessfulAuthenticationError if form

        # Download
        puts "\n>>> Xcode >>>" if @verbose

        if @dry_run
          # HEAD request for testing
          response = head(xcode_url)
          if @verbose
            puts "status code: #{response.code}\n"
            pp response
          else
            puts "filename: #{response.filename}"
            puts "size: #{response.header['content-length']}"
            puts "last-modified: #{response.header['last-modified']}"
            return response.filename
          end
        else
          # GET request for actual download
          pluggable_parser.default = Mechanize::Download
          # self.progressbar{ file = get(xcode_url) }
          file = get(xcode_url)
          file.save
          return file.filename
        end

      rescue Mechanize::ResponseCodeError => exception
        if exception.response_code == '403'
          response = exception.page
          puts "status code: #{response.code}\n"
          pp response
          puts cookie_jar.jar
        else
{'fraction_non_alphanumeric': 0.09107501368862932, 'fraction_numerical': 0.0010950903449534588, 'mean_word_length': 2.108338060124787, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7561267', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1449, 'n_words': 396}