Class: ProxyHandler
- Inherits:
-
Object
- Object
- ProxyHandler
- Defined in:
- lib/linsc/proxy_handler.rb
Instance Method Summary collapse
- #get_proxy ⇒ Object
-
#initialize(cooldown_time = 5) ⇒ ProxyHandler
constructor
A new instance of ProxyHandler.
- #length ⇒ Object
Constructor Details
#initialize(cooldown_time = 5) ⇒ ProxyHandler
Returns a new instance of ProxyHandler.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/linsc/proxy_handler.rb', line 5 def initialize(cooldown_time = 5) @cooldown_time = cooldown_time proxy_file = Pathname.new(File.dirname __dir__).realdirpath + '../data/proxies.txt' agents_file = Pathname.new(File.dirname __dir__).realdirpath + '../data/agents.txt' @proxy_list = File.read(proxy_file.to_s).split("\n") .collect{|proxy| proxy.split(':')} @proxies = [] @ua_list = File.read(agents_file.to_s).split("\n") @proxy_list.each do |proxy_details| proxy = Proxy.new(ip: proxy_details[0], port: proxy_details[1], username: proxy_details[2], password: proxy_details[3], status: 'good', last_used: Time.now - @cooldown_time, user_agent: @ua_list.shift) @proxies << proxy end if @proxies.length == 0 puts "proxies.txt is empty! if you don't want to use any proxies, use the -n flag. see docs for more." exit end end |
Instance Method Details
#get_proxy ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/linsc/proxy_handler.rb', line 28 def get_proxy @good_proxies = @proxies.select { |proxy| proxy.good? } if @good_proxies.length > 0 @good_proxies.sort!{|a, b| a.last_used <=> b.last_used} best_proxy = @good_proxies.first duration = Time.now - best_proxy.last_used sleep(@cooldown_time - duration) if duration < @cooldown_time best_proxy else puts "All proxies are dead. Wait a few hours before resuming." @proxies.each do |proxy| puts "ip: #{proxy.ip} .... pages before burnout: #{proxy.pages_before_burnout} .... time of burnout: #{proxy.burnout_time}" end exit end end |
#length ⇒ Object
45 46 47 |
# File 'lib/linsc/proxy_handler.rb', line 45 def length @proxies.length end |