HTTP Downloader

require ‘net/http’

puts “Enter the URL of the site you want to download from (You must have
http:// and www. if the site has it). e.g. http://www.google.com
site = gets.chomp.to_s

puts “Enter the sub-URL of the file you want to download. e.g.
/img/1.png”
subURL = gets.chomp.to_i

puts “The file will download into the directory this script is placed
in.”

Net::HTTP.start(“#{site}”) { |http|
resp = http.get(“#{subURL}”)
open(“fun.jpg”, “wb”) { |file|
file.write(resp.body)
}
}
puts “Done!”

Again, an error. This time this script is for easily downloading videos
that play in the browser etc.

Why are you trying to convert the subURL to an integer? That’s what to_i
does. What’s the actual error you’re getting? My guess is that HTTP is
saying it can’t find that page. You might want to try to trap that
error, so
you know better what’s going on.

On Tue, Aug 17, 2010 at 9:13 AM, Hd Pwnz0r

On 17 August 2010 14:13, Hd Pwnz0r [email protected]
wrote:

Again, an error.

An error you say? What error would that be and what steps have you
taken to investigate this problem?

The problem is that you must not have the http:// part of the url.

Remove that and it will work.

Peter H. wrote:

The problem is that you must not have the http:// part of the url.

Remove that and it will work.

Thank you; mucho appreciato! :smiley:

Peter H. wrote:

The problem is that you must not have the http:// part of the url.

Remove that and it will work.

Sorry to double post but is there any way to get the script to extract
the extension from the site and add it to the end of the filename?

Thanks

Andrew W. wrote:

Why are you trying to convert the subURL to an integer? That’s what to_i
does. What’s the actual error you’re getting? My guess is that HTTP is
saying it can’t find that page. You might want to try to trap that
error, so
you know better what’s going on.

On Tue, Aug 17, 2010 at 9:13 AM, Hd Pwnz0r

Oops. I dunno why I did that. Anyway, It’s this error:

C:/Ruby191/lib/ruby/1.9.1/net/http.rb:581:in ‘initialize’:getaddrinfo:
The storage control blocks were destroyed. (SocketError)

As an aside. Here’s something to note when reporting errors.

I saved your script as a file called fred.rb and ran it:

/usr/local/lib/ruby/1.8/net/http.rb:560:in initialize': getaddrinfo: nodename nor servname provided, or not known (SocketError) from /usr/local/lib/ruby/1.8/net/http.rb:560:inopen’
from /usr/local/lib/ruby/1.8/net/http.rb:560:in connect' from /usr/local/lib/ruby/1.8/timeout.rb:53:intimeout’
from /usr/local/lib/ruby/1.8/timeout.rb:93:in timeout' from /usr/local/lib/ruby/1.8/net/http.rb:560:inconnect’
from /usr/local/lib/ruby/1.8/net/http.rb:553:in do_start' from /usr/local/lib/ruby/1.8/net/http.rb:542:instart’
from /usr/local/lib/ruby/1.8/net/http.rb:440:in `start’
from fred.rb:12

The first line tells you what the problem is, however this is inside a
library function and is probably not where the problem lies. Note the
“from fred.rb: 12” at the bottom. That is where in your program the
error actually occurred and is what you want to know. It is the line
containing “Net::HTTP.start(”#{site}") { |http|", which shows where
ruby is having a problem. Sometimes these stack traces can get very
long but the useful information tends to be at the top and bottom of
the output. It is useful to provide these parts of a stack trace (you
can trim out the middle if the trace is long) and the relevant lines
of code around where the problem occurs. Such as

10  puts "The file will download into the directory this script is

placed in."
11
12 Net::HTTP.start("#{site}") { |http|
13 resp = http.get("#{subURL}")
14 open(“fun.jpg”, “wb”) { |file|

This will help people help you.

Wow, really? Why the terrible error message then?

On Tue, Aug 17, 2010 at 9:35 AM, Peter H. <

To be honest I would not use the net/http library if you can help it.

require ‘open-uri’

url = “http://www.google.com/intl/en_com/images/srpr/logo1w.png

open(“fun.jpg”, “wb”) { |file|
file.write(open(url).read)
}

The open-uri library is a god send for this kind of thing.