All pastes #838241 Raw Edit

Miscellany

public text v1 · immutable
#838241 ·published 2007-12-30 06:53 UTC
rendered paste body
#!/usr/bin/ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'
require './Proxies/Proxies'
require 'timeout'
#TimeoutX.replace_timeout
#proxy = Proxies.getRandomProxy
proxies = YAML.load_file("./Proxies/proxies.yml")
proxies.each_with_index do |proxy, index|
  puts "Testing proxy ##{index} at #{proxy}"

  begin
    #set a timeout. Proxies which can't pass the timeout will be deleted from the list.
    Timeout.timeout(8){
      begin
        #check whether the proxy works with Hpricot (actually, open-uri)
        doc = Hpricot(open('http://tinyurl.com/api-create.php?url=%22http://www.google.com%22', :proxy=>proxy))
        if (doc.to_s != "http://tinyurl.com/57dcb")
          #this proxy is redirecting me. get rid of it.
          raise Timeout::Error
        else
          puts "Proxy #{index} (#{proxy}) is OK."
        end
      rescue
        puts proxy + " timed out."
        proxies.delete_at(index)
        next
      end
    }
  rescue Timeout::Error
    puts proxy + " timed out."
    proxies.delete_at(index)
  end
end

File.open("./Proxies/proxies.yml", "w") do |file|
  file << proxies.to_yaml
end