Finding the size of a file on web server?

Discussion in 'Ruby' started by CBlair1986, Jul 16, 2006.

  1. CBlair1986

    CBlair1986 Guest

    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
     
    CBlair1986, Jul 16, 2006
    #1
    1. Advertising

  2. "CBlair1986" <> writes:

    > 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)
    }
    }
    }
    }
     
    Daniel Martin, Jul 16, 2006
    #2
    1. Advertising

  3. CBlair1986

    CBlair1986 Guest

    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.

    Daniel Martin wrote:
    > "CBlair1986" <> writes:
    >
    > > 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)
    > }
    > }
    > }
    > }
     
    CBlair1986, Jul 16, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tiewknvc9
    Replies:
    6
    Views:
    695
    Chris Uppal
    Oct 1, 2006
  2. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,742
    Michael Jung
    May 25, 2008
  3. Keith Thompson

    Re: File Size - Big File Size

    Keith Thompson, Oct 1, 2009, in forum: C Programming
    Replies:
    6
    Views:
    314
    Phil Carmody
    Oct 3, 2009
  4. Michael Tsang

    Re: File Size - Big File Size

    Michael Tsang, Oct 4, 2009, in forum: C Programming
    Replies:
    2
    Views:
    343
    Keith Thompson
    Oct 4, 2009
  5. Nobody

    Re: File Size - Big File Size

    Nobody, Oct 5, 2009, in forum: C Programming
    Replies:
    10
    Views:
    2,091
    Flash Gordon
    Oct 10, 2009
Loading...

Share This Page