<commit_before>require 'mechanize'
module AmazonSellerCentral
  class Mechanizer
    MASQUERADE_AGENTS = ['Mac Safari', 'Mac FireFox', 'Linux Firefox', 'Windows IE 9']

    attr_reader :agent

    def initialize
      @logged_in = false
    end

    def login_email
      AmazonSellerCentral.configuration.login_email
    end

    def login_password
      <PASSWORD>ellerCentral.configuration.login_password
    end

    def agent
      @agent ||= Mechanize.new {|ag| ag.user_agent_alias = MASQUERADE_AGENTS[rand(MASQUERADE_AGENTS.size)] }
    end

    def last_page
      agent.current_page
    end

    def login_to_seller_central
      return true if @logged_in

      page = agent.get('https://sellercentral.amazon.com/gp/homepage.html')
      form = page.form_with(:name => 'signinWidget')

      begin
        form['username']    = login_email
        form['password'] = <PASSWORD>
        p = form.submit
        if p =~ /better protect/ # capcha!
          raise CapchaPresentError.new("Holy CAPCHA Batman!")
        end
        @logged_in = !!( p.body =~ /Logout/ && p.body =~ /Manage Inventory/ )

      rescue StandardError => e
        File.open("/tmp/seller_central_#{Time.now.to_i}.html","wb") do |f|
          f.write page.body
        end
        raise e
      end
    end

    def follow_link_with(options)
      raise AgentResetError unless last_page
      link = last_page.link_with(options)
      raise LinkNotFoundError unless link
      agent.click(link)
    end

    def reset!
      @agent     = nil
      @logged_in = false
      #@last_page = nil
    end
<commit_msg>Dealing with the new device issue after logging in<commit_after>require 'mechanize'
module AmazonSellerCentral
  class Mechanizer
    MASQUERADE_AGENTS = ['Mac Safari', 'Mac FireFox', 'Linux Firefox', 'Windows IE 9']

    attr_reader :agent

    def initialize
      @logged_in = false
    end

    def login_email
      AmazonSellerCentral.configuration.login_email
    end

    def login_password
      <PASSWORD>.login_password
    end

    def agent
      @agent ||= Mechanize.new {|ag| ag.user_agent_alias = MASQUERADE_AGENTS[rand(MASQUERADE_AGENTS.size)] }
    end

    def last_page
      agent.current_page
    end

    def login_to_seller_central
      return true if @logged_in
      tries = 3
      begin
        tries -= 1
        page = agent.get('https://sellercentral.amazon.com/gp/homepage.html')
        form = page.form_with(:name => 'signinWidget')

        begin
          form['username']    = login_email
          form['password']    = <PASSWORD>
          p = form.submit

          if p =~ /better protect/ # capcha!
            raise CapchaPresentError.new("Holy CAPCHA Batman!")
          end
          @logged_in = !!( p.body =~ /Logout/ && p.body =~ /Manage Inventory/ )

        rescue StandardError => e
          File.open("/tmp/seller_central_#{Time.now.to_i}.html","wb") do |f|
            f.write page.body
          end
          raise e
        end

        # New device verification
        if p.body =~ /What is the ZIP Code/
          form = p.form_with(:name => 'ap_dcq_form')
          form.dcq_question_subjective_1 = '20706'
          p = form.submit # This raises a response code error, :-(
        end

      rescue Mechanize::ResponseCodeError
        retry if tries > 0
        raise
      end

    end

    def follow_link_with(options)
      raise AgentResetError unless last_page
      link = last_page.link_with(options)
      raise LinkNotFoundError unless link
      agent.click(link)
    end

    def reset!
      @agent     = nil
      @logged_in = false
      #@last_page = nil
    end

{'fraction_non_alphanumeric': 0.09213421342134213, 'fraction_numerical': 0.003025302530253025, 'mean_word_length': 2.195957820738137, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '917782', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1114, 'n_words': 271}