Class: HttpUtilities::Proxies::ProxyChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/http_utilities/proxies/proxy_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ ProxyChecker

Returns a new instance of ProxyChecker.



10
11
12
13
14
15
16
17
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 10

def initialize
  self.client                       =   HttpUtilities::Http::Client.new
  self.processed_proxies            =   []
  
  self.limit                        =   1000
  self.minimum_successful_attempts  =   1
  self.maximum_failed_attempts      =   10
end

Instance Attribute Details

#client ⇒ Object

Returns the value of attribute client.



7
8
9
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 7

def client
  @client
end

#limit ⇒ Object

Returns the value of attribute limit.



8
9
10
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 8

def limit
  @limit
end

#maximum_failed_attempts ⇒ Object

Returns the value of attribute maximum_failed_attempts.



8
9
10
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 8

def maximum_failed_attempts
  @maximum_failed_attempts
end

#minimum_successful_attempts ⇒ Object

Returns the value of attribute minimum_successful_attempts.



8
9
10
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 8

def minimum_successful_attempts
  @minimum_successful_attempts
end

#processed_proxies ⇒ Object

Returns the value of attribute processed_proxies.



7
8
9
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 7

def processed_proxies
  @processed_proxies
end

Instance Method Details

#check_and_update_proxies(protocol = :all, proxy_type = :all, mode = :synchronous) ⇒ Object



19
20
21
22
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 19

def check_and_update_proxies(protocol = :all, proxy_type = :all, mode = :synchronous)
  check_proxies(protocol, proxy_type, mode)
  update_proxies
end

#check_proxies(protocol = :all, proxy_type = :all, mode = :synchronous) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 24

def check_proxies(protocol = :all, proxy_type = :all, mode = :synchronous)
  proxies = Proxy.should_be_checked(protocol, proxy_type, Time.now, self.limit)

  if (proxies && proxies.any?)
    Rails.logger.info "Found #{proxies.size} #{proxy_type} proxies to check."

    proxies.each do |proxy|
      case mode
        when :synchronous
          check_proxy(proxy)
        when :resque
          Resque.enqueue(HttpUtilities::Jobs::Resque::Proxies::CheckProxyJob, proxy.id)
        when :sidekiq
          HttpUtilities::Jobs::Sidekiq::Proxies::CheckProxyJob.perform_async(proxy.id)
      end
    end

  else
    Rails.logger.info "Couldn't find any proxies to check!"
  end
end

#check_proxy(proxy, timeout = 60) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 46

def check_proxy(proxy, timeout = 60)
  document      =   nil
  valid_proxy   =   false

  options = {:method              =>  :net_http,
             :use_proxy           =>  true, 
             :proxy               =>  proxy.proxy_address, 
             :proxy_protocol      =>  proxy.protocol,
             :timeout             =>  timeout,
             :maximum_redirects   =>  1,
             :disable_auth        =>  true
            }

  Rails.logger.info "#{Time.now}: Fetching Proxy #{proxy.proxy_address}."

  response            =   self.client.retrieve_parsed_html("http://www.google.com/webhp?hl=en", options)

  if (response && response.parsed_body)
    title             =   response.parsed_body.css("title").first

    if (title && title.content)
      begin
        title         =   title.content.encode("UTF-8").strip.downcase
        body_content  =   response.parsed_body.content.to_s.encode("UTF-8").strip.downcase
        
        valid_proxy   =   (title.eql?("google") || !(body_content =~ /google home/i).nil?)
        
        Rails.logger.info "Title is: #{title}. Proxy #{proxy.proxy_address}"
        
      rescue Exception => e
        Rails.logger.error "Exception occured while trying to validate proxy. Error Class: #{e.class}. Error Message: #{e.message}"
        valid_proxy   =   false
      end
    end
  end

  if (valid_proxy)
    Rails.logger.info "#{Time.now}: Proxy #{proxy.proxy_address} is working!"
  else
    Rails.logger.info "#{Time.now}: Proxy #{proxy.proxy_address} is not working!"
  end

  self.processed_proxies << {:proxy => proxy, :valid => valid_proxy}
end

#update_proxies ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 91

def update_proxies()
  columns   =   [:host, :port, :last_checked_at, :valid_proxy, :successful_attempts, :failed_attempts]
  values    =   []

  Rails.logger.info "Updating/Importing #{self.processed_proxies.size} proxies"

  if (self.processed_proxies && self.processed_proxies.any?)
    self.processed_proxies.each do |value|
      proxy                 =   value[:proxy]
      valid                 =   value[:valid]
      successful_attempts   =   proxy.successful_attempts
      failed_attempts       =   proxy.failed_attempts

      if (valid)
        successful_attempts +=  1
      else
        failed_attempts     +=  1
      end

      is_valid              =   (successful_attempts >= self.minimum_successful_attempts && failed_attempts < self.maximum_failed_attempts)
      value_arr             =   [proxy.host, proxy.port, Time.now, is_valid, successful_attempts, failed_attempts]
      values                <<  value_arr
    end

    ::Proxy.import(columns, values, :on_duplicate_key_update => [:last_checked_at, :valid_proxy, :successful_attempts, :failed_attempts], :validate => false)
  end

end