HTTP Downloader

H

Hd Pwnz0r

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.
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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.
 
H

Hd Pwnz0r

Andrew said:
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.
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)
 
P

Peter Hickman

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

Remove that and it will work.
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

Wow, really? Why the terrible error message then?

On Tue, Aug 17, 2010 at 9:35 AM, Peter Hickman <
 
P

Peter Hickman

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:in `open'
from /usr/local/lib/ruby/1.8/net/http.rb:560:in `connect'
from /usr/local/lib/ruby/1.8/timeout.rb:53:in `timeout'
from /usr/local/lib/ruby/1.8/timeout.rb:93:in `timeout'
from /usr/local/lib/ruby/1.8/net/http.rb:560:in `connect'
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:in `start'
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.
 
H

Hd Pwnz0r

Peter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top