Finding the size of a file on web server?

C

CBlair1986

I'm writing a little script to help automate downloading videos off of
YouTube, and I would like to know how to figure out the size of the
video files I will be downloading. I've experimented briefly with
Net::HTTP, but it's a little beyond me, as I haven't been able to
figure it out.

The URLs I'm using look like such:

http://www.youtube.com/get_video?video_id=put_real_video_id_here

and I'm getting the video id by a simple regexp. The trouble isn't
finding the file size, per-se, but finding it *before* I download the
entire thing.

I don't know if this is possible or not, but any help on the subject
would be greatly appreciated!

Thank You
 
D

Daniel Martin

CBlair1986 said:
I'm writing a little script to help automate downloading videos off of
YouTube, and I would like to know how to figure out the size of the
video files I will be downloading. I've experimented briefly with
Net::HTTP, but it's a little beyond me, as I haven't been able to
figure it out.

Well, this depends on what you want to do - do you want to ask for the
size of each file separately, or is it acceptable if you ask to get
the download, and can get information about the size of the file the
instant that the download starts?

In the first case, it depends on what information youtube is willing
to give out in response to a HEAD request - not every webserver will
give you a content-length header in HEAD, or even necessarily allow
HEAD requests.

To test the first case, try this in irb:
require 'net/http'
myvidlen = Net::HTTP.start('www.youtube.com', 80) {|http|
http.head('/get_video?video_id=real_video_id').content_length
}

For the second case, you're going to need to do something like this:
require 'net/http'
Net::HTTP.start('www.youtube.com', 80) {|http|
http.request_get('/get_video?video_id=real_video_id') {|resp|
myvidlen = resp.content_length
puts "Video real_video_id has length #{myvidlen}"
# use the length to do something, like choose a directory
File.open('whereISaveMyVideo.whatever', 'wb') {|f|
response.read_body { |data|
f.print(data)
}
}
}
}
 
C

CBlair1986

Well, the first method doesn't work, and the second method isn't
something that I'm looking for. I'd like to confirm if I'd actually
like to download the file. Thanks anyways.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top