# Downloading an MP3 from the internet

**URL:** <https://www.ruby-forum.com/t/downloading-an-mp3-from-the-internet/157646>\
**Category:** Ruby\
**Created:** [January 14, 2009, 2:17am UTC](https://www.ruby-forum.com/t/downloading-an-mp3-from-the-internet/157646 "2009-01-14T02:17:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel\_Escribano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@Nathaniel\_Escribano](https://www.ruby-forum.com/u/Nathaniel_Escribano)\
**Post date:** [January 14, 2009, 2:17am UTC](https://www.ruby-forum.com/t/downloading-an-mp3-from-the-internet/157646/1 "2009-01-14T02:17:18Z")

</div>

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(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://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://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.

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Escribano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@Nathaniel\_Escribano](https://www.ruby-forum.com/u/Nathaniel_Escribano)\
**Post date:** [January 14, 2009, 4:16am UTC](https://www.ruby-forum.com/t/downloading-an-mp3-from-the-internet/157646/2 "2009-01-14T04:16:41Z")

</div>

On Wed, Jan 14, 2009 at 3:15 AM, Nathaniel Escribano  
\<removed\_email\_address@domain.invalid

> wrote:

> 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 T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Escribano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@Nathaniel\_Escribano](https://www.ruby-forum.com/u/Nathaniel_Escribano)\
**Post date:** [January 14, 2009, 10:31am UTC](https://www.ruby-forum.com/t/downloading-an-mp3-from-the-internet/157646/3 "2009-01-14T10:31:07Z")

</div>

Nathaniel Escribano wrote:

> require ‘net/http’
> 
> Net::HTTP.start(“[cdn.stereogum.com](http://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](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](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(Live).mp3](http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20%28Live%29.mp3)”)  
do |src|  
…

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Escribano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@Nathaniel\_Escribano](https://www.ruby-forum.com/u/Nathaniel_Escribano)\
**Post date:** [January 14, 2009, 5:05pm UTC](https://www.ruby-forum.com/t/downloading-an-mp3-from-the-internet/157646/4 "2009-01-14T17:05:24Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Escribano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@Nathaniel\_Escribano](https://www.ruby-forum.com/u/Nathaniel_Escribano)\
**Post date:** [January 14, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/downloading-an-mp3-from-the-internet/157646/5 "2009-01-14T17:07:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Escribano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@Nathaniel\_Escribano](https://www.ruby-forum.com/u/Nathaniel_Escribano)\
**Post date:** [January 14, 2009, 6:01pm UTC](https://www.ruby-forum.com/t/downloading-an-mp3-from-the-internet/157646/6 "2009-01-14T18:01:22Z")

</div>

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

Nathaniel Escribano wrote:  
[…]

> > end

[…]

My $ 0.02:

require ‘open-uri’  
require ‘cgi’

url = ‘[http://cdn.stereogum.com/mp3/](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](http://enigmail.mozdev.org)

iEYEARECAAYFAkluE4MACgkQ76Bs0E5RGKNDnQCgnL2MY93V0uAhc1dQGV1cTP09  
NaEAoKFroaj3pxGPrjf9u2CUzKV2gFAK  
=/OkJ  
-----END PGP SIGNATURE-----
