Hi. I have a small script that I use to download archives from the web.
The code looks like this:
require 'rubygems'
require 'mechanize'
def downloader()
agent = WWW::Mechanize.new
# create agent object
page = agent.get('http://www.myurl.com')
#authenticate
form = page.forms.first
form.username = '####'
form.password = '####'
#submit form
page = agent.submit form
#grab all links that have zip in them
mylinks = page.links_with(:href => /zip\//)
#for each link download and rename
mylinks.each do |archive|
name = archive.href.split('zip/')[1].sub('/','').strip
puts "Archive #{name} is saving..."
myfile = agent.click(archive)
output = File.open("/Users/luka/Desktop/#{name}.zip", 'w') {|file|
file << myfile.body }
puts "Done!"
end
end
begin
downloader
rescue Timeout::Error
puts 'Timeout was detected. Trying again...'
downloader
end
However, I've been having problems with timeouts from time to time, so I
ended up making this code into a function and upon timeout invoke this
function once again, as you can see at the bottom of the code. However,
I still get an error, which looks like this on timeout:
Archive 914188 is saving...
Timeout was detected. Trying again...
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:60:in
`rbuf_fill': execution expired (Timeout::Error)
So it tries to rescue the timeout but obviously calling the same
function again is a bad idea. I was wondering why is it, and what would
be a proper solution.
Thanks!
Luka
···
--
Posted via http://www.ruby-forum.com/.