#!/usr/bin/env ruby
# encoding: UTF-8

require 'net/http' 
require 'uri'
require 'open-uri'
require 'fileutils'
require 'nokogiri'
require 'selenium/webdriver'
require 'watir-webdriver'

module HoneyCoin
  class HoneyCoinHarvester
    START_URL = 'http://www.honeybitcoin.com/'
    GOTO_URL = 'http://www.honeybitcoin.com/index.php?lang=en'

=begin rdoc
Public method returning random number between limits
=end   

    def aleat
      r = Random.new
      return r.rand(300..315)
    end

=begin rdoc
Public method checking if instance is on the true page
=end   

    def check_page
      @b.goto START_URL
      if
        @b.title == "Honey Bitcoin"
      then
        return true
      else
        return false
      end  
    end

=begin rdoc
In order to create a valid uploader instance pass credentials of some registered account.
=end   
    def initialize(data)
      @proxy = data
      make_agent(@proxy)   
    end
    
    def finalize
      kill_agent
    end

=begin rdoc
Logs into the site using address passed to constructor.
Returns true value on success and false value if login failed.
=end 
    def login(addr)
      @addr = addr
      attempt = 0
      begin
        @b.goto START_URL
        sleep(10)
        @b.text_field(:id, "visitorADDRESS").when_present.set(@addr)
        @b.execute_script("ajax_newVisitor();")
      rescue
        attempt += 1
        retry unless attempt > 2
        raise "proxy unable to connect"
      end
    end      
    
=begin rdoc
wait until next_button is present. click when present
=end
    def wait_next
      @b.execute_script("check_VALID_URL_VISIT();")
      sleep(10)
    end

=begin rdoc
unless legal limit, process.
=end

   def check_visit?
      if
       @b.text.include?("Has llegado a tu cupo por hoy, vuelve dentro de 24H por")
      then
        raise "24h limit over"
      elsif
       @b.text.include?("De momento no hay ninguna URL para visitar, pasate")
      then
        raise "no more urls to visit"        
      elsif
       @b.text.include?("Internal Server Error")       
      then
        raise "internal server error"
      elsif
       @b.text.include?("The server encountered an internal error or misconfiguration and was unable to complete your request.")
      then
        raise "internal server error"        
      else       
        return true
      end  
    end

    def mark(quand)
      doc = Nokogiri::HTML(@b.html) 
      balance = doc.search('//*[@id="VCP_visitorTOTAL_REWARD"]').text
      current_addr = doc.search('//*[@id="VCP_visitorADDRESS"]').text      
      puts quand << " - " << @proxy << " - " << current_addr << " - " << balance     
    end    
    
    def process
      sleep(20)
      mark("begin")
      while check_visit?
        Watir::Wait.until(aleat) { 
          text = @b.text
          text.include? "Siguiente URL" or text.include? "Next URL"
        }
        wait_next
      end
      mark("end")
    end    
 
=begin rdoc
In order to create a valid harvester instance pass btc address.
=end  
  private
    def make_agent(proxy_tested)
      proxy_addr, proxy_port = proxy_tested.split(':')                   
      profile = Selenium::WebDriver::Firefox::Profile.new
      profile["network.proxy.type"] = 1      
      profile["network.proxy.http"] = proxy_addr
      profile["network.proxy.http_port"] = proxy_port.to_i    
      @b = Watir::Browser.new :firefox, :profile => profile   
    end  
    
    def kill_agent
      @b.close
    end  
  end
end