Class: Proxylinker::ProxyList

Inherits:
Object
  • Object
show all
Defined in:
lib/proxylinker/proxy_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy_file_path, timeout: 5) ⇒ ProxyList

Initialiserer ProxyList med en filsti og timeout-parameter (standard: 5 sek.)



10
11
12
13
14
# File 'lib/proxylinker/proxy_list.rb', line 10

def initialize(proxy_file_path, timeout: 5)
  @proxy_file_path = proxy_file_path
  @timeout = timeout
  @proxies = load_proxies
end

Instance Attribute Details

#proxiesObject (readonly)

Returns the value of attribute proxies.



7
8
9
# File 'lib/proxylinker/proxy_list.rb', line 7

def proxies
  @proxies
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



7
8
9
# File 'lib/proxylinker/proxy_list.rb', line 7

def timeout
  @timeout
end

Instance Method Details

#fetch_with_proxy(uri_str) ⇒ Object

Metode til at udføre en HTTP-get-anmodning med eller uden proxy



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/proxylinker/proxy_list.rb', line 32

def fetch_with_proxy(uri_str)
  uri = URI.parse(uri_str)
  proxy = get_random_proxy

  begin
    http = Net::HTTP.new(uri.host, uri.port, proxy.addr, proxy.port)
    http.open_timeout = @timeout
    http.read_timeout = @timeout
    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)
    return response.body
  rescue
    # Hvis der opstår en fejl med proxyen, prøv uden proxy
    http = Net::HTTP.new(uri.host, uri.port)
    http.open_timeout = @timeout
    http.read_timeout = @timeout
    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)
    return response.body
  end
end

#get_random_proxyObject

Hent en tilfældig proxy uden at validere



27
28
29
# File 'lib/proxylinker/proxy_list.rb', line 27

def get_random_proxy
  @proxies.sample
end

#load_proxiesObject

Læs proxies fra en fil (forventet format: IP:PORT per linje)



17
18
19
20
21
22
23
24
# File 'lib/proxylinker/proxy_list.rb', line 17

def load_proxies
  proxies = []
  File.readlines(@proxy_file_path).each do |line|
    addr, port = line.strip.split(':')
    proxies << OpenStruct.new(addr: addr, port: port)
  end
  proxies
end