-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxymanager.rb
84 lines (65 loc) · 1.97 KB
/
proxymanager.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
class ProxyManager
class Proxy
attr_reader :address
attr_accessor :last_used
def initialize(address, last_used=nil)
@address = address
@last_used = last_used
end
def used_since?(time)
@last_used && @last_used > time
end
def to_s
@address
end
end
def self.from_proxy_file(path="proxies.txt", delay=10)
proxies = IO.readlines(path).map { |line| line.strip }
new(proxies, delay)
end
def initialize(proxies, delay=10)
raise ArgumentError, "proxies must contain at least 1 proxy" if proxies.empty?
raise ArgumentError, "proxies must be unique, but duplicates were found: #{duplicates(proxies).join(', ')}" if proxies.size != proxies.uniq.size
@addresses = proxies
@delay = delay
@proxies = proxies.map { |address| Proxy.new(address) }
end
def duplicates(list)
list.group_by { |e| e }.select { |k,v| v.size > 1 }.map(&:first)
end
# A proxy that hasn't been used for at least #delay seconds.
# If none is available, the method will block until one becomes available
def available_proxy
proxy = @proxies.shift
@proxies << proxy
if proxy.last_used then
nap_time = @delay - (Time.now - proxy.last_used)
sleep(nap_time) if nap_time > 0
end
proxy.last_used = Time.now
proxy
end
# This will block until n proxies are available
def available_proxies(n)
return nil if n > @proxies.size
proxies = @proxies.shift(n)
proxy = proxies.last
@proxies.concat(proxies)
if proxy.last_used then
nap_time = @delay - (Time.now - proxy.last_used)
sleep(nap_time) if nap_time > 0
end
proxies.each do |proxy|
proxy.last_used = Time.now
end
proxies
end
def proxy_available?
[email protected]_since?(Time.now-@delay)
end
def available_count
time = Time.now-@delay
index = @proxies.find_index { |proxy| proxy.used_since?(time) }
index ? index : @proxies.size
end
end