Downloading an MP3 from the internet

Discussion in 'Ruby' started by Nathaniel Escribano, Jan 14, 2009.

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

    I am trying to write a simple script that downloads a music file from the
    internet.
    For example: if i want to download
    http://cdn.stereogum.com/mp3/My Morning Jacket - Celebration (Live).mp3
    how exactly would you do it?

    I found someone who was able to download a flickr photo using this script:

    require 'net/http'

    Net::HTTP.start("farm1.static.flickr.com") { |http|
    resp = http.get("/92/218926700_ecedc5fef7_o.jpg")
    open("fun.jpg", "wb") { |file|
    file.write(resp.body)
    }
    }
    puts 'Downloaded'

    With a slight modification shouldn't this work for the music file?
    What makes downloading a photo any different than an mp3 file?

    require 'net/http'

    Net::HTTP.start("cdn.stereogum.com") { |http|
    resp = http.get("/mp3/My Morning Jacket - Celebration (Live).mp3")
    open("Celebration.mp3", "wb") { |file|
    file.write(resp.body)
    }
    }
    puts "the song has been downloaded"

    Why doesn't it work? What needs to be changed?
    Any help would be great! Thanks.
    Nathaniel Escribano, Jan 14, 2009
    #1
    1. Advertising

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

    On Wed, Jan 14, 2009 at 3:15 AM, Nathaniel Escribano <
    > wrote:


    > I am trying to write a simple script that downloads a music file from the
    > internet.
    > For example: if i want to download
    >
    > http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20(Live).mp3<http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20%28Live%29.mp3>
    > how exactly would you do it?
    >
    > I found someone who was able to download a flickr photo using this script:
    >
    > require 'net/http'
    >
    > Net::HTTP.start("farm1.static.flickr.com") { |http|
    > resp = http.get("/92/218926700_ecedc5fef7_o.jpg")
    > open("fun.jpg", "wb") { |file|
    > file.write(resp.body)
    > }
    > }
    > puts 'Downloaded'
    >
    > With a slight modification shouldn't this work for the music file?
    > What makes downloading a photo any different than an mp3 file?
    >
    > require 'net/http'
    >
    > Net::HTTP.start("cdn.stereogum.com") { |http|
    > resp = http.get("/mp3/My Morning Jacket - Celebration (Live).mp3")
    > open("Celebration.mp3", "wb") { |file|
    > file.write(resp.body)
    > }
    > }
    > puts "the song has been downloaded"
    >
    > Why doesn't it work? What needs to be changed?
    > Any help would be great! Thanks.
    >


    Probably because you're not encoding the path

    --
    Andrew Timberlake
    http://ramblingsonrails.com
    http://www.linkedin.com/in/andrewtimberlake

    "I have never let my schooling interfere with my education" - Mark Twain
    Andrew Timberlake, Jan 14, 2009
    #2
    1. Advertising

  3. Nathaniel Escribano wrote:
    > require 'net/http'
    >
    > Net::HTTP.start("cdn.stereogum.com") { |http|
    > resp = http.get("/mp3/My Morning Jacket - Celebration (Live).mp3")
    > open("Celebration.mp3", "wb") { |file|
    > file.write(resp.body)
    > }
    > }
    > puts "the song has been downloaded"


    Here is an alternative way of doing it:

    require 'open-uri'
    open("http://cdn.stereogum.com/mp3/My Morning Jacket - Celebration
    (Live).mp3") do |src|
    open("Celebration.mp3","wb") do |dst|
    while blk = src.read(65536)
    dst.write(blk)
    end
    end
    end

    This gives a clear error:

    /usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?):
    http://cdn.stereogum.com/mp3/My Morning Jacket - Celebration (Live).mp3
    (URI::InvalidURIError)

    Fix the URI and it works:

    ...
    open("http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20%28Live%29.mp3")
    do |src|
    ...
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 14, 2009
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    Thank you guys very much!
    I was able to get it to work by fixing my path.
    I wasn't able to get it to work using alternate way even after changing the
    URL.
    I get the following error.

    syntax error, unexpected kDO, expecting $end
    do |src|

    Just out of curiosity- how can I fix this.
    I would like to know so that I can work through it and figure out exactly
    what the script is doing and
    compare it to the previous script.
    Thanks again!


    On Wed, Jan 14, 2009 at 4:30 AM, Brian Candler <> wrote:

    > Nathaniel Escribano wrote:
    > > require 'net/http'
    > >
    > > Net::HTTP.start("cdn.stereogum.com") { |http|
    > > resp = http.get("/mp3/My Morning Jacket - Celebration (Live).mp3")
    > > open("Celebration.mp3", "wb") { |file|
    > > file.write(resp.body)
    > > }
    > > }
    > > puts "the song has been downloaded"

    >
    > Here is an alternative way of doing it:
    >
    > require 'open-uri'
    > open("http://cdn.stereogum.com/mp3/My Morning Jacket - Celebration
    > (Live).mp3") do |src|
    > open("Celebration.mp3","wb") do |dst|
    > while blk = src.read(65536)
    > dst.write(blk)
    > end
    > end
    > end
    >
    > This gives a clear error:
    >
    > /usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?):
    > http://cdn.stereogum.com/mp3/My Morning Jacket - Celebration (Live).mp3
    > (URI::InvalidURIError)
    >
    > Fix the URI and it works:
    >
    > ...
    > open("
    > http://cdn.stereogum.com/mp3/My Morning Jacket - Celebration (Live).mp3
    > ")
    > do |src|
    > ...
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Nathaniel Escribano, Jan 14, 2009
    #4
  5. Nathaniel Escribano wrote:
    > I get the following error.
    >
    > syntax error, unexpected kDO, expecting $end
    > do |src|


    It was a line-wrap from ruby-forum. Move this to the end of the line
    above.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 14, 2009
    #5
  6. Nathaniel Escribano

    Davi Vidal Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Nathaniel Escribano wrote:
    [...]
    >> Here is an alternative way of doing it:
    >>
    >> require 'open-uri'
    >> open("http://cdn.stereogum.com/mp3/My Morning Jacket - Celebration
    >> (Live).mp3") do |src|
    >> open("Celebration.mp3","wb") do |dst|
    >> while blk = src.read(65536)
    >> dst.write(blk)
    >> end
    >> end
    >> end
    >>

    [...]

    My $ 0.02:

    require 'open-uri'
    require 'cgi'

    url = 'http://cdn.stereogum.com/mp3/' + CGI::escape('My Morning Jacket -
    Celebration (Live).mp3')


    open(url) do |src|
    open('Celebration.mp3', 'wb') do |dst|
    while blk = src.read(65536)
    dst.write(blk)
    end
    end
    end


    hth,


    davi
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iEYEARECAAYFAkluE4MACgkQ76Bs0E5RGKNDnQCgnL2MY93V0uAhc1dQGV1cTP09
    NaEAoKFroaj3pxGPrjf9u2CUzKV2gFAK
    =/OkJ
    -----END PGP SIGNATURE-----
    Davi Vidal, Jan 14, 2009
    #6
    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. Daniel Dyer
    Replies:
    6
    Views:
    19,876
    Knute Johnson
    Jan 22, 2006
  2. Asle Frantzen

    Mp3-player - how to play an mp3?

    Asle Frantzen, Jul 8, 2003, in forum: C++
    Replies:
    5
    Views:
    663
    Kelsey Bjarnason
    Jul 22, 2003
  3. Dustan
    Replies:
    0
    Views:
    262
    Dustan
    Jun 1, 2006
  4. Andreas Leitgeb
    Replies:
    2
    Views:
    369
    Andreas Leitgeb
    Jul 10, 2008
  5. HY Tech
    Replies:
    0
    Views:
    615
    HY Tech
    Sep 7, 2003
Loading...

Share This Page